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)

Hibernate interview questions

!!! Hibernate interview questions !!!

How to create Session and SessionFactory in Hibernate?




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();
        }
    }
    
    
    
    
}

Reviews and Comments


PMP, CAPM, PMI is a registered certification mark of the Project Management Institute, Inc

Copyright © www.techfaq360.com 2016


About US | Contact US | Privacy Policy | Terms and Conditions  | Website disclaimer  | Cancellation and Refund Policy  | Shipping & Delivery Policy