Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why return null from entity bean ejbCreate()

 
Keith Rosenfield
Ranch Hand
Posts: 277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On page 331 of HF there is a note on the bottom of the page that says "return null?? What's up with that?" I couldn't find the answer in the book so "what IS up with that?" Why do we return null from ejbCreate()?
Thanks in advance.
 
Goan Balchao
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because, after the bean has been created, it is the responsibility of the container to return the primary key instance associated with the entity.
Also sometimes the priimary key is not know (unknown primary key) - so you would not always be able to return the primary key class. That;s the reason I guess, the spec says that null has to be returned, so that the Container can make the decision as to what (primary key class) is to be returned from ejbCreate
Hope that helps
 
Prasad Kuppa
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the answer is :
This is for CMP beans. as the container is managing the persistence no need to return anything. But, for BMP Beans container has to make sure that the ejbCreate() is completed successfully by getting the PrimaryKey class. ( BMP beans are out of scope for the exam.)
To make it same signature of ejbCreate() for both BMP and CMP beans, it is specified to return null for CMP entity beans.
-Prasad
 
Kathy Sierra
Cowgirl and Author
Rancher
Posts: 1589
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy,
Yes, Prasad has the right answer... the *original* idea was that you could take a BMP bean, and subclass it to be a CMP bean, so the ejbCreate() method had to be the same... I think they still explain it that way in the 2.0 spec. Keith, I think that's kind of funny (in a sad way) that I actually did say "What's up with that?" and then never explained it. It was supposed to be in a Q & A Sorry!
cheers,
Kathy
 
Vishwa Kumba
Ranch Hand
Posts: 1066
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok...In CMP2.0 Entity bean, the spec says, we return null, so we return null.
But what happens if I return the primary key object itself. In most cases, I think I have access to the primary key object in ejbCreate() method.
example :
public String ejbCreate(String key) {
setID(key);
//return null;
return key;
}
Any comments?
One of the CMP examples in Ed Roman's book has similar code.
I wonder if it is right. (7.6, p:190 and p:191 of the pdf file, Chapter 7(CMP))
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic