|Home |Login |Registration |Struts Step by Step Tutorial |Hibernate Step by Step Tutorial |Spring Step by Step Tutorial |JSP Step by Step Tutorial |JDBC Step by Step Tutorial |Web Services Step by Step Tutorial |EJB fundamentals | ORACLE Step by Step Tutorial | SCJP 5.0 and SCJP 6.0 Study Guide | SCWCD 5.0 Study Guide | SCJP Tips
Java mock test | SCJP mock test | SCJP DUMP | SCBCD mock test |Java online test exam | JSP online test | ORACLE online test | Hibernate online test | Servlet online test | Struts online test | EJB online test | C online test | C++ online test | Aptitude online test

Java interview questions | JSP interview questions | ORACLE interview questions | Hibernate interview questions | Servlet interview questions | Struts interview questions | JDBC interview questions | C/C++ interview questions | Spring interview questions | JMS interview questions | Informatica interview questions | EJB interview questions | OOPS and Design Pattern interview questions
1600 PMP mock questions 1400 CAPM mock questions 800 SCJP 6 mock questions 600 OCAJP 7 mock questions 590 OCPJP 7 mock questions 556 SCWCD 5 mock questions 500 OCEJWCD 6 mock questions pdfDownload (java,struts, hibernet etc) JobsJobs and Walkins

 

Recent Questions

!!! Hibernate Frequently Asked Questions !!!

How to create Session and SessionFactory in Hibernate?

SCJP 1.5/1.6 Exam Kit

!!!Answer!!!- From Technical Expert

Step 1> Put Hibernate properties in the classpath.

Step 2> Put .hbm.xml in class path ?

Code is Here to create session ...



package com.dao;




import java.io.File;
import java.io.FileInputStream;

import java.util.Properties;






import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

/**
*
* @author Satya Das
*/
public class HibernateUtil {
    protected static final Logger logger=Logger.getLogger(HibernateUtil.class);
    public static String appHome = "No";
    
    
    private static SessionFactory sessionFactory;
    
    
    private static final ThreadLocal threadSession = new ThreadLocal();
    private static final ThreadLocal threadTransaction = new ThreadLocal();
    
    
    
    /**
     * Initialize Hibernate Configuration
     */
    public static void initMonitor(){
        logger.info("Hibernate configure");
        try {
            logger.info("appHome"+appHome);
            String path_properties = appHome+File.separatorChar+"hibernate.properties";
            String path_mapping = appHome+File.separatorChar+"mapping_classes.mysql.hbm.xml";
            //String ecache = appHome+File.separatorChar+"ehcache.xml";
            
            
            Properties propHibernate = new Properties();
            propHibernate.load(new FileInputStream(path_properties));
            
            Configuration configuration = new Configuration();
            configuration.addFile(path_mapping);
            configuration.setProperties(propHibernate);
            
            /* try {
CacheManager.create(ecache);
} catch (CacheException e) {
// logger.logError(e);
}*/
            
            sessionFactory = configuration.buildSessionFactory();
            
            
            
        } catch (Throwable ex) {
            logger.error("Exception in initMonitor",ex);
            throw new ExceptionInInitializerError(ex);
        }
    }
    /**
     * @return a Session Factory Object
     */
    public static SessionFactory getSessionFactory() {
        logger.info("Inside getSessionFactory method");
        try {
            
            if (sessionFactory == null) {
                initMonitor();
            }else {
                
                //sessionFactory.getStatistics().logSummary();
            }
            
            
        } catch (Exception e) {
            logger.error("Exception in getSessionFactory",e);
        }
        
        return sessionFactory;
    }
    
    /**
     * @return Session . Start a Session
     */
    public static Session getSession() {
        
        Session s = (Session) threadSession.get();
        logger.debug("session"+s);
        if (s == null) {
            
            s = getSessionFactory().openSession();
            threadSession.set(s);
            logger.debug("session 1 $"+s);
        }
        return s;
    }
    
    /**
     * Close Session
     */
    public static void closeSession(){
        
        Session s = (Session) threadSession.get();
        threadSession.set(null);
        if (s != null && s.isOpen()) {
            s.flush();
            s.close();
        }
    }
    
    
    /**
     * Start a new database transaction.
     */
    public static void beginTransaction(){
        Transaction tx = null;
        
        if (tx == null) {
            tx = getSession().beginTransaction();
            threadTransaction.set(tx);
        }
    }
    
