-----BEGIN PGP SIGNED MESSAGE-----
Hash: RIPEMD160

Hello, Lasantha

sure, no problem. I attach the 2 source files for the 2 CMPs. Keep in
mind that I'm german (see all the descr's) :)

Best regards

Dirk

Lasantha Ranaweera schrieb:
> Hi Dirk,
> 
> Would you mind sharing your code with us?
> 
> Thanks,
> Lasantha Ranaweera
> 
> Kaeto23 wrote:
>> Hello,
>>
>> OK, I tried your way now and at least Geronimo keeps talking to me. The
>> stacktrace now reads as follows:
>>
>> Caused by: org.tranql.ql.QueryException: Select [Select
>> method=[ejbSelectForumPostsNum]; EJB-QL=[select distinct count(p.id)
>> from Post as p where p.thread.forum.id=?1]] does not return a EJB and
>> defines the prefetch group [Default].
>>      at
>> org.tranql.builder.SQLQueryBuilder.buildSelects(SQLQueryBuilder.java:224)
>>      at
>> org.openejb.deployment.CMPContainerBuilder.buildIt(CMPContainerBuilder.java:311)
>>      at
>> org.openejb.deployment.AbstractContainerBuilder.createConfiguration(AbstractContainerBuilder.java:354)
>>      at
>> org.openejb.deployment.CMPEntityBuilder.createBean(CMPEntityBuilder.java:213)
>>      ... 32 more
>>
>>
>> It seems Geronimo wants a EJB in return. Counting is still a problem ;)
>>
>> I just consider to switch to JOnAS or JBoss ;)
>>
>> Milan: how did you count the EJBs? Do you also use the "Default"
>> prefetch group (only the CMP fields are loaded instead of CMP+CMR)?
>>
>> Best regards
>>
>> Dirk
>>
>>
>> Unger, Milan schrieb:
>>   
>>> Hello, 
>>>
>>> I faced similar problem, the trick to use following EJB QL: 
>>>  
>>> select distinct count(p.key) from Post as p where
>>> p.thread.forum.id=?1
>>>
>>> notice count(p.key) - where key should be a property of CMP bean. 
>>> I'm not sure if such syntax is required by EJB spec or simply a bug in
>>> geronimo/open-ejb.
>>>
>>> Regards, Milan. 
>>>
>>> -----Original Message-----
>>> From: Kaeto23 [mailto:[EMAIL PROTECTED] 
>>> Sent: Thursday, October 19, 2006 6:55 PM
>>> To: Geronimo ML
>>> Subject: ejbSelect tutorial?
>>>
>>> Hello,
>>>
>>> today I started to work on a CMP that also provides a ejbSelect for
>>> counting some contents from a database. Basically, a CMP named Post
>>> which should count how much entries exist in the database.
>>>
>>> Nothing so hard ;)
>>>
>>> Anyway, Geronimo has some trouble to use the EJB so I'd like to know
>>> WHAT is the problem :/
>>>
>>> Here the code snippet that, when not commented out, makes the trouble:
>>>
>>>
>>>
>>>
>>> @XD5EjbSelectMethode(descr = "returns the number of entries", ejbQL =
>>> "select distinct count(p) from Post as p where p.thread.forum.id=?1")
>>> public abstract long ejbSelectForumPostsNum(long forumId) throws
>>> FinderException;
>>>
>>>
>>>
>>> Now XD5 creates the following deployment descriptor entries:
>>>
>>> ejb-jar.xml:
>>> ...
>>> <query>
>>>     <description>returns the number of entries</description>
>>>     <query-method>
>>>             <method-name>ejbSelectForumPostsNum</method-name>
>>>             <method-params>
>>>                     <method-param>long</method-param>
>>>             </method-params>
>>>     </query-method>
>>>     <ejb-ql>select distinct count(p) from Post as p where
>>> p.thread.forum.id=?1</ejb-ql>
>>> </query>
>>> ...
>>>
>>> openejb-jar.xml
>>> ...
>>> <query>
>>>     <query-method>
>>>             <method-name>ejbSelectForumPostsNum</method-name>
>>>             <method-params>
>>>                     <method-param>long</method-param>
>>> </method-params>
>>>     </query-method>
>>>     <group-name>Default</group-name>
>>> </query>
>>> ...
>>>
>>> If these lines are present, Geronimo will crash with this exception:
>>>
>>> Caused by: java.lang.ClassCastException: org.tranql.ql.EntityReference
>>>         at
>>> org.tranql.ejbqlcompiler.PathTransformer$Context.<init>(PathTransformer.
>>> java:401)
>>>         at
>>> org.tranql.ejbqlcompiler.PathTransformer$Context.<init>(PathTransformer.
>>> java:380)
>>>         at
>>> org.tranql.ejbqlcompiler.PathTransformer.visit(PathTransformer.java:73)
>>>         at org.tranql.ql.Query.visit(Query.java:59)
>>>         at
>>> org.tranql.ejbqlcompiler.InterRepresentationTransformer.transform(InterR
>>> epresentationTransformer.java:41)
>>>         at
>>> org.tranql.ejbqlcompiler.DerbyEJBQLCompilerFactory$DerbyEJBQLCompiler.tr
>>> ansform(DerbyEJBQLCompilerFactory.java:69)
>>>         at
>>> org.tranql.sql.EJBQLToPhysicalQuery.expandToSQLQuery(EJBQLToPhysicalQuer
>>> y.java:243)
>>>         at
>>> org.tranql.sql.EJBQLToPhysicalQuery.buildSelect(EJBQLToPhysicalQuery.jav
>>> a:171)
>>>         at
>>> org.tranql.sql.EJBQLToPhysicalQuery.buildSelects(EJBQLToPhysicalQuery.ja
>>> va:109)
>>>         at
>>> org.tranql.builder.SQLQueryBuilder.buildSelects(SQLQueryBuilder.java:204
>>> )
>>>         at
>>> org.openejb.deployment.CMPContainerBuilder.buildIt(CMPContainerBuilder.j
>>> ava:311)
>>>         at
>>> org.openejb.deployment.AbstractContainerBuilder.createConfiguration(Abst
>>> ractContainerBuilder.java:354)
>>>         at
>>> org.openejb.deployment.CMPEntityBuilder.createBean(CMPEntityBuilder.java
>>> :213)
>>>
>>>
>>> So, basically, do you have some examples or tutorials that show the
>>> usage of ejbSelects with Geronimo 1.1.1? Let me know :)
>>>
>>> Best regards
>>>
>>> Dirk
>>>
>>>
>>>     
>>>
>>>     
>>>             
>>> ___________________________________________________________ 
>>> Der frhe Vogel fngt den Wurm. Hier gelangen Sie zum neuen Yahoo! Mail:
>>> http://mail.yahoo.de
>>>
>>>     
>>
>>   
> 

