Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Entity Bean -- Few Questions

 
Rajesh Shiggaon
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Everyone:

I have some questions on the entity beans, these have been bugging me for a quite time. So just thought of getting them straight.

1) The primary key object thats gets generated after the create/finder methods from the EJBHome interface, how is that related with EJBObject?
a) How the container relates this Primary object with EJBObject? I know that Container keeps track of this primary object.
b) Lets say an Entity bean EJBObject has been created for a particular record. If a new client wants to access the same record a weather a new EJBObject gets created or the same one will be used? Will there be different Primary object instance also or only one?

2) If you could see the "Client View of Entity Object Life Cycle" in the EJB spceification, they say that whenever we directly delete a record from a DB, how does the EJBObject also gets deleted with it?
a) Does the container always keep on querying on the DB and then delete the EJBObject?
b) If it does not what is the use of the callback method of ejbLoad(). If it could not find the record based on the primary key that it gets, then how it should handle? it cant even written back an expection saying that the record doesnt exist

If any of the questions are misleading or not understandable, let me know I can reframe those.

Thanks in advance

Rajesh Shiggaon
 
JayaSiji Gopal
Ranch Hand
Posts: 303
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

Here are the answers to ur q:

1) The primary key object thats gets generated after the create/finder methods from the EJBHome interface, how is that related with EJBObject?

In entity beans, findByPrimaryKey() returns a single ejb instance, representing a single row in the database table. The container wraps up this primary key to form an EJBObject, which is RMI compliant. This EJBObject provides an interface for clients to call business methods.

a) How the container relates this Primary object with EJBObject? I know that Container keeps track of this primary object.

- Refer above.

b) Lets say an Entity bean EJBObject has been created for a particular record. If a new client wants to access the same record a weather a new EJBObject gets created or the same one will be used? Will there be different Primary object instance also or only one?

- There are no EJBObjects created. There is only a pool of EJB instances in memory. When you write EJBs, u write a primary-key class. This class implements the equals() and hashcode(). The hashcode() is used by the container to check if two instances in memory have the same data. At any point of time, there is only one instance, representing a primary key.

2) If you could see the "Client View of Entity Object Life Cycle" in the EJB spceification, they say that whenever we directly delete a record from a DB, how does the EJBObject also gets deleted with it?

- You dont directly delete a record. U delete the record by calling ejbRemove(). This deletes a row from the database. Also, it returns the ejb instance back to the pool.

a) Does the container always keep on querying on the DB and then delete the EJBObject?


b) If it does not what is the use of the callback method of ejbLoad(). If it could not find the record based on the primary key that it gets, then how it should handle? it cant even written back an expection saying that the record doesnt exist

The ejbLoad() is used by the container to load the data from the db into a bean instance, based on the primary key value supplied. if it doesnt find the record, then it throws an exception.
 
Reid M. Pinchback
Ranch Hand
Posts: 775
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another important thing to keep in mind, and I don't think it might jump out at you from either the spec or the explanation above, is that the EJBObject and the entity bean instance can have quite a loose association with each other. It is tempting to think of them has having a one-to-one correspondence once you've been handed a stub reference, but that isn't always the case. Really the EJBObject just knows which pool and primary key matter to you. The container will tend to associate a particular instance from the pool for the life of the transaction, but it isn't a requirement, and isn't even likely in some situations (like read-only beans or execution of home business methods). Taken to an absurd extreme, it is even possible for a container to use a single instance to serve *all* requests for *all* entities. It would be slow, but it is possible via flushing but not committing ejbStores.
 
Rajesh Shiggaon
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi:

Please find my comments followed by rajesh

Here are the answers to ur q:

1) The primary key object thats gets generated after the create/finder methods from the EJBHome interface, how is that related with EJBObject?

In entity beans, findByPrimaryKey() returns a single ejb instance, representing a single row in the database table. The container wraps up this primary key to form an EJBObject, which is RMI compliant. This EJBObject provides an interface for clients to call business methods.

rajesh -- OK, for the first client i agree. Now another client fetches the same record, will the same EJBObject instance is return or a new one generated?

a) How the container relates this Primary object with EJBObject? I know that Container keeps track of this primary object.

- Refer above.

b) Lets say an Entity bean EJBObject has been created for a particular record. If a new client wants to access the same record a weather a new EJBObject gets created or the same one will be used? Will there be different Primary object instance also or only one?

- There are no EJBObjects created. There is only a pool of EJB instances in memory. When you write EJBs, u write a primary-key class. This class implements the equals() and hashcode(). The hashcode() is used by the container to check if two instances in memory have the same data. At any point of time, there is only one instance, representing a primary key.

rajesh -- EJBObjects are created (as you mentioned above), based on which the client can make a business call on the bean instance.
I am not sure weather there is only one instance of primary key or not, but as explained in the Mastering of EJB, the container can maintain multiple instance of entity bean representing the same data and uses the transaction attributes for ACID properties.

2) If you could see the "Client View of Entity Object Life Cycle" in the EJB spceification, they say that whenever we directly delete a record from a DB, how does the EJBObject also gets deleted with it?

- You dont directly delete a record. U delete the record by calling ejbRemove(). This deletes a row from the database. Also, it returns the ejb instance back to the pool.

rajesh -- i do understand we have to use the ejbRemove to remove the record in the DB, but lets suppose if we have another application which also uses the same DB, then that application can delete the record. So my question was put in that context

a) Does the container always keep on querying on the DB and then delete the EJBObject?


b) If it does not what is the use of the callback method of ejbLoad(). If it could not find the record based on the primary key that it gets, then how it should handle? it cant even written back an expection saying that the record doesnt exist

The ejbLoad() is used by the container to load the data from the db into a bean instance, based on the primary key value supplied. if it doesnt find the record, then it throws an exception.

rajesh -- if it throws an exception will that exception be written as RMIException? not sure on this part. As far the spec diagram tells us that the EJBObject does not exist once the record is directly deleted, there is no way it can call any methods on the entity bean instance.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic