Pet store architecture question

Hello,

I have downloaded Sun's sample J2EE pet store

enterprise application 1.1.2. I was surprised

to find that in at least two entity beans that

they loaded model data into the entity bean

in the ejbFindByPrimaryKey method:

AccountEJB.java:

.

.

public String ejbFindByPrimaryKey (String key) throws FinderException {

try{

AccountDAO dao = getDAO();

String userId = dao.findByPrimaryKey(key);

this.accountDetails = dao.load(userId);

return(userId);

.

.

Could someone please tell me if there is any

benefit to using the DAO to load data in the ejb

find method? Why not just load the data in

the ejb load method? Is this even a good idea?

Thanks for any advice!

[800 byte] By [neu9109] at [2007-9-26 5:19:19]
# 1
if you view the code in getDAO(), you will know why they use this pattern.
bidzhao at 2007-6-29 19:24:12 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2
http://java.sun.com/j2ee/blueprints/design_patterns/data_access_object/index.html
neville_sequeira at 2007-6-29 19:24:12 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 3

Thank you for your responses.

I think I might have phrased my question poorly,

and that's why I think I received the feedback

I did.

I wasn't questioning the use of DAO. As a pattern,

I see how useful it is. What I was surprised about

was WHERE the DAO.load method was used.

Why should the entity bean read data from the DAO

in the ejbFindByPrimaryKey method? If I was

writing the code, I would leave out the line:

this.accountDetails = dao.load(userId);

because that's the job of the ejbLoad method. Since

loading from the DAO can potentially be slow, doesn't

this lead to a slight performance problem?

neu9109 at 2007-6-29 19:24:12 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 4
The reason is:Normally, after u do a find by PK, you want to get the details of the account (see getDetails() - here u avoid another ejbLoad method... )hope this answers your question...sudu
mihira1 at 2007-6-29 19:24:12 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 5

Thanks for your reply!

I thought the way it worked was:

1) A client calls AccountHome.findByPrimaryKey

2) The container calls AccountEJB.ejbFindByPrimaryKey

which returns a PK

3) The container sets the PK into the EntityContext

of an EJBObject

4) The container calls the ejbLoad of the EJBObject.

This EJBObject calls getPrimaryKey of its

EntityContext and uses its DAO to load the

account data

5) The container returns a remote interface to the

EJBObject to the client

6) The client calls getDetails of the remote interface

If this logic is correct, then according to the

pet store code, account data is loaded after the

container calls ejbFindByPrimaryKey AND after ejbLoad.

So it seems wasteful to load the data twice. I don't

see how you can avoid the ejbLoad in this scenario...

neu9109 at 2007-6-29 19:24:12 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 6

I don't think ejbFindByPrimaryKey(PK) will result in ejbLoad() being executed on the EntityBean.

After u send ejbFBPK(PK), u get the RI for the EB.

Then, when u send u'r very first busines method to the RI (tx/non-tx), then the container would always invoke the ejbLoad() method on the EB.

Thereafter, every transactional business method would result in a ejbLoad(), business method, ejbStore() on the EB.

For non-transactional business methods, u can never be sure that ejbLoad() will be called....

mihira1 at 2007-6-29 19:24:12 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...