Hi, David.

I'm spinning my wheels with a WebLogic 7 issue, and their searching
facilities are sorely lacking, so it's nice to have a diversion that I
can actually be successful with. :)

Let me summarize the outcome:

Correct is:

java.util.Collection findUserByPhoneId(PhoneLocal phone)
SELECT OBJECT(u) FROM User AS u, IN(u.phones) AS p WHERE p = ?1


another possibility is:

java.util.Collection findUserByPhoneId(PhoneLocal phone)
   SELECT OBJECT(u) FROM Phone AS p, User AS u WHERE p.user = u AND p = ?1

True?

My problem was that I wanted instead of the second version the WRONG thing:

java.util.Collection findUserByPhoneId(PhoneLocal phone)
   SELECT OBJECT(u) FROM User AS u WHERE ?1.user = u


Which of course is excluded by the syntax. The trick is thus, that for every argument A a,
you need to have an "A as a0" in the SELECT part, and an "a0 = ?1" in the WHERE part.

Is that closer?


Thats perfect.

For the application I have to write this week, I will have the following query
(just for your viewing pleasure ;-)

java.util.Collection findAffinityData(UserEntityLocal u)

SELECT OBJECT ad
FROM
userEntity AS u1,
virtualRoom AS v,
IN(v.memberUsers) AS u2,
IN(u2.affinityDataHoldByUser) AS ad
WHERE
u1 = ?1 AND u1.virtualRoom = v
AND u2 <> ?1
AND ad.describedUser = u1


All the best for your WebLogic issue! Here its 11pm, and I am happy, I have solved my problem.

Best, Philipp

PS: Thanks to Matthew Excell. Indeed, I used the getter instead of the field name.
I had a look on www.ejb-ql.com and will past the outcome of this discussion
there.

David Harkness
Sony Pictures Digital Networks
(310) 482-4756


-----Original Message-----
From: Philipp W. Kutter [mailto:[EMAIL PROTECTED] Sent: Tuesday, August 05, 2003 11:55 AM
To: [EMAIL PROTECTED]
Subject: Re: [Xdoclet-user] Using CMR field with EJBQL


Hi, David.

Thanks for the prompt answer!



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.]




Yes, and one user for any given phone, right?



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.




This is exactly the kind of example I got working. I even got it working

using the PhoneLocal directly,
I mean:

java.util.Collection findPhonesByUser(UserLocal user)
 SELECT OBJECT(p) FROM User AS u, IN(u.phones) AS p WHERE u = ?1

This is working in my examples. Is there any reason to go for the .name field, instead of
using the UserLocal directly?

BUT BACK TO MY QUESTION:
I was expressing my question wron. Sorry about that.
I want findUserByPhone, not phonesByUser, as you give me.

I want:

java.util.Collection findUserByPhone(PhoneLocal phone)
 SELECT OBJECT(u) FROM User AS u WHERE u.getPhone = ?1


Or, if there is a reason I cannot go over PhoneLocal directly, I want:

java.util.Collection findUserByPhoneId(String phoneId)
 SELECT OBJECT(u) FROM User AS u WHERE u.getPhone.getId = ?1


This is what is not working. Why is it not working? How can I do it?

For me it seems like, I cannot do it, since it is not needed to do it. You always can
get the user by phone by applying getPhone outside of the EJBQL, and
thus it is not supported and nobody gives an example.

Is this understanding right?

Best, Philipp



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_0
1/01
_______________________________________________
xdoclet-user mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/xdoclet-user







-------------------------------------------------------
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




Attachment: smime.p7s
Description: S/MIME Cryptographic Signature

Reply via email to