Example usage for org.hibernate.cfg Configuration getProperties

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

Introduction

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

Prototype

public Properties getProperties() 

Source Link

Document

Get all properties

Usage

From source file:com.europabrewing.util.HibernateUtil.java

License:Open Source License

/**
 * Connect to the database and create a sessionFactory object
 *
 * @return the newly created session factory
 *//*from   www .j a  v a 2  s .  c o m*/
private static SessionFactory buildSessionFactory() {
    try {
        Configuration configuration = new Configuration();
        // avoid slow startup: http://stackoverflow.com/questions/10075081/hibernate-slow-to-acquire-postgres-connection
        //         configuration.setProperty("hibernate.temp.use_jdbc_metadata_defaults", "false");
        configuration.configure();

        ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
                .applySettings(configuration.getProperties()).buildServiceRegistry();

        return configuration.buildSessionFactory(serviceRegistry);

    } catch (Throwable ex) {
        System.err.println("Initial SessionFactory creation failed." + ex);
        throw new ExceptionInInitializerError(ex);
    }
}

From source file:com.ewerp.mud.content.EjMudContentSessionFactory.java

License:Apache License

@Override
public void init() {
    super.init();
    Configuration cfg = new Configuration();

    cfg.setProperty("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
    cfg.setProperty("hibernate.connection.driver_class", "org.h2.Driver");
    cfg.setProperty("hibernate.connection.url", "jdbc:h2:/home/cboyden/db1;DB_CLOSE_DELAY=-1;MVCC=TRUE");
    //        cfg.setProperty("hibernate.connection.url", "jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;MVCC=TRUE");
    cfg.setProperty("hibernate.connection.username", "sa");
    cfg.setProperty("hibernate.connection.password", "");
    cfg.setProperty("hibernate.connection.pool_size", "1");
    cfg.setProperty("hibernate.hbm2ddl.auto", "create");
    cfg.setProperty("cache.provider_class", "org.hibernate.cache.internal.NoCacheProvider");

    cfg.addClass(com.ewerp.mud.content.Room.class);

    ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(cfg.getProperties())
            .buildServiceRegistry();// w ww  .  j  a  v a  2s  .  co  m
    sessionFactory = cfg.buildSessionFactory(serviceRegistry);
}

From source file:com.ex.util.HibernateUtil.java

License:Apache License

private static SessionFactory configureSessionFactory() throws HibernateException {
    try {//from   w  w w. j a  v a 2s  . co  m
        Configuration configuration = new Configuration();
        configuration.configure();
        serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties())
                .buildServiceRegistry();
        sessionFactory = configuration.buildSessionFactory(serviceRegistry);
        return sessionFactory;
    } catch (Throwable ex) {
        System.err.println("Initial SessionFactory creation failed." + ex);
        throw new ExceptionInInitializerError(ex);
    }
}

From source file:com.fharms.marshalling.utils.HibernateTestUtil.java

License:Open Source License

public static SessionFactory getSessionFactory() {
    try {//from  w ww. j av  a  2 s  .  com
        if (sessionFactory == null) {
            URL configFile = HibernateTestUtil.class.getClassLoader().getResource("META-INF/hibernate.cfg.xml");
            Configuration configuration = new Configuration();
            configuration.configure(configFile); // configures settings from hibernate.cfg.xml
            StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
                    .applySettings(configuration.getProperties()).build();
            sessionFactory = configuration.buildSessionFactory(serviceRegistry);
        }
    } catch (Throwable ex) {
        throw new ExceptionInInitializerError(ex);
    }
    return sessionFactory;
}

From source file:com.flipkart.fdp.migration.db.DBInitializer.java

License:Apache License

private SessionFactory buildSessionFactory() {
    try {/* www. j  a  v a  2s.c om*/

        Configuration configuration = new Configuration();
        configuration.setProperties(getHibernateProperties());
        configuration.addAnnotatedClass(Batch.class);
        configuration.addAnnotatedClass(BatchRun.class);
        configuration.addAnnotatedClass(MapperDetails.class);

        StandardServiceRegistryBuilder serviceRegistryBuilder = new StandardServiceRegistryBuilder();
        serviceRegistryBuilder.applySettings(configuration.getProperties());
        ServiceRegistry serviceRegistry = serviceRegistryBuilder.build();
        sessionFactory = configuration.buildSessionFactory(serviceRegistry);

        return sessionFactory;
    } catch (Throwable e) {
        System.err.println("Initial SessionFactory creation failed." + e);
        throw new ExceptionInInitializerError(e);
    }
}

From source file:com.football.site.db.HibernateUtil.java

private static SessionFactory getSessionFactory() {
    if (sessionFactory == null) {
        try {//from   w  w w.  java  2  s.co m
            // loads configuration and mappings
            Configuration configuration = new Configuration().configure("hibernate.cfg.xml");
            //TODO : yeni bir entity geldiinde ekleme yaplacak
            configuration.addAnnotatedClass(Fixtures.class);
            configuration.addAnnotatedClass(FixturesMatchResult.class);
            configuration.addAnnotatedClass(LeagueTableRows.class);
            configuration.addAnnotatedClass(LeagueTableTeamStatistics.class);
            configuration.addAnnotatedClass(Leagues.class);
            configuration.addAnnotatedClass(Leaguetable.class);
            configuration.addAnnotatedClass(Teams.class);
            configuration.addAnnotatedClass(Players.class);
            configuration.addAnnotatedClass(TokenInfo.class);
            configuration.addAnnotatedClass(UserInfo.class);
            configuration.addAnnotatedClass(FixturesTeam.class);
            configuration.addAnnotatedClass(FixturesTeamMatchResult.class);
            configuration.addAnnotatedClass(LeaguesCountry.class);

            ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
                    .applySettings(configuration.getProperties()).build();

            // builds a session factory from the service registry
            sessionFactory = configuration.buildSessionFactory(serviceRegistry);
        } catch (HibernateException ex) {
            HelperUtil.AddErrorLog(LOG, ex);
        } catch (Exception e) {
            HelperUtil.AddErrorLog(LOG, e);
        }
    }

    return sessionFactory;
}

From source file:com.fpmislata.banco.persistence.dao.impl.hibernate.HibernateUtil.java

public static void buildSessionFactory() {
    Configuration configuration = new Configuration();
    configuration.configure();//from   w  w w .ja  v  a 2s  .c  o  m
    configuration.setProperty("hibernate.current_session_context_class", "thread");
    ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
            .applySettings(configuration.getProperties()).build();
    sessionFactory = configuration.buildSessionFactory(serviceRegistry);
}

From source file:com.fpmislata.daw.hibernateex1.Query.java

public static void main(String[] args) {
    Configuration config = new Configuration();
    config.configure();//from  ww w  . j a  v  a 2  s  .c  o m
    ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties())
            .buildServiceRegistry();
    SessionFactory sessionFactory = config.buildSessionFactory(serviceRegistry);

    Session session = sessionFactory.openSession();

    //org.hibernate.Query query =session.createQuery("SELECT p FROM Profesor p WHERE id=1001");

    //--Query with name
    org.hibernate.Query query = session.getNamedQuery("findAllProfesores");

    //---Query with name
    List<Profesor> profesores = query.list();
    for (Profesor profesor : profesores) {
        System.out.println(profesor.toString());
    }

    //---One row

    /* Profesor profesor = (Profesor) query.uniqueResult();
     System.out.println(profesor.toString());
             
     */
}

From source file:com.fpmislata.seguros.datos.hibernate.HibernateUtil.java

License:Apache License

public static synchronized void buildSessionFactory() {
    if (sessionFactory == null) {
        Configuration configuration = new Configuration();
        configuration.configure();/*from w  w w . j a  v  a 2  s  . com*/
        configuration.setProperty("hibernate.current_session_context_class", "thread");
        ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
                .applySettings(configuration.getProperties()).buildServiceRegistry();
        sessionFactory = configuration.buildSessionFactory(serviceRegistry);

    }
}

From source file:com.github.antennaesdk.messageserver.db.H2.H2Database.java

License:Apache License

public void generateSchemaAndCreateTables(SimpleDriverDataSource dataSource) {

    // Get the tables that are already in the DATABASE
    List<String> tables = new ArrayList<>();
    try {/*from ww w.j  a  va 2  s .  c  o m*/
        Connection connection = dataSource.getConnection();
        DatabaseMetaData databaseMetadata = connection.getMetaData();
        ResultSet resultSet = databaseMetadata.getTables(null, null, null, new String[] { "TABLE" });
        while (resultSet.next()) {
            String table = resultSet.getString(3);
            logger.info("Table : " + table + " ... exists");
            tables.add(table);
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }

    // Get the tables that are needed from Entity Classes
    List<Class> tablesToCreate = new ArrayList<>();
    for (Class<?> c : entityClasses) {
        // get the table names
        Table table = c.getAnnotation(Table.class);

        logger.info("Entity: " + c.getName() + " , Table: " + table.name());
        boolean isExisting = false;
        for (String dbTable : tables) {
            if (dbTable.equals(table.name())) {
                isExisting = true;
                break;
            }
        }

        if (!isExisting) {
            // these tables must be created
            tablesToCreate.add(c);
        }
    }

    // Check whether the tables need to be created...
    if (tablesToCreate.size() == 0) {
        logger.info("Tables already exist... ");
        return;
    } else {
        logger.info("Creating tables...");
    }

    //create a minimal configuration
    org.hibernate.cfg.Configuration cfg = new org.hibernate.cfg.Configuration();
    cfg.setProperty("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
    cfg.setProperty("hibernate.hbm2ddl.auto", "create");

    // create a temporary file to write the DDL
    File ddlFile = null;
    try {
        File dir = getDirectoryFromClasspath();
        ddlFile = File.createTempFile("H2_", ".SQL", dir);
        ddlFile.deleteOnExit();
    } catch (IOException e) {
        e.printStackTrace();
    }

    // add the tables to be created
    for (Class c : tablesToCreate) {
        cfg.addAnnotatedClass(c);
    }

    //build all the mappings, before calling the AuditConfiguration
    cfg.buildMappings();
    cfg.getProperties().setProperty(AvailableSettings.HBM2DDL_IMPORT_FILES, ddlFile.getName());

    cfg.getProperties().setProperty("hibernate.connection.driver_class", "org.h2.Driver");
    cfg.getProperties().setProperty("hibernate.connection.url", dataSource.getUrl());
    cfg.getProperties().setProperty("hibernate.connection.username", dataSource.getUsername());
    cfg.getProperties().setProperty("hibernate.connection.password", dataSource.getPassword());

    //execute the export
    SchemaExport export = new SchemaExport(cfg);

    export.setDelimiter(";");
    export.setFormat(true);
    // create the tables in the DB and show the DDL in console
    export.create(true, true);
}