'lo Philipp, >2. Phone and User have a 1:N relationship. Each P has a U, and thus a U
> also has many Ps that consider them as their U. > >Now I would like to make a finder method on phones, where I select those >(the single one in fact) having a certain user: [One correction: you'll get many phones for any given user as it is U 1:N P.] You cannot search directly on the CMR field like that, but you're close. You need to create what would effectively be a join in SQL by searching the User bean and relating it to its Phones. There are many ways to do this, and some may be easier, but let's continue with your example. I'm adapting the following EJB-QL from a different set of beans, but here's a first stab at it. The following finds the Phones related to the user (or users) with the given name: java.util.Collection findPhonesByUserName(java.lang.String name) SELECT OBJECT(p) FROM User AS u, IN(u.phones) AS p WHERE u.name = ?1 "phones" is the CMR Collection field in User. "name" is a CMP field in User. In SQL, this would be select p.* from phone p, user u where p.user_id = u.id and u.name = '<value>' If you were searching by user.id instead of user.name (as you would be given your example where you must have found a single User first), you could simply use UserLocal user = UserLocalHome.findByPrimaryKey(id); Collection phones = user.getPhones(); David Harkness Sony Pictures Digital Networks (310) 482-4756 -----Original Message----- From: Philipp W. Kutter [mailto:[EMAIL PROTECTED] Sent: Tuesday, August 05, 2003 11:05 AM To: [EMAIL PROTECTED] Subject: [Xdoclet-user] Using CMR field with EJBQL Hi, David. I had a second look on your answer, and I think I can formulate my problem based on the code proposed by you: I would like to know why * @ejb.finder * signature = "java.util.Collection findPhoneOfUser(UserLocal user)" * query = "SELECT OBJECT(p) FROM phone p WHERE p.getUser = ?1 " is not working. In Detail: >1. User and Phone are entity beans. >2. Phone and User have a 1:N relationship. Each P has a U, and thus a U >also has > many Ps that consider them as their U. >3. The User table has a PK column called id. >4. The Phone table has a FK column called user. > >If those are all correct, then you need to change your xdoclet tags to >specify a relationship instead of a field. For example, for User <-- >1:N >--> Phones, here are the tags I use in UserEJB.java: > > /** > * @ejb.interface-method > * @ejb.transaction type="Supports" > * @ejb.relation > * name="User-Phone" > * role-name="User-has-many-Phones" > * cascade-delete="no" > */ > public abstract Collection getPhones ( ) ; > public abstract void setPhones ( Collection phones ) ; > >And in PhoneEJB.java: > > /** > * @ejb.interface-method > * view-type="local" > * @ejb.transaction type="Supports" > * @ejb.relation > * name="User-Phone" > * role-name="Phones-have-a-User" > * cascade-delete="yes" > * @weblogic.column-map > * foreign-key-column="user_id" > * key-column="id" > */ > public abstract UserLocal getUser ( ) ; > > /** > */ > public abstract void setUser ( UserLocal user ) ; > >This is for a bidirectional relationship, so both beans know about and >can find each other via relationships and in finder queries. I also >added a helper method so I could hook a Phone up to a User given only >the User's ID: > > /** > * Attaches this phone to the user with the given ID. > * > * @param userId unique ID of the owning user > */ > protected void setLocalUserId ( Integer userId ) > { > try { > setUser(UserUtil.getLocalHome().findByPrimaryKey(userId)); > } > catch ( NamingException e ) { > throw new ServiceException("Failure looking up user home", e); > } > catch ( FinderException e ) { > throw new ServiceException("Failure finding a user", e); > } > } > >First of all, is this where you were going with this? > Yes, this is what I am going to, adapted to your Phone/User example. Now I would like to make a finder method on phones, where I select those (the single one in fact) having a certain user: * @ejb.finder * signature = "java.util.Collection findPhoneOfUser(UserLocal user)" * query = "SELECT OBJECT(p) FROM phone p WHERE p.getUser = ?1 " This is not working in my examples. Why? Best, and thanks again, Philipp ------------------------------------------------------- This SF.Net email sponsored by: Free pre-built ASP.NET sites including Data Reports, E-commerce, Portals, and Forums are available now. Download today and enter to win an XBOX or Visual Studio .NET. http://aspnet.click-url.com/go/psa00100003ave/direct;at.aspnet_072303_01/01 _______________________________________________ xdoclet-user mailing list [EMAIL PROTECTED] https://lists.sourceforge.net/lists/listinfo/xdoclet-user
