-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Structure of servlets using hibernate
PostPosted: Sun Apr 23, 2006 3:28 pm 
Newbie

Joined: Mon Apr 17, 2006 6:18 pm
Posts: 12
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


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 24, 2006 7:44 am 
Beginner
Beginner

Joined: Fri Oct 28, 2005 10:46 am
Posts: 37
"Best" strategy--use the DAO pattern described here to isolate your DB code:
http://java.sun.com/blueprints/corej2ee ... bject.html

Use the open-session-in-view pattern described here to allow for lazy loading:
http://hibernate.org/43.html


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.