wood burning stoves 2.0*
The moose likes Architect Certification (SCEA/OCMJEA) and the fly likes ejbCreate's return value(Entity Bean) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Architect Certification (SCEA/OCMJEA)
Bookmark "ejbCreate Watch "ejbCreate New topic
Author

ejbCreate's return value(Entity Bean)

Yi Si
Ranch Hand

Joined: Nov 12, 2003
Posts: 54
Hi all!

About ejbCreate methods in Entity Bean, I read "NULL value of type BeanPK in CMP and Primary Key in BMP." in some matrials.

If I understand correctly, in CMP ejbCreate should always return null.

Can anyboy take a detail of why should always return null?

Thanks a lot


Sun(java)-SCJA SCJP SCWCD SCBCD SCDJWS SCEA<br />Sun(solaris)-SCSA SCSN<br />IBM-486 Object-Oriented Analysis and Design with UML Test <br />IBM Certified System Administrator - WebSphere Application Server Network Deployment V6.0<br />Oracle-OCA
B.Sathish
Ranch Hand

Joined: Aug 18, 2005
Posts: 372
Very good question, here's my understanding.

In the case of CMP, you have a set of container-managed CMP fields. In ejbCreate() method, you populate those fields and generate the primary key and populate the CMP PK field with the generated value. Once you complete the method, the container looks into the CMP fields to get the values that you have populated and does the insert into the persistent store using those values. Now apart from just inserting the data in the persistent store, the container has to do a lot of other things for which it needs the primary key. For example, it has to create an EJB Object instance so that its stub can be returned to the client. But to create the EJB Object for the newly created entity, it needs the PK of the entity. Because EJBObject is something specific to each entity in the persistent store. Or you could say that EJBObject has a primaryKey property as it has a getPrimaryKey() method. Hence the container needs the PK to create the EJBObject. It also has to update the EntityContext of the bean instance representing the entity with the PK. (EntityContext also has a getPrimaryKey method). Now in CMP, the container can get the PK, just by looking at the CMP field value that you populated in the ejbCreate () method right? Hence there is no need for you to return the PK explicitly from the ejbCreate() method.

Now lets take the case of BMP. You do the insert in ejbCreate() , but the container still needs the primary key as before to create the EJBObject and update the EntityContext of the bean. How will the container know the PK? There are no container managed fields to look into. So the container will not know unless you tell it. Hence you have to return the PK to the container from the ejbCreate() method so that the container can go ahead and use it in creating the EJBObject etc

Hence ideally, for CMP, it should have been public void ejbCreate() and for BMP, public PK ejbCreate(). But the spec developers did not want to introduce separate interfaces for BMP and CMP. Even the EntityBean interface that both CMP and BMP classes implement is the same. Hence for consistency sake, they kept the method signature the same (both return PK), but you have to return null from the ejbCreate() method. I think even if you return the PK from ejbCreate() method of a CMP bean, the container will ignore it and always look into the CMP PK field to get the PK and do its job.

Makes sense?
Yi Si
Ranch Hand

Joined: Nov 12, 2003
Posts: 54
I got it
Thanx for the excellent explanation !!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ejbCreate's return value(Entity Bean)