Example usage for org.hibernate.cfg Configuration buildSessionFactory

List of usage examples for org.hibernate.cfg Configuration buildSessionFactory

Introduction

In this page you can find the example usage for org.hibernate.cfg Configuration buildSessionFactory.

Prototype

public SessionFactory buildSessionFactory() throws HibernateException 

Source Link

Document

Create a SessionFactory using the properties and mappings in this configuration.

Usage

From source file:com.viettel.model.HibernateUtils.java

License:Open Source License

public static Session getCurrentSession() {
    if (getSessionFactory() == null) {
        try {//from   ww w  . ja  va 2 s.com
            Configuration dbConfig = new Configuration().configure("hibernate.cfg.xml");
            sessionFactory = dbConfig.buildSessionFactory();
        } catch (Throwable ex) {
            log.error(ex);
            ex.printStackTrace();
        }
    }
    return getSessionFactory().getCurrentSession();
}

From source file:com.voa.weixin.db.WeixinSessionFactory.java

License:Open Source License

public void init() throws Exception {

    Configuration config = new Configuration();
    File configFile = new File(Carp.ROOTPATH + "db.config.xml");

    config.configure(configFile);// w  w  w. j  av  a  2 s.c o m
    sessionFactory = config.buildSessionFactory();
    log.info("init end");
}

From source file:com.wavemaker.runtime.data.DataServiceDefinition.java

License:Open Source License

public DataServiceDefinition(String serviceName, Configuration hbcfg, boolean isImportDB,
        boolean useIndividualCRUDOperations) {
    this.metaData = new DataServiceMetaData_Hib(serviceName, hbcfg);

    try {/*from   w  ww  . j a va  2  s  . c  o m*/
        this.sessionFactory = hbcfg.buildSessionFactory();
        Session session = null;
        try {
            session = this.sessionFactory.openSession();
            this.metaData.init(session, useIndividualCRUDOperations);
        } finally {
            try {
                session.close();
            } catch (RuntimeException ignore) {
            }
        }
    } catch (RuntimeException ex) {
        if (isImportDB) {
            // the following happens during import db - so far looks like
            // it can be ignored:
            // java.lang.NullPointerException
            // at org.hibernate.mapping.PersistentClass.
            // prepareTemporaryTables(PersistentClass.java:737)
        } else {
            throw ex;
        }
    }
}

From source file:com.xgestion2.manager.SessionFactoryManager.java

public SessionFactory getSessionFactory() {
    if (SessionFactoryManager.sessionFactory == null) {
        try {/*  w ww  .j a v a 2  s.  c  o m*/
            Configuration config = new AnnotationConfiguration().configure(new File("src/hibernate.cfg.xml"));
            config.setProperty("hibernate.connection.password",
                    Encriptador.Desencriptar(config.getProperty("hibernate.connection.password")));
            config.setProperty("hibernate.connection.username",
                    Encriptador.Desencriptar(config.getProperty("hibernate.connection.username")));
            sessionFactory = config.buildSessionFactory();
        } catch (Exception ex) {
            FormPrincipal.logger.error("Error: " + ex.toString());
        }
    }
    return sessionFactory;
}

From source file:com.xpn.xwiki.store.XWikiHibernateStore.java

License:Open Source License

private SessionFactory injectCustomMappingsInSessionFactory(XWikiDocument doc, XWikiContext context)
        throws XWikiException {
    // If we haven't turned of dynamic custom mappings we should not inject them
    if (context.getWiki().hasDynamicCustomMappings() == false) {
        return getSessionFactory();
    }// w  ww . j a  va  2s.c om

    boolean result = injectCustomMappings(doc, context);
    if (result == false) {
        return getSessionFactory();
    }

    Configuration config = getConfiguration();
    SessionFactoryImpl sfactory = (SessionFactoryImpl) config.buildSessionFactory();
    Settings settings = sfactory.getSettings();
    ConnectionProvider provider = ((SessionFactoryImpl) getSessionFactory()).getSettings()
            .getConnectionProvider();
    Field field = null;
    try {
        field = settings.getClass().getDeclaredField("connectionProvider");
        field.setAccessible(true);
        field.set(settings, provider);
    } catch (Exception e) {
        throw new XWikiException(XWikiException.MODULE_XWIKI_STORE,
                XWikiException.ERROR_XWIKI_STORE_HIBERNATE_MAPPING_INJECTION_FAILED, "Mapping injection failed",
                e);
    }
    return sfactory;
}

