File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes EJB and other Java EE Technologies and the fly likes Entity EJB accessing Database without primary key 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 "Entity EJB accessing Database without primary key" Watch "Entity EJB accessing Database without primary key" New topic

Entity EJB accessing Database without primary key

Kathy OConnell

Joined: Dec 03, 2001
Posts: 29
I am fairly new to EJBs
Is it possible to use EJBs to retrieve data from a database (eg db2) when you don't have the primary key. From what I can see, to create a remote interface you always need to have the primary key.
I want to access the database using a non-primary key and return 2 or more results that the session bean can handle and manipulate. Is it possible to do this and if so, could you tell me how.
I would really appreciate some help here
Dave Landers
Ranch Hand

Joined: Jul 24, 2002
Posts: 401
You can write finders for anything. The findByPrimaryKey is required, but you can also write findByCity(String city) and findAllWetStinkyDogs() or whatever.
They just have to be named "find<whatever>".
Finders may return a single bean or a Collection of them.
You just define what you want in the home interface and provide an appropriate implementation. If you are using CMP (Container Managed Persistence), this means you supply the appropriate EJB-QL in your deployment descriptor.
If you are writing BMP (Bean managed), then you author a method called "ejbFind<whatever>" that takes the same arguments as the one in your home. The only difference is that this one returns a PK or a Collection of PK objects.
So that's the two-cent overview.
Grab a good EJB book - I like the one by Richard Monson-Haefel from OReilly. Ed Rowman's is pretty good too, and you can download a copy from
abhi zulu

Joined: May 18, 2002
Posts: 14
hey ,
we cannot find a unique entity bean instance without defining a primarykey.
If we do not have a findbyprimarykey in my home Interface the creation of jar will fail.
Kyle Brown
Ranch Hand

Joined: Aug 10, 2001
Posts: 3892
You MUST define a find by primary key, but you can also define custom finders. The findByPrimaryKey should only ever return one instance (that is why it's called find by primary key -- in relational datbase terminology there must ALWAYS be some combination of columns that uniquely identifies a row). However a custom finder may return a collection of objects, since it searches on something other than the primary key. Again READ A GOOD EJB BOOK. It will help.

Kyle Brown, Author of Persistence in the Enterprise and Enterprise Java Programming with IBM Websphere, 2nd Edition
See my homepage at for other WebSphere information.
I agree. Here's the link:
subject: Entity EJB accessing Database without primary key
It's not a secret anymore!