aspose file tools*
The moose likes EJB and other Java EE Technologies and the fly likes About ejbPostCreate Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "About ejbPostCreate" Watch "About ejbPostCreate" New topic
Author

About ejbPostCreate

Sujittt Tripathyrr
Ranch Hand

Joined: Jun 21, 2006
Posts: 96
Why there is a ejbPostCreate method for every ejbCreate method.
Vinay Raj
Ranch Hand

Joined: Jun 19, 2006
Posts: 81
The best example for need of a ejbPostCreate method is an entity bean. ejbCreate is the process of inserting a record/row in the database. The primary key generation could be left to the database, hence on completion of the ejbCreate, the entity bean may not be aware of the primary key. The primary key is available to the bean during ejbPostCreate. Typically it uses the ejbPostCreate method to initialize/create any local relationships.
Sujittt Tripathyrr
Ranch Hand

Joined: Jun 21, 2006
Posts: 96
Thanks vinay for Reply

Is that the only requirement of ejbPostCreate or any other else.Can we write any code in the ejbPostCreate method.

Thanks
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968

Only in the ejbPostCreate do we know that the database object was REALLY created. An exception can happen at any time during ejbCreate, and the database instance might not be created. As a result, it's not until the ejbPostCreate that we REALLY KNOW a primary key has been initialized, and often, the ejbPostCreate method is used to work with the primary key.

Of course, the primary key is a manifestation of the fact that the entity now really exists. You can do anythign you want in the ejbPostCreate. From what I've seen, quite often logic that should be in the ejbPostCreate is placed in the create method, just because programmers don't know any better, and also because problems in the create method are somewhat rare.
Jeroen T Wenting
Ranch Hand

Joined: Apr 21, 2006
Posts: 1847
begs the question of what happens to the primary key field if you set it in ejbCreate (say by calling a generator on the database or some other service to create it) and the insert fails.
Will the PK field have been reset to null when ejbPostCreate is reached?
How else would insertion failure be specified, assuming that's what you want to detect in ejbPostCreate, if you get there at all (I'd expect a CreateException to be thrown by the appserver if that happened in fact, and ejbPostCreate to be skipped).


42
Vinay Raj
Ranch Hand

Joined: Jun 19, 2006
Posts: 81
In case of failure of database insertion, I expect a CreateException to be thrown and ejbPostCreate to be skipped.

The idea of having ejbPostCreate is to ensure that the entity is successfully created and all other dependants like relationships can be initialized properly.
Jeroen T Wenting
Ranch Hand

Joined: Apr 21, 2006
Posts: 1847
you mean that the data that actually was inserted is what should have been I guess?
Makes sense, though what would cause an actual insertion to insert the wrong data I wouldn't know, seems rather unlikely to happen (unless maybe you use BMP and made a programming error in the insertion code).
Vinay Raj
Ranch Hand

Joined: Jun 19, 2006
Posts: 81
The only possible scenarios for database insert failures that I can think of are duplicate primary keys or in case database is unavailable. As regards the primary key, the primary key is generated using a generator would be lost. Now the interesting part is whether the entity bean instance is discarded by the container? From whatever I have read it will not be discarded as CreateExceptions are not system exceptions. Which means the client application needs to do it on its own.

Any thoughts?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: About ejbPostCreate
 
Similar Threads
need ejbpostcreate method for each create method in the bean
CMR save problem
is ejbPostCreate() method is only for entitiy beans?
EJB:: ejbPostCreate()
ejbPostCreate