aspose file tools*
The moose likes EJB and other Java EE Technologies and the fly likes ejbFind() in Entity Bean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "ejbFind() in Entity Bean" Watch "ejbFind() in Entity Bean" New topic
Author

ejbFind() in Entity Bean

Bis Bang
Greenhorn

Joined: Jan 28, 2004
Posts: 15
Hi,
How we able to call ejbFind(primarykey) method from the pool state of an Entity Bean Life Cycle? Do we have any PK available foa a Bean in pool state?? I am not clear about that...Plz help...
Thanx in Advance....


The glory lies not in not falling, but it lies in rising everytime you fall
Seetesh Hindlekar
Ranch Hand

Joined: Feb 13, 2004
Posts: 244
Hi,
Container puts the instance in pool state after it calls the setEntityContext() method.
Container is responsible for calling the ejbCreate method for obtaining the primary key fields of the newly created entity object.
Container must set this PRIMARY KEY before it invokes the ejbPostCreate method.
Now coming back to the ejbFind method,
When Container invokes ejbFind method, the container must pick an instance that is in the pooled state (ie instance not associated with any entity object identity) for the execution.
Its upto the developer to design the ejbFind method to be declared to return either a primary key or a collection of primary keys to return the same to the client.
Is this what ur asking or there is something else to this?
Rgds,
Seetesh
Seetesh Hindlekar
Ranch Hand

Joined: Feb 13, 2004
Posts: 244
Hi,
Illustrated in the code below for a BMP Entity bean.
The primary key is passed to the ejbCreate method and the same is used for ejbFind too.
public String ejbCreate(String accountId, double initialBalance) throws CreateException
{
this.accountId = accountId;
this.balance = initialBalance;
Connection con = null;
PreparedStatement ps = null;
try
{
con = getConnection();
ps = con.prepareStatement("insert into xxx (sampid, sampbal) values (?, ?)");
ps.setString(1, accountId);
ps.setDouble(2, balance);
if (ps.executeUpdate() != 1)
{
String error = "Row error";
log(error);
throw new CreateException (error);
}
return accountId;
}
catch (SQLException sqe)
{
try
{
ejbFindByPrimaryKey(accountId);
}
catch(ObjectNotFoundException onfe) {}
}
xxxxxxxxxxxxxxxxxxxxxxxxxxx
}
public String ejbFindByPrimaryKey(String pk) throws ObjectNotFoundException
{ Connection con = null;
PreparedStatement ps = null;
try
{ con = getConnection();
ps = con.prepareStatement("select sampbal from xxx where sampid = ?");
ps.setString(1, pk);
ps.executeQuery();
ResultSet rs = ps.getResultSet();
if (rs.next())
{ balance = rs.getDouble(1);
}
else
{ throw new xxxxxxxxxxxxxxException (error);
}
}
catch (SQLException sqe) { }
finally {
cleanup(con, ps);
}
return pk;
}
Rgds,
Seetesh
Bis Bang
Greenhorn

Joined: Jan 28, 2004
Posts: 15
hi,
Seetesh, Thanx for ur ans... I mean to say that..how a client able to find an EJB by its primary key in home interface... before the client calls create(args,args)...method...As we know from the lifecycle of entity bean that... once the client calls home.create(aa,hhh) method bean is created in data base...... and a primary key is returned to the container...I mean how a client able to call the bean with its primary key fron its pool state..?
Seetesh Hindlekar
Ranch Hand

Joined: Feb 13, 2004
Posts: 244
Hi,
Before client calls the ejbCreate there is no question of primary key being created as the bean is still in the pooled state.
Try using the EntityContext.getPrimaryKey() and see what happens. Assignment for you.
Anyways the Container is responsible for calling the ejbCreate method for obtaining the primary key fields of the newly created entity object persistent representation. Container establishes the PRIMARY KEY before it invokes ejbPostCreate. Thus the entity object created by ejbCreate() must have a Primary Key.
A bean instance moves from pooled to ready state in 2 ways
1. ejbCreate and ejbPostCreate
2. ejbActivate
Now after ejbCreate u can get the Primary Key by using the EntityContext.getPrimaryKey().
Remember the result of EntityContext.getPrimaryKey() method might be different each time the instance moves from pooled to ready state.
Say now the bean it its unused is sent back to the pool with either ejbPassivate or unSetEntityContext.
Now to regain the primary key in the ejbActivate method which the Container invokes on the entity bean instance when instance is taken from the pool and assigned to an entity object identity. Here CONTAINER ENSURES THAT PRIMARY KEY OF THE ASSOCIATED ENTITY OBJECT IS AVAILABLE TO THE INSTANCE IF THE INSTANCE INVOKES GETPRIMARYKEY(),GETEJBOBJECT() on its entity context.
>>I mean how a client able to call the bean with its primary key fron its pool state..?
To answer this the CONTAINER IS RESPONSIBLE and not the client.
HTH,
Seetesh
Bis Bang
Greenhorn

Joined: Jan 28, 2004
Posts: 15
Hi Satish...I do have the same explanation... Plz go through the sequence diagram of both CMP and BMP..(Ed Roman soft copy page 627)
I feel if u call a finder method of a been which is not created means calling the finder method b4 create method in home interface will either return null or exception.. and both these method can be called in pool state.. cos these r in "Home Interface" Plz correct me if I m wrong..
Seetesh Hindlekar
Ranch Hand

Joined: Feb 13, 2004
Posts: 244
Hi Bis,
>>I feel if u call a finder method of a been which is not created means calling the finder method b4 create method in home interface will either return null or exception.. and both these method can be called in pool state.. cos these r in "Home Interface" Plz correct me if I m wrong..
I agree with you on the Sequence Diagram. I also have a few points to state.
1. When a client calls a finder method for a bean which is not created, the Container or bean provider should throw him an exception as u rightly said. Remember in this case the bean is not existing and wont be in the pool too so dont u think that the lookup itself will give some exception as the bean is not created. Thats why we normally use the Service Locator pattern.
2. When a bean is created say 10 days back and the client calls the finder method to retrive some valid or invalid information. Here again the Container or Bean provider has to take care of the exceptions raised if any.
The instance will be in pool only if its created and not otherwise.
Using Service Locator pattern will definately take care of the problem u've mentioned.
Snipped added below
public EJBHome getHome( int s ) throws ServiceLocatorException {
EJBHome home = null;
try {
Context initial = new InitialContext();
// Look up using the service name from defined constant
Object objref = initial.lookup(getServiceName(s));
// Narrow using the EJBHome Class from defined constant
Object obj = PortableRemoteObject.narrow(objref, getServiceClass(s));
home = (EJBHome)obj;
}
catch( NamingException ex ) {
throw new ServiceLocatorException(...);
}
catch( Exception ex ) {
throw new ServiceLocatorException(...);
}
return home;
}
}
I would rather have this code returning me an exception or valid "Home" reference before the client can execute the finder method.
HTH,
Seetesh
Bis Bang
Greenhorn

Joined: Jan 28, 2004
Posts: 15
thanx Seetesh
Lots of things are clear to me now..
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: ejbFind() in Entity Bean