O'Reilly logo

Head First EJB by Bert Bates, Kathy Sierra

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Bean things you can do during entity construction:

image with no caption
image with no caption

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)

image with no caption
  1. Client calls create(“Billy Bob”) on the home stub

  2. The create(“Billy Bob”) method invocation is passed to the home object.

  3. A bean is pulled out of the pool to do the creation

  4. Container ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required