Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

About ejbPostCreate

 
Sujittt Tripathyrr
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why there is a ejbPostCreate method for every ejbCreate method.
 
Vinay Raj
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1847
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Vinay Raj
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1847
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic