This week's book giveaway is in the Cloud/Virtualization forum. We're giving away four copies of Mastering Corda: Blockchain for Java Developers and have Jamiel Sheikh on-line! See this thread for details.
In the place where I work, we use EJB, although I'm studying these properly now for the certification. We currently have related tables (let's say a Movie-Director) relationship. Because I didn't know about ejbPostCreate I never set the CMR field in this method. However I can ensure that, having declared the relationship between Movies and Director, if the client uses a CMR abstract method on the Director entity bean class, a collection of movies is returned (I can ensure it because, as I said, we're using in at work). So my question is: why shall I invoke the CMR setter method in the ejbPostCreate? Is this necessary? If so, how does it come that I can get all the related fields for an entity bean, even if I don't invoke the CMR setter in the ejbPostCreate()?
However I can ensure that, having declared the relationship between Movies and Director, if the client uses a CMR abstract method on the Director entity bean class, a collection of movies is returned (I can ensure it because, as I said, we're using in at work). If you are calling an abstract method on the entity bean, it means that the entity bean already exists and thus that the relationships have been set already at the time the bean was created. Actually, ejbCreate/ejbPostCreate are called only when the entity bean is created the first time in the DB. When the bean is already in a table somewhere, you usually get them using finder or select methods which will retrieve existing entity beans from the DB. Then the container will call ejbActivate on it to send it to the READY state and then ejbLoad to populate the fields.
If you create yourself new beans you are not required to set the relationship fields in the ejbPostCreate method. You can also invoke create() on the home which will trigger ejbCreate/ejbPostCreate on the bean class and then use the setter methods for setting relationship fields as normal. The requirement is not to set relationship fields in the ejbCreate method because the identity of the bean is not yet known. But after create() has been invoked you may set the relationships at any time but before the relationship field is actually used.
I hope this makes sense to you. [ July 19, 2004: Message edited by: Valentin Crettaz ]