aspose file tools*
The moose likes EJB and other Java EE Technologies and the fly likes Unknown PK Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Unknown PK Problem" Watch "Unknown PK Problem" New topic
Author

Unknown PK Problem

Jamal Hasanov
Ranch Hand

Joined: Jan 08, 2002
Posts: 411
Hi, ranchers

How should I use unknown PK in EJB QL?

Regards,

JML
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 31105
    
165

Jamal,
I don't understand. How is the PK unknown? Doesn't the entity bean have to know which is the PK?


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Jamal Hasanov
Ranch Hand

Joined: Jan 08, 2002
Posts: 411
If you have table USERS with fields:
USER_ID INTEGER AUTO INCREMENT,
USER_NAME VARCHAR(20)
STATUS VARCHAR(1)

and you create your CMP EntityBean with 2 fields. name and status. USER_ID field will be managed (auto incremented) by database. That's called UNKNOWN PRIMARY KEY. If you want to get primary key of your bean, you can use getPrimaryKey() method.

My question is how to get primary key in EJB-QL?

Is it clear now?

JML
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 31105
    
165

Jamal,
Yes! I should have been able to figure that out from the initial post.

You can do a query on the two fields you know. If it returns more than one result, pick the one with the highest key number. Databases assign keys in order, so this system works unless you insert a lot of the same record simultaneously.
Jamal Hasanov
Ranch Hand

Joined: Jan 08, 2002
Posts: 411
No, you didn't understand the problem.

If I create another CompanyBean with fields:
Integer compID
String compName
UserBean users


and I want to get companyName where the user with ID 103 works.

EJB-QL:
=======================================
select object(o)
from CompanyBean as o
where o.users.idontknow = ?1
=======================================
Jamal Hasanov
Ranch Hand

Joined: Jan 08, 2002
Posts: 411
The questtion : What should be instead of idontknow?

JML
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 31105
    
165



This seems too simple to be the answer to what you are asking though.
[ March 17, 2005: Message edited by: Jeanne Boyarsky ]
Jamal Hasanov
Ranch Hand

Joined: Jan 08, 2002
Posts: 411
:-)))))

But I have no userID field in my UsersBean. I've defined it as a UNKNOWN PK. It exists only in table, not in bean!

Pls, read about Unknown primary keys in EJB specification...
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 31105
    
165

Jamal,
I do understand what unknown PKs are from JDBC. (I was a bit confused earlier in the post.) The idea is the same in JDBC and EJB.

The following comment still holds:
Originally posted by Jeanne Boyarsky:
You can do a query on the two fields you know. If it returns more than one result, pick the one with the highest key number. Databases assign keys in order, so this system works unless you insert a lot of the same record simultaneously.


You can do the query EJB-QL. If you get multiple results, you can go through them in Java.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Unknown PK Problem