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 (UserPresentation.java)
And this is UserDAOImpl.java
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.
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.