File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Entity EJB accessing Database without primary key

 
Kathy OConnell
Greenhorn
Posts: 29
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Thanks
Kathy
 
Dave Landers
Ranch Hand
Posts: 401
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 theserverside.com.
 
abhi zulu
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author
Ranch Hand
Posts: 3892
5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic