This week's book giveaway is in the OCMJEA forum. We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line! See this thread for details.
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 ]
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.
SCJP, SCJD, SCWCD, SCBCD
Joined: Nov 13, 2002
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 ]
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
Joined: Nov 13, 2002
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.