    /**
     * Commit the database transaction.
     */
    public static void commitTransaction(){
        Transaction tx = (Transaction) threadTransaction.get();
        try {
            if ( tx != null ) {
                tx.commit();
            }
            
            threadTransaction.set(null);
            
        } catch (HibernateException ex) {
            rollbackTransaction();
            
            throw ex;
        }
    }
    
    
    /**
     * Rollback the database transaction.
     */
    public static void rollbackTransaction(){
        
        Transaction tx = (Transaction) threadTransaction.get();
        try {
            threadTransaction.set(null);
            if ( tx != null && !tx.wasCommitted() && !tx.wasRolledBack() ) {
                tx.rollback();
            }
        } finally {
            closeSession();
        }
    }
    
    
    
    
}

Answered By : null Replied Date : Mar 9 2012
Answer :


Answered By : null Replied Date : Jan 10 2012
Answer :


Answered By : null Replied Date : Dec 15 2011
Answer :


Answered By : null Replied Date : Dec 12 2011
Answer :


Answered By : null Replied Date : Dec 11 2011
Answer :


Answered By : null Replied Date : Nov 29 2011
Answer :


Answered By : null Replied Date : Oct 15 2011
Answer :


Answered By : null Replied Date : Aug 17 2011
Answer :


Answered By : null Replied Date : Jun 22 2011
Answer :


Answered By : null Replied Date : Jun 19 2011
Answer :


Answered By : null Replied Date : May 16 2011
Answer :


Answered By : null Replied Date : Apr 19 2011
Answer :


Answered By : null Replied Date : Apr 13 2011
Answer :


Answered By : null Replied Date : Apr 12 2011
Answer :


Answered By : null Replied Date : Apr 12 2011
Answer :


Answered By : null Replied Date : Feb 28 2011
Answer :


Answered By : null Replied Date : Feb 3 2011
Answer :


Answered By : null Replied Date : Feb 2 2011
Answer :


Answered By : null Replied Date : Jan 23 2011
Answer :


Answered By : null Replied Date : Jan 17 2011
Answer :


Answered By : null Replied Date : Jan 15 2011
Answer :


Answered By : null Replied Date : Jan 12 2011
Answer :


Answered By : null Replied Date : Jan 11 2011
Answer :


Answered By : null Replied Date : Jan 4 2011
Answer :


Answered By : null Replied Date : Dec 30 2010
Answer :


Answered By : null Replied Date : Dec 29 2010
Answer :


Answered By : null Replied Date : Dec 23 2010
Answer :


Answered By : null Replied Date : Dec 23 2010
Answer :


Answered By : null Replied Date : Dec 21 2010
Answer :


Answered By : null Replied Date : Dec 19 2010
Answer :


Answered By : null Replied Date : Dec 16 2010
Answer :


Answered By : null Replied Date : Nov 20 2010
Answer :


Answered By : null Replied Date : Nov 12 2010
Answer :


Answered By : null Replied Date : Aug 21 2010
Answer :


Answered By : null Replied Date : Aug 21 2010
Answer :


Answered By : null Replied Date : Aug 14 2010
Answer :


Answered By : null Replied Date : Aug 11 2010
Answer :


Answered By : null Replied Date : May 8 2012
Answer :


Answered By : null Replied Date : Jul 22 2012
Answer :


Answered By : null Replied Date : Aug 22 2012
Answer :


Answered By : null Replied Date : Sep 8 2012
Answer :


Answered By : null Replied Date : Oct 10 2012
Answer :


Answered By : null Replied Date : Nov 21 2012
Answer :


Answered By : null Replied Date : Dec 26 2012
Answer :


Answered By : null Replied Date : Feb 5 2013
Answer :


Answered By : null Replied Date : Mar 8 2013
Answer :


Answered By : null Replied Date : Mar 20 2013
Answer :


 

You can also contribute to this answer:

Your Name:
Answer:

 
Ask Question and get answer from Expert.
View Answers List from Expert.

The information you are posting should be related to java and ORACLE technology. Not political.