Entity Beans - ejbPostCreate

J

Jayaram

Hi
I would like to know what is the lifecycle of an entity bean assuming
it was found (by a finder method - ejbFindBy...) rather than being
created (ejbCreate(...)) ?
Is the ejbPostCreate(...) method called on such beans OR is it simply
a call to ejbActivate() followed by ejbLoad() ?
Isn't it a necessity that ejbPostCreate(...) be caled on every entity
bean despite the fact that they were found rather than created ?
Would appreciate if someone could explain the real intent of the
ejbPostCreate(...) method ?
Regards,
Jayaram
 
U

Usman Saleem

Jayaram,

Entity bean life cycle can be divided into three states 1. Does not
exists 2. Pooled and 3. Ready

As far as your question is concerned, the transition will be from
Pooled state to Ready state.

There are two type of transitions that can occur from pooled state to
ready state

Pool -> ejbCreate() -> ejbPostCreate() -> Ready
Pool -> ejbActivate() -> ejbLoad() -> Ready

Using finder (query) methods, the EJBs will follow the "activate"
transition instead of "create" transition from pooled to ready state.
Isn't it a necessity that ejbPostCreate(...) be caled on every entity
bean despite the fact that they were found rather than created ?

No, it is not necessary.
real intent of the ejbPostCreate(...) method ?
In very simple words it is for any logic that should be executed
"after" the INSERT statements to the databases has been executed. The
bean primary key (id) is not available in ejbCreate, but it is
available in ejbPostCreate. You can use the id to perform any
functionality before any of the business methods are called. The
primary key is also required to manage any referential integrity
contrainsts (foreign key relations).

Hope it satisfies your query.

Best Regards,

Usman Saleem
Associate Consultant
Fusion Technologies
http://www.fusiontech.com
 
A

Andrea Sansottera

In very simple words it is for any logic that should be executed
"after" the INSERT statements to the databases has been executed. The
bean primary key (id) is not available in ejbCreate, but it is
available in ejbPostCreate. You can use the id to perform any
functionality before any of the business methods are called. The
primary key is also required to manage any referential integrity
contrainsts (foreign key relations).

Correct me if I'm wrong... can't we say:

ejbCreate(...)-->setEntityContext(EntityContext ctx)-->ejbPostCreate(...)

In fact in ejbPostCreate we can access the primaryKey using the entity
context:

public EntityContext ctx;
public void setEntityContext(EntityContext ctx) {
this.ctx = ctx;
}
public void ejbPostCreate() {
debug_stream.println("Primary key: "+this.ctx.getPrimaryKey().toString());
}
 
U

Usman Saleem

Andrea,
Correct me if I'm wrong... can't we say:

ejbCreate(...)-->setEntityContext(EntityContext ctx)-->ejbPostCreate(...)

The correct sequence would be

DOES NOT EXIST-->Class.newInstance()-->setEntityContext()-->POOLED-->ejbCreate()
-->ejbPostCreate()-->READY

where DOES NOT EXIST, POOLED and READY are three different states in
life cycle of entity bean.
In fact in ejbPostCreate we can access the primaryKey using the entity
context:

public EntityContext ctx;
public void setEntityContext(EntityContext ctx) {
this.ctx = ctx;
}
public void ejbPostCreate() {
debug_stream.println("Primary key: "+this.ctx.getPrimaryKey().toString());
}

Yes you are right in your argument. But the point to note here is that
the EntityContext maintained by the bean (for instance, ctx in your
case) does not provide the proper primary key reference until
ejbCreate() method is completed. This implies that in ejbCreate()
method we can not use EntityContext to obtain the primary key refernce
and EJB object reference. However, we can obtain following information
from EjbContext reference in ejbCreate method, such as:
- Information about callers identity (for security purposes)
- Access to EJB home (local, remote) object

Best regards,

Usman Saleem
Associate Consultant
Fusion Technologies
http://www.fusiontech.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top