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.