• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

ejbCreate's return value(Entity Bean)

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 372
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got it
Thanx for the excellent explanation !!
 
Message for you sir! I think it is a tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic