aspose file tools*
The moose likes Object Relational Mapping and the fly likes Too Lazy to Write DAOs Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Too Lazy to Write DAOs" Watch "Too Lazy to Write DAOs" New topic
Author

Too Lazy to Write DAOs

Ivan Krizsan
Ranch Hand

Joined: Oct 04, 2006
Posts: 2198
    
    1
Hi!
I often encounter the situation when I need to retrieve and store data from/to existing database tables. Writing DAOs and value objects for this is rather tedious and I came up with an idea about only having to write value objects, which I then annotate with some annotations that describes the mapping to table and column in the database.
Then there is some kind of persistence manager, no DAOs, that accesses the database reading and writing data.
I cannot possibly be the first one to have come to think of this idea, so now I am looking for some existing framework that can help me do this.
So, anyone got any names for me?
Many thanks in advance!
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3855

You are talking about persistence technologies . JPA introduced with EJB 3 does this and you can plug in the desired persistence provider. And there are some providers Hibernate,iBatis (among the mostly used) etc.. you might want to look at.


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Even if you do use a JPA provider it is still prudent to use DAOs, that way your code can cope with a change of JPA provider or hiding the ugly truth of some data access too complex to do with JPA.

There are code generation tools out there that will automate the creation of code for common patterns.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

I think you'll find much of EJB 3, with lazy initializations and domain based POJOs can help eliminate many value objects. Also, the entity manager is similar to the concept you are describing.

Still, what if you change entity managers or something? There's still value in DAOs that hide all of that back end stuff. Don't give up on your DAOs!

I'd also say that you only need key DAOs for your object model, not a DAO for every object. This can cut down on the number of DAOs needed as well.

By the way, Generics can greatly speed up DAO creation. Here's a little tutorial on that topic.

Advanced DAO Creation for Hibernate and JPA Using Generics

-Cameron McKenzie
Benjamin Winterberg
Ranch Hand

Joined: Sep 19, 2007
Posts: 36
Im no friend of writing one DAO for multiple entities. Requirements are changing during development. You have to write more and more DAO-methods so you end up with one bloated DAO class you have to refactor.

What Im doing is writing one DAO per entity class with a generic super class to implement CRUD methods which is the same to all entities.

You can find a good explanation of the generic DAO here: https://www.hibernate.org/328.html


Hibernate DAOs with Spring | Separation of Concerns | Eclipse Code Templates | more...
Ivan Krizsan
Ranch Hand

Joined: Oct 04, 2006
Posts: 2198
    
    1
Thanks a lot guys!
Yes, I have seen DAOs that heavily uses generics and indeed it helps some to cut down on the amount of code.
I often face a lot of requirement changes regarding database schemas, so I prefer multiple DAOs.
Well, I definitely have some material to investigate!
Thanks again!
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11509
    
  95

You will also find that there are a slew of DAO generators out there, many of which will either read the database and generate DAOs for you, or read your Java code and generate the database for you. JavaRanch has it's own code generator based on DB structures: Jenny (see also the Jenny FAQ) - very lightweight, but you will have to determine for yourself whether it is a good fit or not. There are plenty of other tools out there as well that will create DAOs for you - one I have used extensively is XDoclet - very good at working from any way of viewing the data and generating the "other" code for you.


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Too Lazy to Write DAOs