• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Too Lazy to Write DAOs

 
Ranch Hand
Posts: 2198
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Bartender
Posts: 4109
72
Hibernate Fedora Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author and cow tipper
Posts: 5006
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
author and jackaroo
Posts: 12199
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic