This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have David & Dawn Griffiths on-line!
See this thread for details.
Win a copy of Head First Android this week in the Android forum!
  • 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

Hibernate and DAO pattern

 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a Struts action controller which loads data from a view into the ActionForm using Hibernate. Is it really useful to use a DAO to decouple the data access from the controller?

If so would the ActionForm also serve as the DTO? If I have a separate DTO then how can I transfer the data from the DTO into the ActionForm? I know I could use reflection but all this seems unecessarily complex. Sorry I am new to Hibernate and J2EE patterns so I'd appreciate any thoughts?
 
author
Posts: 608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I have a Struts action controller which loads data from a view into the ActionForm using Hibernate. Is it really useful to use a DAO to decouple the data access from the controller?



Yes. Layering your application is a very useful architecture pattern. Separation of concerns is generally a pretty good thing. Also, DAO is one of several strategies to encapsulate database access.

- Scott
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Having DAO separates the data access layer and isolates the changes to that particular layer. This is desirable.

Ideally, the ActionForm should only represent the attributes that are displayed in the html form. Your design must conform to this rule as much as possible.

The data transfer between the DTO and ActionForm can be accomplished by using the BeanUtils (reflection) utility class. In general it is not a good practice to use reflection. Main reason is that you cannot use debugger for troubleshooting problems in your code. Reflection is useful in developing tools that requires introspection.

Reflection complicates things unnecessarily. If you decide to use reflection mechanism such as BeanUtils be very careful, the class will silently fail to copy any properties that do not have the same names. The bugs can be very difficult to find. In practice it is just not worth using it in large applications. Whenever the requirements changes if you change either the DTO attributes or ActionForm attributes things can break. You have to manually make sure that they are consistent.

Another issue is that some of the attributes such as conversion from String to Date cannot be done by BeanUtils. You need to write transformation methods yourself.

Depending on the complexity of your application there are several ways that you move data from the presenation layer to the business layer. You could have an adapter that will do all the transformation from the ActionForm to the business layer objects. If the conversion is only for a few attributes then you could just have transformation methods defined in the ActionForm itself. If you are aware of the limitations of BeanUtils and if the attributes are such that it can benefit from it, you could just use it.
 
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The simple answer is yes - you should try to use Data Access Objects.

I just did a blog entry yesterday on the topic of DAOs and Hibernate.

http://www.codefutures.com/weblog/corporate/archives/2005/07/hibernate_suppo.html


Of course, this is a rather simplistic answer, and you can find a discussion on "Data Access Object (DAO) versus Object Relational Mapping (ORM)" here:

http://www.codefutures.com/weblog/andygrove/archives/2005/02/data_access_obj.html


As you can see, what CodeFutures is suggesting to Hibernate users is that they at least try both straight Hibernate and Hibernate with DAO.
 
Jack Wiesenthaler
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
awesome guys! Thanks very much for the pointers, appreciate it
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic