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.ephesoft.dcma.core.hibernate.DynamicHibernateDao.java

License:Open Source License

/**
 * Constructor./*from   w  ww.  j a va  2 s.c  o  m*/
 * 
 * @param userName String
 * @param password String
 * @param driverName String
 * @param jdbcUrl String
 * @param dialectName String
 */
public DynamicHibernateDao(String userName, String password, String driverName, String jdbcUrl,
        String dialectName) {
    Properties properties = createHibernateProperties(userName, password, driverName, jdbcUrl, dialectName);

    Configuration configuration = new Configuration().addProperties(properties);
    try {
        if (sessionFactory != null) {
            sessionFactory.close();
        }
    } catch (Exception e) {
        LOG.error("Could not close session factory", e);
    }
    sessionFactory = configuration.buildSessionFactory();
}

From source file:com.ephesoft.dcma.core.hibernate.DynamicHibernateDao.java

License:Open Source License

/**
 * Constructor.//from  www .  ja v  a2s. c  o  m
 * 
 * @param cfgLocation String
 */
public DynamicHibernateDao(String cfgLocation) {
    Configuration configuration = new Configuration().configure(cfgLocation);
    String filePath = META_INF + File.separator + FOLDER_NAME + File.separator + FILE_NAME + ".properties";
    Properties properties;
    InputStream propertyInStream = null;
    try {
        propertyInStream = new ClassPathResource(filePath).getInputStream();
        properties = new Properties();
        properties.load(propertyInStream);
    } catch (Exception e) {
        LOG.error(e.getMessage(), e);
        throw new HibernateException(e.getMessage(), e);
    } finally {
        if (propertyInStream != null) {
            try {
                propertyInStream.close();
            } catch (IOException ioe) {
                LOG.info("Could not close property input stream in Dynamic Hibernate Dao.");
            }
        }
    }
    for (Object propertyKey : properties.keySet()) {
        String propertyKeyString = (String) propertyKey;
        if (!propertyKeyString.equalsIgnoreCase(PASSWORD)) {
            configuration.setProperty(propertyKeyString, properties.getProperty(propertyKeyString));
        } else {
            PasswordDecryptor passwordDecryptor = new PasswordDecryptor(
                    properties.getProperty(propertyKeyString));
            try {
                configuration.setProperty(propertyKeyString, passwordDecryptor.getDecryptedString());
            } catch (CryptographyException e) {
                LOG.error(e.getMessage(), e);
                throw new HibernateException(e.getMessage(), e);
            }
        }
    }
    try {
        if (sessionFactory != null) {
            sessionFactory.close();
        }
    } catch (Exception e) {
        LOG.error("Could not close session factory", e);
    }
    sessionFactory = configuration.buildSessionFactory();
}

From source file:com.flipkart.flux.guice.module.HibernateModule.java

License:Apache License

@Provides
@Singleton/* w  w  w.  j  a v  a2  s  .  c om*/
@Named("fluxSessionFactoryContext")
public SessionFactoryContext getSessionFactoryProvider(
        @Named("fluxHibernateConfiguration") Configuration configuration,
        @Named("fluxReadOnlyHibernateConfiguration") Configuration readOnlyConfiguration) {
    SessionFactory sessionFactory = configuration.buildSessionFactory();
    SessionFactory readOnlysessionFactory = readOnlyConfiguration.buildSessionFactory();
    Map<DataSourceType, SessionFactory> map = new HashMap<>();
    map.put(DataSourceType.READ_WRITE, sessionFactory);
    map.put(DataSourceType.READ_ONLY, readOnlysessionFactory);
    return new SessionFactoryContextImpl(map, DataSourceType.READ_WRITE);
}

From source file:com.flipkart.flux.redriver.boot.RedriverModule.java

License:Apache License

/**
 * Returns {@link SessionFactoryContext} which holds the Session Factory for Redriver.
 *///  w w  w. jav  a  2 s . c o m
@Provides
@Singleton
@Named("redriverSessionFactoryContext")
public SessionFactoryContext getSessionFactoryProvider(
        @Named("redriverHibernateConfiguration") Configuration configuration) {
    SessionFactory sessionFactory = configuration.buildSessionFactory();
    Map<DataSourceType, SessionFactory> map = new HashMap<>();
    map.put(DataSourceType.READ_WRITE, sessionFactory);
    return new SessionFactoryContextImpl(map, DataSourceType.READ_WRITE);
}

From source file:com.floreantpos.model.dao._BaseRootDAO.java

License:Open Source License

public static void initialize(String configFileName, Configuration configuration) {
    if (null == configFileName && null != sessionFactory)
        return;/* w  ww .  j  av a2 s .c o  m*/
    else if (null != sessionFactoryMap && null != sessionFactoryMap.get(configFileName))
        return;
    else {
        if (null == configFileName) {
            configuration.configure();
            com.floreantpos.model.dao._RootDAO.setSessionFactory(configuration.buildSessionFactory());
        } else {
            configuration.configure(configFileName);
            com.floreantpos.model.dao._RootDAO.setSessionFactory(configFileName,
                    configuration.buildSessionFactory());
        }
    }
}