From source file:com.xpn.xwiki.store.XWikiHibernateStore.java

License:Open Source License

private SessionFactory injectInSessionFactory(Configuration config) throws XWikiException {
    SessionFactoryImpl sfactory = (SessionFactoryImpl) config.buildSessionFactory();
    Settings settings = sfactory.getSettings();
    ConnectionProvider provider = ((SessionFactoryImpl) getSessionFactory()).getSettings()
            .getConnectionProvider();//w w  w .  j  ava 2 s  . co m
    Field field = null;
    try {
        field = settings.getClass().getDeclaredField("connectionProvider");
        field.setAccessible(true);
        field.set(settings, provider);
    } catch (Exception e) {
        throw new XWikiException(XWikiException.MODULE_XWIKI_STORE,
                XWikiException.ERROR_XWIKI_STORE_HIBERNATE_MAPPING_INJECTION_FAILED, "Mapping injection failed",
                e);
    }
    return sfactory;
}

From source file:com.zin.PojoBean.java

public void add() {
    PojoBean p = new PojoBean();
    p.setId(id);/*  w w w.  j  ava 2 s  .  co  m*/
    p.setAge(age);
    p.setCity(city);
    p.setName(name);

    Configuration cfg = new Configuration();
    cfg.configure("hibernate.cfg.xml");

    SessionFactory sf = cfg.buildSessionFactory();
    Session s = sf.openSession();
    Transaction t = s.beginTransaction();

    s.saveOrUpdate(p);
    t.commit();
    System.out.println("Record Inserted");
}

From source file:controller.ContextLoader.java

@Override
public void contextInitialized(ServletContextEvent sce) {
    ServletContext sc = sce.getServletContext();
    try {/* w ww. j av a 2s  .  c o  m*/

        System.out.println("Listiner is called");
        Configuration cf = new Configuration();
        cf.configure("xmlFiles/hibernate.cfg.xml");
        SessionFactory sf = cf.buildSessionFactory();
        sc.setAttribute("sessionFactory", sf);
        sc.setAttribute("hibernateSession", sf.openSession());
    } catch (Exception e) {
        System.out.println("Exception in ContextListener " + e.getMessage());
        e.printStackTrace();
    }
    //To change body of generated methods, choose Tools | Templates.
}

From source file:controller.DriverListener.java

@Override
public void contextInitialized(ServletContextEvent sce) {

    ServletContext sc = sce.getServletContext();
    try {/*  w ww. jav a  2  s. c  o m*/

        Configuration cf = new Configuration();
        cf.configure("xmlFiles/hibernate.cfg.xml");
        SessionFactory sf = cf.buildSessionFactory();
        sc.setAttribute("sessionFactory", sf);

    } catch (Exception e) {

    }
}

From source file:controller.InsertAttendance.java

public static void main(String[] args) {
    //configure cfg xml file
    Configuration cf = new Configuration();
    cf.configure("xmlFiles/hibernate.cfg.xml");

    //build session factory
    SessionFactory sf = cf.buildSessionFactory();
    //get session object
    Session session = sf.openSession();/*from   w  w  w  .j a  v a 2  s. c o m*/
    //get Transaction object
    Transaction tr = session.beginTransaction();

    //STUDENT ATTANDENCE
    StudentAttendance studentAttendance = new StudentAttendance();

    studentAttendance.setAttendance("P");
    studentAttendance.setLectureDate("20/7/2013");
    studentAttendance.setRollNum("13_CS_19");
    studentAttendance.setTheoryOrPractical("practical");
    studentAttendance.setSubject("BEE");
    studentAttendance.setSemester("2nd");
    studentAttendance.setBatch("13");
    studentAttendance.setDepart("Computer System");
    studentAttendance.setSemesterState("no");

    //INSERT ATTENDANCE OBJECT
    session.save(studentAttendance);
    tr.commit();
    session.evict(studentAttendance);

    //close session and session factory
    session.close();
    sf.close();

}