- --
Kaeto23

HTTP: http://kaeto23.dnsalias.com (german!)
Jabber: jabber://[EMAIL PROTECTED]/DS2

Reclaim Your Inbox!
http://www.mozilla.org/products/thunderbird/
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFFPbbCbiSTaBU+G6oRAxusAKCLfCnTxPISU5ulmU8D591lEqMC+QCfZmbC
swIYNIUy+JIr1sg4M4TTAXs=
=lC7I
-----END PGP SIGNATURE-----
/**
 * 
 */
package ds2.forum.ejb;

import java.util.Collection;
import java.util.Date;

import javax.ejb.CreateException;
import javax.ejb.FinderException;

import ds2.forum.ejbIF.ForumLocal;
import ds2.forum.ejbIF.ForumLocalHome;
import ds2.forum.ejbIF.UserLocal;
import ds2.forum.ejbIF.UserLocalHome;
import ds2.forum.ejbIF.UserRolle;
import ds2.forum.ejbIF.UserRolleLocal;
import ds2.forum.ejbIF.UserRolleLocalHome;
import xd5.annotations.GenerierungsPolicy;
import xd5.annotations.XD5Param;
import xd5.annotations.ejb.XD5EjbCreateMethod;
import xd5.annotations.ejb.XD5EjbInterfaceMethod;
import xd5.annotations.ejb.XD5EjbInternalRef;
import xd5.annotations.ejb.XD5EjbSelectMethode;
import xd5.annotations.ejb.XD5EntityEjb;
import xd5.annotations.ejb.XD5EntityFieldMethod;
import xd5.annotations.ejb.XD5EntityRelation;
import xd5.annotations.ejb.XD5QueryMethode;
import xd5.annotations.geronimo.XD5GeronimoEntityEjb;
import kaeto23.j2ee.ejb.BaseCMP;
import kaeto23.mysqldb.SQLSupport;