From source file:com.floreantpos.model.dao._RootDAO.java

License:Open Source License

public static void initialize(String configFileName, Configuration configuration) {
    com.floreantpos.model.dao._RootDAO.setSessionFactory(configuration.buildSessionFactory());
}

From source file:com.floreantpos.model.dao._RootDAO.java

License:Open Source License

public static Configuration reInitialize() {
    Configuration configuration = getNewConfiguration(null);
    com.floreantpos.model.dao._RootDAO.setSessionFactory(configuration.buildSessionFactory());

    return configuration;
}

From source file:com.floreantpos.util.DatabaseUtil.java

License:Open Source License

public static void checkConnection(Configuration configuration) throws DatabaseConnectionException {
    try {// w w  w.j  a v  a  2 s.c o  m
        SessionFactory sessionFactory = configuration.buildSessionFactory();
        Session session = sessionFactory.openSession();
        Transaction transaction = session.beginTransaction();
        transaction.rollback();
        session.close();
    } catch (Exception e) {
        throw new DatabaseConnectionException(e);
    }
}

From source file:com.floreantpos.util.datamigrate.DataMigrationWindow.java

License:Open Source License

protected void migrate() {
    sourceDatabase = sourceDbPanel.getDatabase();
    destDatabase = destDbPanel.getDatabase();

    sourceConnectString = sourceDbPanel.getConnectString();
    destConnectString = destDbPanel.getConnectString();

    Configuration sourceConfiguration = _RootDAO.getNewConfiguration(null);

    sourceConfiguration = sourceConfiguration.setProperty("hibernate.dialect", //$NON-NLS-1$
            sourceDatabase.getHibernateDialect());
    sourceConfiguration = sourceConfiguration.setProperty("hibernate.connection.driver_class", //$NON-NLS-1$
            sourceDatabase.getHibernateConnectionDriverClass());

    sourceConfiguration = sourceConfiguration.setProperty("hibernate.connection.url", sourceConnectString); //$NON-NLS-1$
    sourceConfiguration = sourceConfiguration.setProperty("hibernate.connection.username", //$NON-NLS-1$
            sourceDbPanel.getUser());//from  w ww .  j  a  v  a 2s. c  o m
    sourceConfiguration = sourceConfiguration.setProperty("hibernate.connection.password", //$NON-NLS-1$
            sourceDbPanel.getPass());

    Configuration destConfiguration = _RootDAO.getNewConfiguration(null);

    destConfiguration = destConfiguration.setProperty("hibernate.dialect", destDatabase.getHibernateDialect()); //$NON-NLS-1$
    destConfiguration = destConfiguration.setProperty("hibernate.connection.driver_class", //$NON-NLS-1$
            destDatabase.getHibernateConnectionDriverClass());

    destConfiguration = destConfiguration.setProperty("hibernate.connection.url", destConnectString); //$NON-NLS-1$
    destConfiguration = destConfiguration.setProperty("hibernate.connection.username", destDbPanel.getUser()); //$NON-NLS-1$
    destConfiguration = destConfiguration.setProperty("hibernate.connection.password", destDbPanel.getPass()); //$NON-NLS-1$

    SessionFactory sourceSessionFactory = sourceConfiguration.buildSessionFactory();
    SessionFactory destSessionFactory = destConfiguration.buildSessionFactory();

    Session sourceSession = sourceSessionFactory.openSession();
    Session destSession = destSessionFactory.openSession();

    Transaction transaction = destSession.beginTransaction();
    List<MenuCategory> categories = MenuCategoryDAO.getInstance().findAll(sourceSession);
    for (MenuCategory menuCategory : categories) {
        MenuCategory m = new MenuCategory();
        m.setName(menuCategory.getName());
        m.setTranslatedName(menuCategory.getTranslatedName());
        m.setBeverage(menuCategory.isBeverage());
        m.setVisible(menuCategory.isVisible());

        PosLog.info(DataMigrationWindow.class, "" + menuCategory);

        MenuCategoryDAO.getInstance().save(m, destSession);
    }

    transaction.commit();
    destSession.close();

    PosLog.info(getClass(), "done"); //$NON-NLS-1$
}

From source file:com.frameworkset.common.poolman.hibernate.LocalSessionFactoryBean.java

License:Apache License

/**
 * Subclasses can override this method to perform custom initialization
 * of the SessionFactory instance, creating it via the given Configuration
 * object that got prepared by this LocalSessionFactoryBean.
 * <p>The default implementation invokes Configuration's buildSessionFactory.
 * A custom implementation could prepare the instance in a specific way,
 * or use a custom SessionFactoryImpl subclass.
 * @param config Configuration prepared by this LocalSessionFactoryBean
 * @return the SessionFactory instance//  ww  w.j a  v  a  2 s .com
 * @throws HibernateException in case of Hibernate initialization errors
 * @see org.hibernate.cfg.Configuration#buildSessionFactory
 */
protected SessionFactory newSessionFactory(Configuration config) throws HibernateException {
    return config.buildSessionFactory();
}