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

EJb 2.1 Question  RSS feed

 
Duran Harris
Ranch Hand
Posts: 608
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm busy looking at a n example from Mastering EJB 3rd edition(pg 168)and I really don't quite get how this ejbFindByPrimaryKey(AccountPK key) manages to return any results.In the method a statement is prepared from parsing a field out of the 'key' parameter.


ie.
PreparedStatement p = conn.prepareStatement("Select id from accounts where id=?");
p.setString(1,key.toString());
ResultSet rs=p.executeQuery();
rs.next();
return key;

The key that is returned is the same key used in the parameter to the method....so what does this method actually accomplish?
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 37230
519
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It throws an exception if an entity bean isn't found with that id. A useful piece of information if you need to know whether such a bean exists.
 
Duran Harris
Ranch Hand
Posts: 608
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It throws a NullPointerException?Is this the usual way of implementing a findByPrimaryKey method?But wouldn't this way mess around with client code...
Why not just check if the resultset is null and if it is then return null?

Then client code could do this:

if(AccountHomeObj.findByPrimary(key)!=null)//do something
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 37230
519
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Duran Harris wrote:It throws a NullPointerException?

No. A result set isn't null. Just empty. rs.next() returns false. Which come to think of it doesn't throw the FinderException I was thinking of.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!