Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Too Lazy to Write DAOs

 
Ivan Krizsan
Ranch Hand
Posts: 2198
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 3930
21
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ivan Krizsan
Ranch Hand
Posts: 2198
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 12014
220
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic