-->
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: where to create Hibernatesession object.
PostPosted: Mon Jun 23, 2008 6:32 am 
Newbie

Joined: Mon Jun 23, 2008 6:21 am
Posts: 1
Hi All


I need one suggestion from your.

It is fine to create hibernateSession object on struct sction file or servlet ?


Or it is fine to create it into java Class file ?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 23, 2008 10:29 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Whatever makes the most sense is the best approach.

Usually, creating it in a HibernateService or HibernateUtil class is the most flexible or manageable solution.

Here's a sweet little tutorial on how to create a HibernateUtil class in Java. It's pretty helpful:

http://jpa.ezhibernate.com/Javacode/learn.jsp?tutorial=05samplehibernateutilclassexample

Code:
package com.examscam;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import com.examscam.model.User;

public class HibernateUtil {

private static SessionFactory factory;

public static Configuration 
             getInitializedConfiguration() {
   AnnotationConfiguration config =
               new AnnotationConfiguration();
/* add all of your JPA annotated classes here!!! */
   config.addAnnotatedClass(User.class);
   config.configure();
   return config;
}

public static Session getSession() {
   if (factory == null) {
   Configuration config = 
      HibernateUtil.getInitializedConfiguration();
   factory = config.buildSessionFactory();
   }
   Session hibernateSession =
                   factory.getCurrentSession();
   return hibernateSession;
}

public static void closeSession() {
   HibernateUtil.getSession().close();
}


public static void recreateDatabase() {
   Configuration config;
   config =
       HibernateUtil.getInitializedConfiguration();
   new SchemaExport(config).create(true, true);
}

public static Session beginTransaction() {
   Session hibernateSession;
   hibernateSession = HibernateUtil.getSession();
   hibernateSession.beginTransaction();
   return hibernateSession;
}

public static void commitTransaction() {
   HibernateUtil.getSession()
                .getTransaction().commit();
}

public static void rollbackTransaction() {
   HibernateUtil.getSession()
                 .getTransaction().rollback();
}

  public static void main(String args[]) {
    HibernateUtil.recreateDatabase();
  }

}

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


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.