Bean things you can do during entity construction:
What to put in the constructor
We know it’s painfully obvious by now... NOTHING.
Unless you’re forced to, don’t even put a constructor in your code at all, and just use the compiler-generated default constructor.
Whatever you do, be SURE you have a public no-arg constructor in your class!
public Customer() { }
What to put in the setEntityContext() method
Assign the context to an instance variable. Remember, you get only ONE chance to save it. You might not always need to use a session context, but you’ll probably need an entity context. Besides, your context is gonna stay alive whether you keep a reference to it or not, so the only memory you save if you don’t keep it is for the reference variable, not the object itself.
public void setEntityContext(EntityContext ctx) { context = ctx; }
CMP Entity bean creation
Scenario: client wants to create a new entity in the database (remember, the Container made the bean instance and the context earlier, and put them in the pool)
Client calls create(“Billy Bob”) on the home stub
The create(“Billy Bob”) method invocation is passed to the home object.
A bean is pulled out of the pool to do the creation
Container ...
Get Head First EJB now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.