Hi,
I'm not sure, how to structure a program especially a servlet thats using hibernate?
Until now I had always a big class where all the DB work was done, it contains some methodes like:
Code:
Person getPerson(int id)
boolean updatePerson(Person person)
boolean deletePerson(int id)
.
.
.
In these methodes all the SQL stuff was executed. Using hibernate this class seems to be useless.
But where to execute all the Database- stuff?
My servlet will do something like this:
Code:
doGet(HttpServletRequest....)
{
doActions(); // updating database depending on userinput
prepareData(); // preparing data to be shown;
reqDisp.forward(req, resp); // execute JSP, showing data from database;
}
I feel ill, doing something like this:
Code:
doGet(HttpServletRequest....)
{
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
doActions(); // updating Database depending on userinput
prepareData(); // preparing Data to be shown;
reqDisp.forward(req, resp); // execute JSP;
session.getTransaction().commit();
}
This looks, like it would jumble persistence layer and business logic layer.
Doing things like
Code:
class Person
{
.
.
.
public Person getPerson(Long id)
{
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
Person ret = (Person) session.load(Person.class, id);
session.getTransaction().commit();
return ret;
}
prevents advantages like lazy proxies.
So, whats the best strategy?
Greetings Michael