• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

ejbFind() in Entity Bean

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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....
 
Ranch Hand
Posts: 244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanx Seetesh
Lots of things are clear to me now..
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic