Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unknown PK Problem

 
Jamal Hasanov
Ranch Hand
Posts: 411
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, ranchers

How should I use unknown PK in EJB QL?

Regards,

JML
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 33696
316
Eclipse IDE Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jamal,
I don't understand. How is the PK unknown? Doesn't the entity bean have to know which is the PK?
 
Jamal Hasanov
Ranch Hand
Posts: 411
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 33696
316
Eclipse IDE Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 411
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 411
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The questtion : What should be instead of idontknow?

JML
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 33696
316
Eclipse IDE Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 411
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
:-)))))

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
Posts: 33696
316
Eclipse IDE Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic