-->
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.  [ 4 posts ] 
Author Message
 Post subject: Organising my code
PostPosted: Tue Aug 01, 2006 12:56 pm 
Newbie

Joined: Fri Jul 28, 2006 5:59 am
Posts: 7
Hello all. I've just completed my first Hibernate application, and am wondering how I can better organise my code. I've put all my Hibernate calls into one class (DataManager) which is over 500 lines long, containing loads of methods which perform some sql. Each method creates and commits a session. Please see the code example below, and let me know if this is a horrible design, how it can be improved etc.

Code:
public class DataManager{

  public Long insertValue1(String value) {

        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
   Object o; // create my object   
        Long id = (Long)session.save(o);
        session.getTransaction().commit();
        return id;
    }

  public Long insertValue2(String value) {
        // as above with different object
    }

  public List getObjects(){
   Session session = HibernateUtil.getSessionFactory().getCurrentSession();
  session.beginTransaction();
  List result = session.createQuery("My query")
   .setString(0, username).list();
   session.getTransaction().commit();
   return result;
  }

  public List getFoo(){
     // as above with different objects
  }

  // you get the idea, loads more methods doing the same thing with different sql...

}


Is this kind of normal, or is there some better way? Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 03, 2006 5:26 am 
Newbie

Joined: Fri Jul 28, 2006 5:59 am
Posts: 7
Any ideas on this folks? Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 03, 2006 8:18 am 
Newbie

Joined: Thu Aug 03, 2006 6:02 am
Posts: 2
I think what you are looking for are Data Access Objects (DAO). See http://en.wikipedia.org/wiki/Data_Access_Object.

Maybe you want to look at some other software patterns: http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 03, 2006 11:57 am 
Newbie

Joined: Fri Jul 28, 2006 5:59 am
Posts: 7
Well possibly yes, but I was wondering what other people generally do, and whether they thought what I had was 'A Bad Thing'..!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.