• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

why use DAO with hibernate?

 
Ranch Hand
Posts: 755
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

So far in my struts action I used to persist my pojo with DAO eg:



Recently I've been introduce to Hibernate and I saw this usage:



My question is this: given the syntax of hibernate, is there any reason continuing using DAO to persist the data? I mean why bother building DAOs if hibernate provide this syntax to persist your data?

Why developer would choose using DAO if they are using Hibernate?

Thanks for any thoughts
 
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Struts is a MVC-based framework. The application's business logic, including data CRUD operations are part of the Model application (aka Business Object Model).

A Struts Action class is part of the Controller. This is how you create a Controller for your specific application.

What does this mean? When programming with Struts, you should avoid writing business logic code in the Action class. This includes any operations that "save" data in a database.

In your example, the execute() method and a DAO object are tightly coupled. This mean that you have coded business logic in your Controller code, not a business object. Your are missing a business object.

The purpose of a Data Access Object is to encapsulate the "data access logic" and shield your "business logic" from the details, e.g. syntax, of the data access logic.

If you pollute the code of the Controller with "data access logic", e.g. Hibernate statements, you introduce a significant dependency upon the implementations. This is not acceptable in enterprise software. It is ok for Jim's marketing website with 100 visitors per month.

If you pollute the business logic with hard-coded syntax of your ORM implementaiton, you introduce a significant dependency. The overall objective is to foster "low coupling" between components, not "hard-coded" dependencies.

Again for emphasis, there is a difference between large-scale systems and Sayeed's Java Codes Interview Questions website
 
Ranch Hand
Posts: 1936
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Major benefit of using DAO pattern is you can change implementation without impact client codes.

Think about it, if you don't use DAO and you want to change from Hibernate to JDBC what you need to do?
 
Peter Primrose
Ranch Hand
Posts: 755
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you James for your observation


When programming with Struts, you should avoid writing business logic code in the Action class. This includes any operations that "save" data in a database.



True, I read (and I think it was you) that a good indication of 'the right way to write the actions' are with junit. If it's hard to test the action - then something is wrong with the way you design it.

Ok...technical question:

Could you please comment my actions above, is this the right way to write them? Do I maintain MVC to the level of 'large-system'

Thank you and hope to learn something new!


 
Jimmy Clark
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To have a method name actionA or actionB is not very descriptive. It does not convey the behavior of this operation, what it does.

The code you posted will not compile. It looks like you have a way to go... keep up the learning and reading.

Good luck!
 
Peter Primrose
Ranch Hand
Posts: 755
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My question is with reference to the concept not the compilation (for this I have eclipse )

Does the action make sense to you in terms of BUSINESS OBJECT construction?

 
Jimmy Clark
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What action method are you reffering too?
 
Peter Primrose
Ranch Hand
Posts: 755
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Say an end-user made a payment and I wish to send him an invoice (or a reference for the payment). So s/he completes a form and clicks submit.

My question is this: given the action below - is this something that makes sense from MVC perspective? Is there a tight coupling between the action and the business logic? Am I missing the point of sturts' actions? how would you do it differently.

Thank you James

 
Jimmy Clark
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The essence of the Model-View-Controller design pattern is that the model application has no dependencies upon the View implementation.

Struts is a skeleton framework for a HTML-based View and a Java-based Controller. There is nothing concerning the Model application in Struts. This is a good thing.

So, for any business operation, you should be able to execute it from a simple command line as well as from an HTML control on an HTML web page. If you cannot run your Model application from a command-line then you have not properly implemented the MVC design pattern.





Remember, the Action class is the Controller. It should know absolutley nothing about what business logic occurs, i.e. sending emails, creating PDF files, whatever. All the Controller does is forward appropriate data from View to the appropriate business method via delegate, and send appropriate data returned from business method to appropriate View.

VIEW -----> CONTROLLER ------> MODEL -------> CONTROLLER --------> VIEW


The Struts Action classes and struts-config.xml file is how you create a specific Controller for your application.

The Struts JSP Tag Libraries are how you can create a View for your application.

In terms of the Model, you could used plain old Java objects or Session EJB

Hope this helps!
reply
    Bookmark Topic Watch Topic
  • New Topic