/**
 * @author kaeto23
 *
 */
@XD5EntityEjb(descr = "Die Rolle eines Users in einem Forum", name = "ForumRolle", primKeyFeld = "id", tabName = "forumrolle",
localHomeIF="ds2.forum.ejbIF.local.ForumRolleHome",localIF="ds2.forum.ejbIF.local.ForumRolle",ejbLocalRefs= {
	@XD5EjbInternalRef(ejbName = "Forum", jndiName = "ejb/Forum"),
	@XD5EjbInternalRef(ejbName = "User", jndiName = "ejb/User"),
	@XD5EjbInternalRef(ejbName = "UserRolle", jndiName = "ejb/UserRolle")
},finderMethoden= {
	@XD5QueryMethode(descr = "liefert alle Rollen des angeg. Users f\u00fcr das angeg. Forum zur\u00fcck", ejbQL = "select distinct object(r) from ForumRolle as r where r.forum.id=?1 and r.user.username like ?2", name = "findForumRolleVonUser",params= [EMAIL PROTECTED](name = "forumId",fqKlasse="long",descr="Die Forenid"),@XD5Param(name = "username",fqKlasse="java.lang.String",descr="Der Username")})
})
@XD5GeronimoEntityEjb(localJndiName = "ejb/ds2/forum/ForumRolle")
public abstract class ForumRolleCMP extends BaseCMP {
	private static final long serialVersionUID = 4955545274817517787L;

	public ForumRolleCMP() {
		super(ForumRolleCMP.class);
	}
	@XD5EjbCreateMethod(descr="Erstellt einen Eintrag f\u00fcr einen User f\u00fcr das angeg. Forum")
	public Object ejbCreate(String username, long forumId, UserRolle rolle) throws CreateException {
		setId(SQLSupport.erzeugeIDvonDate(new Date()));
		ForumLocalHome fh=holeLocalJndiObjekt("ejb/Forum", ForumLocalHome.class);
		UserLocalHome uh=holeLocalJndiObjekt("ejb/User", UserLocalHome.class);
		UserRolleLocalHome rh=holeLocalJndiObjekt("ejb/UserRolle", UserRolleLocalHome.class);
		try {
			fh.findByPrimaryKey(forumId);
		} catch (FinderException e) {
			throw new CreateException("Kein Forum mit der ID "+forumId+" gefunden!");
		}
		try {
			uh.findByPrimaryKey(username);
		} catch (FinderException e) {
			throw new CreateException("Kein Nutzer mit Username \""+username+"\" gefunden!");
		}
		try {
			rh.findByPrimaryKey(rolle.getRollenLevel());
		} catch (FinderException e) {
			throw new CreateException("Kein Rollenlevel mit ID "+rolle.getRollenLevel()+" bekannt!");
		}
		return null;
	}
	public void ejbPostCreate(String username, long forumId, UserRolle rolle) {
		ForumLocalHome fh=holeLocalJndiObjekt("ejb/Forum", ForumLocalHome.class);
		UserLocalHome uh=holeLocalJndiObjekt("ejb/User", UserLocalHome.class);
		UserRolleLocalHome rh=holeLocalJndiObjekt("ejb/UserRolle", UserRolleLocalHome.class);
		try {
			UserLocal user=uh.findByPrimaryKey(username);
			ForumLocal forum=fh.findByPrimaryKey(forumId);
			UserRolleLocal r=rh.findByPrimaryKey(rolle.getRollenLevel());
			setUser(user);
			setForum(forum);
			setUserRolle(r);
		} catch (FinderException e) {
			schreibeException(e);
		}
	}
	@XD5EntityFieldMethod(descr = "die ID des Eintrags",gen=GenerierungsPolicy.NurGet)
	public abstract Long getId();
	public abstract void setId(Long l);
	@XD5EntityRelation(descr = "Der User des Forums", relName = "relForumrolleUser", spaltenName = "username", zielBeanName = "User",gen=GenerierungsPolicy.NurGet)
	public abstract UserLocal getUser();
	public abstract void setUser(UserLocal l);
	@XD5EntityRelation(descr = "Das Forum", relName = "relForumrolleForum", spaltenName = "forum", zielBeanName = "Forum",gen=GenerierungsPolicy.NurGet)
	public abstract ForumLocal getForum();
	public abstract void setForum(ForumLocal f);
	@XD5EntityRelation(descr = "die Rolle des Users", relName = "relForumrolleUserrolle", spaltenName = "rolle", zielBeanName = "UserRolle")
	public abstract UserRolleLocal getUserRolle();
	public abstract void setUserRolle(UserRolleLocal r);
	@XD5EjbInterfaceMethod(descr="Setzt das Recht des Users in dem Forum")
	public void setRolle(UserRolle r) throws FinderException {
		UserRolleLocalHome rh=holeLocalJndiObjekt("ejb/UserRolle", UserRolleLocalHome.class);
		UserRolleLocal rolle=rh.findByPrimaryKey(r.getRollenLevel());
		setUserRolle(rolle);
	}
	@XD5EjbInterfaceMethod(descr="Liefert die Rolle des Users im Forum zur\u00fcck")
	public UserRolle getRolle() {
		UserRolleLocal rolle=getUserRolle();
		for(UserRolle r : UserRolle.values()){
			if(r.getRollenLevel()==rolle.getId()) {
				return r;
			}
		}
		return null;
	}
	@XD5EjbSelectMethode(descr = "returns all users with the appropriate role", ejbQL = "select distinct r.user from ForumRolle r where r.userRolle.id=?1 order by r.user.username asc")
	public abstract Collection<UserLocal> ejbSelectForumsuserMitRolle(int roleId);

}
/**
 * 
 */
package ds2.forum.ejb;

import java.sql.Timestamp;
import java.util.Date;

import javax.ejb.CreateException;
import javax.ejb.FinderException;

import ds2.forum.ejbIF.UserLocal;
import ds2.forum.ejbIF.UserLocalHome;
import ds2.forum.ejbIF.local.Thread;
import ds2.forum.ejbIF.local.ThreadHome;

import xd5.annotations.GenerierungsPolicy;
import xd5.annotations.XD5Param;
import xd5.annotations.ejb.XD5EjbCreateMethod;
import xd5.annotations.ejb.XD5EjbHomeMethod;
import xd5.annotations.ejb.XD5EjbInterfaceMethod;
import xd5.annotations.ejb.XD5EjbInternalRef;
import xd5.annotations.ejb.XD5EjbSelectMethode;
import xd5.annotations.ejb.XD5EntityEjb;
import xd5.annotations.ejb.XD5EntityFieldMethod;
import xd5.annotations.ejb.XD5EntityRelation;
import xd5.annotations.ejb.XD5QueryMethode;
import xd5.annotations.geronimo.XD5GeronimoEntityEjb;
import kaeto23.j2ee.ejb.BaseCMP;
import kaeto23.mysqldb.SQLSupport;

/**
 * @author kaeto23
 *
 */
@XD5EntityEjb(descr = "Die Beitr\u0ee4ge zu einem Thread", name = "Post", primKeyFeld = "id", tabName = "eintrag",
localHomeIF="ds2.forum.ejbIF.local.PostHome",localIF="ds2.forum.ejbIF.local.Post",ejbLocalRefs= {
	@XD5EjbInternalRef(ejbName = "Thread", jndiName = "ejb/Thread"),
	@XD5EjbInternalRef(ejbName = "User", jndiName = "ejb/User")
},finderMethoden= {
	@XD5QueryMethode(descr = "findet alle Posts des angeg. Threads", ejbQL = "select distinct object(p) from Post as p where p.thread.id=?1 order by p.erstellt asc", name = "findAllVonThread",params= [EMAIL PROTECTED](name = "threadId",fqKlasse="long",descr="Die ID des Threads")}),
	@XD5QueryMethode(descr = "findet alle Posts eines Forums", ejbQL = "select distinct object(p) from Post as p where p.thread.forum.id=?1 order by p.thread.erstellt desc,p.erstellt asc", name = "findAllVonForum",params= [EMAIL PROTECTED](name = "forumId",fqKlasse="long",descr="Die ID des Forums")})
})
@XD5GeronimoEntityEjb(localJndiName="ejb/ds2/forum/Post")
public abstract class PostCMP extends BaseCMP {
	private static final long serialVersionUID = -2562692495710298146L;

	public PostCMP() {
		super(PostCMP.class);
	}
	@XD5EjbCreateMethod(descr="Erstellt einen Eintrag")
	public Object ejbCreate(long threadId, String inhalt, String username) throws CreateException{
		if(inhalt==null||inhalt.length()<=0) {
			throw new CreateException("Kein Inhalt angegeben!");
		}
		Date jetzt=new Date();
		setId(SQLSupport.erzeugeIDvonDate(jetzt));
		setErstellt(SQLSupport.holeGMTTime(jetzt));
		setEditiert(null);
		setInhalt(inhalt);
		ThreadHome th=holeLocalJndiObjekt("ejb/Thread", ThreadHome.class);
		UserLocalHome uh=holeLocalJndiObjekt("ejb/User", UserLocalHome.class);
		try {
			th.findByPrimaryKey(threadId);
		} catch (FinderException e) {
			throw new CreateException("Kein Thread mit der ID "+threadId+" gefunden!");
		}
		try {
			uh.findByPrimaryKey(username);
		} catch (FinderException e) {
			throw new CreateException("Kein Nutzer mit der ID \""+username+"\" gefunden!");
		}
		return null;
	}
	public void ejbPostCreate(long threadId, String i, String username) {
		ThreadHome th=holeLocalJndiObjekt("ejb/Thread", ThreadHome.class);
		UserLocalHome uh=holeLocalJndiObjekt("ejb/User", UserLocalHome.class);
		try {
			Thread thread=th.findByPrimaryKey(threadId);
			UserLocal user=uh.findByPrimaryKey(username);
			setThread(thread);
			setAutor(user);
		} catch (FinderException e) {
			schreibeException(e);
		}
	}
	@XD5EntityFieldMethod(descr="Die ID des Eintrags",gen=GenerierungsPolicy.NurGet)
	public abstract Long getId();
	public abstract void setId(Long l);
	@XD5EntityFieldMethod(descr="Der textuelle Inhalt")
	public abstract String getInhalt();
	public abstract void setInhalt(String s);
	@XD5EntityFieldMethod(descr="Das GMT-Erstelldatum",gen=GenerierungsPolicy.KeinMapping)
	public abstract Timestamp getErstellt();
	public abstract void setErstellt(Timestamp ts);
	@XD5EntityFieldMethod(descr="Das GMT-Editierdatum",gen=GenerierungsPolicy.KeinMapping)
	public abstract Timestamp getEditiert();
	public abstract void setEditiert(Timestamp ts);
	@XD5EntityRelation(descr = "Der Thread, zu dem dieser Post hier gehoert", relName = "relPostThread", spaltenName = "thread", zielBeanName = "Thread",gen=GenerierungsPolicy.NurGet)
	public abstract Thread getThread();
	public abstract void setThread(Thread t);
	@XD5EntityRelation(descr="Der Autor dieses Postings", relName = "relPostUser", spaltenName = "autor", zielBeanName = "User",gen=GenerierungsPolicy.NurGet)
	public abstract UserLocal getAutor();
	public abstract void setAutor(UserLocal l);
	@XD5EjbInterfaceMethod(descr="Liefert das Erstelldatum zur\u00fcck")
	public Date getErstelltDate() {
		return SQLSupport.erstelleDate(getErstellt());
	}
	@XD5EjbInterfaceMethod(descr="Liefert das Editierdatum zur\u00fcck, oder null")
	public Date getEditiertDate() {
		if(getEditiert()==null) {
			return null;
		}
		return SQLSupport.erstelleDate(getEditiert());
	}
	@XD5EjbSelectMethode(descr = "returns the number of postings from a forum", ejbQL = "select distinct count(p.id) from Post as p where p.thread.forum.id=?1")
	public abstract long ejbSelectForumPostsNum(long forumId) throws FinderException;
	@XD5EjbHomeMethod(descr = "Liefert die Anzahl Forumposts zur\u00fcck")
	public long ejbHomeGetForumPostsNum(long forumId){
		try {
			return ejbSelectForumPostsNum(forumId);
		} catch (FinderException e) {
			schreibeException(e);
		}
		return 0;
	}
}

Reply via email to