This week's book giveaway is in the Agile and Other Processes forum.We're giving away four copies of The Little Book of Impediments (e-book only) and have Tom Perry on-line!See this thread for details.

Using Java EE entities over DAO

Could somebody please explain to me in what circumstances it makes sense to use Java EE Entities over DAO.

Conversely - when does it make sense to use DAO over Java EE Entities

Thanks!
Joe

ntumba lobo

Ranch Hand

Posts: 180

posted 5 years ago

1

Well it depends on the complexity of your persistence layer. If it is very simple you could do without DAO.
But most of the time you will find entities and DAO are used in combination even when the persistenc layer is very simple.
It just keeps your business logic separated from your persistence logic.
In that case a DAO manages the persistence of a specific entity. It is sometimes called EAO for Entity Access Object.

SCJP 5 , SCWCD 5, SCEA 5

Joe O'Toole

Ranch Hand

Posts: 51

posted 5 years ago

Thanks for the response ntumba.

Actuall, from re-reading my question I don't think I was entirely clear. Really, what I mean to say in what circumstances would you use JPA/EJB3 over DAO

Thanks
Joe

ntumba lobo

Ranch Hand

Posts: 180

posted 5 years ago

oh I see so your question is ORM vs pure JDBC.
In that case you would use an ORM you when have a complex OO business to persist in DB and you want to favor ease of development.
The pure JDBC approach is more used for relatively simple applications or when you want to tune your data access for better performance.
And again you can mix both approaches in the same application to cater for edge cases.
I hope that's more in line with your question now.

SCJP 5 , SCWCD 5, SCEA 5

Joe O'Toole

Ranch Hand

Posts: 51

posted 5 years ago

Hi Ntumba

Thanks for the response.

Sorry really what I was getting at is why would you go the DAO route to persist your data as opposed to JPA.

From reading more I think I would go the DAO route if the persist store is a file, or the data is being retrieved from stored proc's or finally you want to decouple persist info from the business object.

Think you would use JPA for Rapid development and Java domain layer is a good fit to DB tables

Thoughts?

Thanks
Joe

Lakshman Arun

Greenhorn

Posts: 19

posted 5 years ago

DAO is a layer between the business logic and persistent logic. The layer can use different frameworks for doing the persistence of objects, could be JPA/JDO and vendor specific implementations like hibernate etc.

Irrespective of the complexity/size of the application, its always better to have well defined contracts(interfaces) between the layers.

ntumba lobo

Ranch Hand

Posts: 180

posted 5 years ago

DAO and JPA are not mutually exclusive concepts, you can have DAOs with JPA code.
So if your question is when using JPA is appropriate or not, the answer will be that whenever you have objects that need to be stored in a relational database you can use JPA.
Any other storage like flat files, LDAP for example you would have DAO with the appropriate logic for the specific storage.
JPA caters only for object to relational DB storage.

Regarding last response, Im sorry but the complexity/size of an app is a factor affecting the decision of using an ORM or not.
The cost of adding an ORM is not null, in some cases a simple design a more suited than a rigorous more complex one -> KISS