This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Struts and the fly likes Hibernate and Struts 2 question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Hibernate and Struts 2 question" Watch "Hibernate and Struts 2 question" New topic

Hibernate and Struts 2 question

Benny Zlo

Joined: Jan 28, 2012
Posts: 7

I'm trying to learn how to work with Struts 2 (along with JPA and Hibernate) and I've written this piece of code to start my learning project:

This is Registeration.jsp:

This is struts.xml

This is the action class (

And this is

UserService and and UserServiceImpl are the same just without the implementation.

Now for my questions:
I am aware that my UserDAOImpl is written pretty bad but I just wanted to see if it works. I want some advice about how should I be writing this DAO classes, I suppose I shouldn't be creating an instance of EntityManager and EntityManagerFactory in every class and begin and close a transaction for each persist(). I assume that if I want this done write, I should write another layer, probably an abstract class or an interface which will contain the functions that are common to all DAO classes and the all the EntityManager operations should be done from there, and where should I close the EntityManager and the EntityManagerFactory, I know it should be done at the end of the application, but how do I tell it in a JEE application. I don't have any actual experience with this so I was hoping to get some advice from experienced users on how these things are done in the real world.

I actually had a question more related to the code I've pasted here but I managed to resolve it during the writing of this post, so I'm left with only one (and quite big) question.

I know this question is a little abstract and there is more than one correct answer for it, but I would like to get some direction on this.

Thanks in advance!
Joe Ess

Joined: Oct 29, 2001
Posts: 9191

I haven't used JPA, but there is a Hibernate Session Plugin for Struts 2.
I didn't know about it when I wrote my last Hibernate Struts app, so I just wrote an interceptor to take care of the Hibernate setup and put a Session in the action.

[How To Ask Questions On JavaRanch]
I agree. Here's the link:
subject: Hibernate and Struts 2 question
It's not a secret anymore!