wood burning stoves
The moose likes OO, Patterns, UML and Refactoring and the fly likes Should it be one DAO per entity? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Should it be one DAO per entity?" Watch "Should it be one DAO per entity?" New topic

Should it be one DAO per entity?

Bala Gangadhar
Ranch Hand

Joined: Oct 07, 2008
Posts: 98
In my project the persistance layer is implemented in such a way that one DAO per service. For example GenerateCSVDao for csv generation service and so. This DAO may access more than one table and may handle more than one entity.
Now i was asked to refactor the dao layer to make use of Generics. When I gone though the hibernate site for DAO patterns using Generics, DAO's declaration is like CustomerHibernateDao<Customer, custId>.

My doubt is Generic DAO pattern applicable only when one DAO per entitiy cases?
Can't i make use of Generics in my project? if so how?

Thanks in advance,
Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 33102

DAOs can be, but don't have to be, one per entity.

The person who asked you to use generics might have meant on a method level rather than a class level. For example, DAOs frequently query for a list of something. You could return List<Customer> rather than List.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Ran Pleasant
Ranch Hand

Joined: Jan 16, 2003
Posts: 76

In many cases each entity needs its own DAO. However, when possible have your DAO objects deal with components of objects. For example, you want a DAO for an Invoice class but you probably don't want a DAO for the related LineItem class. Deal with the LineItem objects only as part of Invoice obect itself, ie save the LineItems objects by saving the Invoice object. If you have a DAO for every entity you are probably dealing with an Anemic Domain Model.

I agree. Here's the link: http://aspose.com/file-tools
subject: Should it be one DAO per entity?
It's not a secret anymore!