Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Unknown PK Problem

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

How should I use unknown PK in EJB QL?

Regards,

JML
 
author & internet detective
Posts: 40035
809
Eclipse IDE VI Editor Java
  • 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
  • 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
Posts: 40035
809
Eclipse IDE VI Editor Java
  • 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
  • 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
  • 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
Posts: 40035
809
Eclipse IDE VI Editor Java
  • 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
  • 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
Posts: 40035
809
Eclipse IDE VI Editor Java
  • 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.
 
look! it's a bird! it's a plane! It's .... a teeny tiny ad
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic