Example usage for org.hibernate.cfg Configuration configure

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

Introduction

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

Prototype

public Configuration configure() throws HibernateException 

Source Link

Document

Use the mappings and properties specified in an application resource named hibernate.cfg.xml.

Usage

From source file:org.halyph.sessiondemo.Demo.java

License:Open Source License

public static void main(String[] args) {
    // configures settings from hibernate.cfg.xml
    // Hibernate 3 style
    //      SessionFactory sessionFactory = new Configuration().configure()
    //            .buildSessionFactory();

    // Hibernate 4 style
    Configuration configuration = new Configuration();
    configuration.configure();
    ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties())
            .buildServiceRegistry();//from ww  w.  j a  v a 2 s  .  co  m
    SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);

    // ------------------------------

    // create a couple of events...
    Session session = sessionFactory.openSession();
    session.beginTransaction();
    session.save(new Event("Our very first event!", new Date()));
    session.save(new Event("A follow up event", new Date()));
    session.getTransaction().commit();
    session.close();

    // now lets pull events from the database and list them
    session = sessionFactory.openSession();
    session.beginTransaction();
    List result = session.createQuery("from Event").list();
    for (Event event : (List<Event>) result) {
        System.out.println("Event (" + event.getDate() + ") : " + event.getTitle());
    }
    session.getTransaction().commit();
    session.close();

    if (sessionFactory != null) {
        sessionFactory.close();
    }
}

From source file:org.infinity.util.HibernateUtil.java

License:Open Source License

private HibernateUtil() {
    try {// w w  w  .jav  a  2s  .  c o  m
        Configuration configuration = new Configuration();

        configuration = configuration.configure();

        try {
            _sessionFactory = configuration.buildSessionFactory();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    } catch (Exception e) {
        e.printStackTrace();
        _log.error(e, e);
    }
}

From source file:org.jboss.seam.persistence.test.util.ManagedHibernateSessionProvider.java

License:Open Source License

@RequestScoped
@Produces//w  w w  . j a v  a  2  s  . c  o m
@SeamManaged
public SessionFactory createSessionFactory() {
    Configuration config = new Configuration();
    config.configure();
    return config.buildSessionFactory();
}

From source file:org.light.portal.core.dao.hibernate.PortalDaoImpl.java

License:Apache License

public void init(Portlets portlets, PortalLayout portalLayout, PortalSecurity portalSecurity) {
    if (portalSecurity.getApplication().getReCreateTable()) {
        Configuration config = new Configuration();
        SchemaExport s = new SchemaExport(config.configure());
        s.create(false, true);/* ww  w.  j  a  v a2 s . c  o  m*/
    }
    this.createDefaultRef(_DEFAULT_ROLE, portlets);
    this.createDefaultPortalByUser(portalLayout, portalSecurity);
    this.createDefaultPortalSecurity(portalSecurity);
}

From source file:org.mgenterprises.openbooks.configuration.HibernateConfigurationLoader.java

License:Open Source License

public Configuration getConfiguration() {
    Configuration configuration = new Configuration();
    configuration.configure();
    for (Object key : properties.keySet()) {
        String keyString = (String) key;
        configuration.setProperty(keyString, properties.getProperty(keyString));
    }// w  ww  .ja v  a  2 s . c o  m
    return configuration;
}

From source file:org.openbp.server.persistence.hibernate.HibernatePersistenceContextProvider.java

License:Apache License

/**
 * Creates a Hibernate configuration object that includes the OpenBP classes.
 * Adds the following class mappings (if not present yet):<br>
 * TokenContextImpl<br>//from   w  ww  .  ja v a2 s  . co  m
 * WorkflowTaskImpl<br>
 * DbModel<br>
 * DbModelItem
 *
 * @return The new configuration object
 */
public Configuration createHibernateConfiguration() {
    // Prepare a configuration object for the session creation
    Configuration configuration = new Configuration();

    // Add the configuration in the hibernate.cfg.xml file
    configuration.configure();

    // Add the OpenBP core objects
    addOpenBPClassMappingsToConfiguration(configuration);

    return configuration;
}

From source file:org.openspaces.persistency.hibernate.SessionFactoryBuilder.java

License:Open Source License

/**
 * Configure according to hibernate.cfg.xml
 *//*from   w  w  w.  jav a2s  . c  om*/
private static Configuration configure(Configuration config, String hibernateFile) {
    // In case that hibernate config file location is null find hibernate.cfg.xml
    // file in classpath
    if (hibernateFile == null)
        return config.configure();
    else
        return config.configure(hibernateFile);

}

From source file:org.opentides.persistence.hibernate.MultiTenantSchemaUpdate.java

License:Apache License

/**
 * Creates or updates the schema for the given tenantId.
 * Invoke this method only when a separate schema is needed 
 * for the tenant./*from w  w w  .ja v  a  2 s.  c om*/
 * 
 * @param tenantId
 * @return
 * @throws ClassNotFoundException 
 * @throws SQLException 
 * @throws HibernateException 
 */
@Transactional
public boolean schemaEvolve(String schema) {
    if (StringUtil.isEmpty(schema))
        schema = defaultSchema;
    _log.info("Performing schema update for schema = " + schema);
    try {
        Connection connection = connectionProvider.getConnection();
        connection.createStatement().execute("CREATE SCHEMA IF NOT EXISTS " + schema + ";");
        connection.createStatement().execute("USE " + schema + ";");

        // Code below is specific to hibernate
        Configuration cfg = new Configuration();
        for (String clazz : DatabaseUtil.getClasses()) {
            try {
                cfg.addAnnotatedClass(Class.forName(clazz));
            } catch (ClassNotFoundException e) {
                _log.error("Class not found for schema upate [" + clazz + "]", e);
            }
        }
        // add classes from packagesToScan
        for (String clazz : persistenceScanner.scanPackages()) {
            try {
                cfg.addAnnotatedClass(Class.forName(clazz));
            } catch (ClassNotFoundException e) {
                _log.error("Class not found for schema upate [" + clazz + "]", e);
            }
        }
        cfg.configure();

        // is this a new schema?
        if (connection.createStatement().executeQuery("SHOW TABLES LIKE 'SYSTEM_CODES'").next() == false) {
            //new schema, let's build it
            initializeSchema(cfg, connection, schema);
        }
        return true;
    } catch (HibernateException e) {
        _log.error("Failed to update schema for [" + schema + "].", e);
        return false;
    } catch (SQLException e) {
        _log.error("Failed to update schema for [" + schema + "].", e);
        return false;
    }
}

From source file:org.photovault.common.MysqlDescriptor.java

License:Open Source License

/**
 * Cretes Hibernate configuration for accessing the database.
 * @param username MySql user name//from   w  w  w  .j a  v a  2  s  .c o  m
 * @param passwd password for user
 * @return The Hibernate configuration
 * @throws org.photovault.common.PhotovaultException Not thrown by this
 * class.
 */
public Configuration initHibernate(String username, String passwd) throws PhotovaultException {
    Configuration cfg = new AnnotationConfiguration();
    cfg.configure();
    cfg.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
    cfg.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
    cfg.setProperty("hibernate.connection.url", "jdbc:mysql://" + host + "/" + dbname);
    cfg.setProperty("hibernate.connection.username", username);
    cfg.setProperty("hibernate.connection.password", passwd);
    return cfg;
}

From source file:org.snaker.engine.access.hibernate.HibernateHelper.java

License:Apache License

/**
 * SessionFactoryhibernate.cfg.xml?sessionFactory
 * ?Configuration.initAccessDBObjectsessionfactory
 * ioc//from  w  ww  .jav  a 2  s  . c  o  m
 */
private static void initialize() {
    String driver = ConfigHelper.getProperty("jdbc.driver");
    String url = ConfigHelper.getProperty("jdbc.url");
    String username = ConfigHelper.getProperty("jdbc.username");
    String password = ConfigHelper.getProperty("jdbc.password");
    String dialect = ConfigHelper.getProperty("hibernate.dialect");
    AssertHelper.notNull(driver);
    AssertHelper.notNull(url);
    AssertHelper.notNull(username);
    AssertHelper.notNull(password);
    AssertHelper.notNull(dialect);
    String formatSql = ConfigHelper.getProperty("hibernate.format_sql");
    String showSql = ConfigHelper.getProperty("hibernate.show_sql");
    Configuration configuration = new Configuration();

    if (StringHelper.isNotEmpty(driver)) {
        configuration.setProperty("hibernate.connection.driver_class", driver);
    }
    if (StringHelper.isNotEmpty(url)) {
        configuration.setProperty("hibernate.connection.url", url);
    }
    if (StringHelper.isNotEmpty(username)) {
        configuration.setProperty("hibernate.connection.username", username);
    }
    if (StringHelper.isNotEmpty(password)) {
        configuration.setProperty("hibernate.connection.password", password);
    }
    if (StringHelper.isNotEmpty(dialect)) {
        configuration.setProperty("hibernate.dialect", dialect);
    }
    if (StringHelper.isNotEmpty(formatSql)) {
        configuration.setProperty("hibernate.format_sql", formatSql);
    }
    if (StringHelper.isNotEmpty(showSql)) {
        configuration.setProperty("hibernate.show_sql", showSql);
    }
    sessionFactory = configuration.configure().buildSessionFactory();
}