aspose file tools*
The moose likes EJB and other Java EE Technologies and the fly likes Design: should I add a 'DAO' layer between my Session bean and my Entities? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Design: should I add a Watch "Design: should I add a New topic
Author

Design: should I add a 'DAO' layer between my Session bean and my Entities?

Sol Mayer-Orn
Ranch Hand

Joined: Nov 13, 2002
Posts: 311
Hi,

I'm new to EJB3, and would like to ask a design question:

Is it customary to add a 'Data Access' layer between the Session beans and the Entities?
Or would your Session bean simply access the Entity API directly?

For example, say I have a 'Bank' session bean, and Account entities.

Then Bank can directly access the Entity API:


But I'm considering an alternative of writing a Data Access layer, something like the following.
It seems more work, but the session bean becomes more readable (especially if it has some additional complex logic and not just simple 'find'), and it can hide some sql implementation choices (e.g. if we find some way to optimize the query, or move to native sql, etc):


Of course this is a simplified example... in real life, the code may be more complex (especially the Session logic), and a query may be re-used from several methods.

If anyone can share their views, and/or refer me to EBJ design tutorials, it will be appreciated.
Thanks
[ September 24, 2007: Message edited by: Sol Ma-Orn ]
Darya Akbari
Ranch Hand

Joined: Aug 21, 2004
Posts: 1855
Hi Sol,

I would use a DAOFactory in your Session Bean. See the links I gave in this thread why DAO? from JavaRanch's ORM forum for more info.

Regards,
Darya


SCJP, SCJD, SCWCD, SCBCD
Sol Mayer-Orn
Ranch Hand

Joined: Nov 13, 2002
Posts: 311
Thanks for the excellent reply and the link.

Can I just ask: how do you make the DAO available to the Session bean?
I'm considering 3 alternatives (see below).
Which seems most reasonable, in your experience ? Or do you have a different, better approach?
Thanks again.

Options I was considering:





[ September 26, 2007: Message edited by: Sol Mayer-Orn ]
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
What about a factory which returns the DAO instance. The factory could be configured to return the correct instance depending on a configuration which is set before the EJB container starts up.


SCJP 1.4, SCWCD 1.3, SCBCD 1.3
Sol Mayer-Orn
Ranch Hand

Joined: Nov 13, 2002
Posts: 311
Thanks, Roger Chung-Wee, for this reply.
The factory approach sounds good - neat and flexible.

But in that case, could I just shift my question a little: how do you make the *Factory* available to the Session bean ? Is your factory just a static method (or static singleton) ? Or is your factory a Local session bean ? etc.

Thanks again
 
Don't get me started about those stupid light bulbs.
 
subject: Design: should I add a 'DAO' layer between my Session bean and my Entities?
 
Similar Threads
MZs Notes - Pg 159
Rephrase: Does the DOA need to be an session bean as well? SOS.... lossing hairs...
Query on Detached and Removed Entities
DOA as session bean ?
Greenhorn: JPA entitymanager is null and I don't think is suppose to be?