This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes OO, Patterns, UML and Refactoring and the fly likes why use DAO with hibernate? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "why use DAO with hibernate?" Watch "why use DAO with hibernate?" New topic
Author

why use DAO with hibernate?

Peter Primrose
Ranch Hand

Joined: Sep 10, 2004
Posts: 755
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
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
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
Hong Anderson
Ranch Hand

Joined: Jul 05, 2005
Posts: 1936
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?


SCJA 1.0, SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCJP 5.0, SCEA 5, SCBCD 5; OCUP - Fundamental, Intermediate and Advanced; IBM Certified Solution Designer - OOAD, vUML 2; SpringSource Certified Spring Professional
Peter Primrose
Ranch Hand

Joined: Sep 10, 2004
Posts: 755
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

Joined: Apr 16, 2008
Posts: 2187
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

Joined: Sep 10, 2004
Posts: 755
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

Joined: Apr 16, 2008
Posts: 2187
What action method are you reffering too?
Peter Primrose
Ranch Hand

Joined: Sep 10, 2004
Posts: 755
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

Joined: Apr 16, 2008
Posts: 2187
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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why use DAO with hibernate?
 
Similar Threads
Data not persisting, when it is sent through service layer
Hibernate - Cascading problems using ManyToOne association
Hibernate Tool custom template
JPA Query
question Quartz with Hibernate