Example usage for org.hibernate.cfg Configuration setProperty

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

Introduction

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

Prototype

public Configuration setProperty(String propertyName, String value) 

Source Link

Document

Set a property value by name

Usage

From source file:de.tudarmstadt.ukp.lmf.transform.LMFDBUtils.java

License:Apache License

/**
 * Create all LMF Tables in the database based on the hibernate mapping
 * @param dbConfig//from www . j ava  2s . c  o  m
 * @throws FileNotFoundException
 */
public static void createTables(DBConfig dbConfig) throws FileNotFoundException {
    // public static  void createTables(DBConfig dbConfig/*, boolean constraints*/) 
    System.out.println("CREATE TABLES");
    Configuration cfg = HibernateConnect.getConfiguration(dbConfig);
    cfg.setProperty("hibernate.hbm2ddl.auto", "none");
    SchemaExport se = new SchemaExport(cfg);
    se.create(true, true);

    /*if (constraints) {
       turnOnConstraints(dbConfig);
    }*/
}

From source file:de.tudarmstadt.ukp.lmf.transform.LMFDBUtils.java

License:Apache License

public static void dropTables(final DBConfig dbConfig) {
    System.out.println("DROP TABLES");
    Configuration cfg = HibernateConnect.getConfiguration(dbConfig);
    cfg.setProperty("hibernate.hbm2ddl.auto", "none");
    SessionFactory sf = cfg.buildSessionFactory(
            new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry());
    Session session = sf.openSession();//from w  w w.  ja v a 2 s. c o m
    Transaction tx = session.beginTransaction();
    try {
        // Create a set of all tables.
        Set<String> dropSQL = new TreeSet<String>();
        Iterator<Table> iter = cfg.getTableMappings();
        while (iter.hasNext())
            dropSQL.add("DROP TABLE " + iter.next().getName());

        // Try to delete them repeatedly until no tables are left or 
        // there have been too many repetitions.
        int remainingLoops = dropSQL.size();
        while (!dropSQL.isEmpty() && remainingLoops >= 0) {
            Iterator<String> sqlIter = dropSQL.iterator();
            while (sqlIter.hasNext()) {
                try {
                    String sql = sqlIter.next();
                    session.createSQLQuery(sql).executeUpdate();
                    sqlIter.remove();
                    System.out.println(sql);
                } catch (HibernateException e) {
                }
            }
            remainingLoops--;
        }
    } finally {
        tx.commit();
        session.disconnect();
        session.close();
    }
}

From source file:de.unisb.cs.st.javalanche.mutation.util.HibernateServerUtil.java

License:Open Source License

private static SessionFactory getServerConnection(Server s) {
    SessionFactory sessionFactory = null;
    try {//from   www.j av a2s .c  om
        AnnotationConfiguration annotationConfiguration = new AnnotationConfiguration();
        Configuration configuration = annotationConfiguration.configure();
        // String property1 = configuration
        // .getProperty(HIBERNATE_CONNECTION_URL);
        configuration.setProperty(HIBERNATE_CONNECTION_URL, s.getConnection());
        // String property2 = configuration
        // .getProperty(HIBERNATE_CONNECTION_URL);
        // System.err.println(property2);
        // System.err.println(property1.equals(property2));
        sessionFactory = configuration.buildSessionFactory();
        sessionFactories.put(s, sessionFactory);
    } catch (Throwable ex) {
        ex.printStackTrace();
        throw new ExceptionInInitializerError(ex);
    }
    return sessionFactory;
}

From source file:de.uniwue.info6.database.map.conf.HibernateUtil.java

License:Apache License

/**
 *
 *
 * @return/*from   w ww  . j  av a2  s. c  o  m*/
 */
private static SessionFactory buildSessionFactory() {
    try {
        Cfg prop = Cfg.inst();

        Configuration hibernate = new Configuration().configure("hibernate.cfg.xml");
        hibernate.setProperty("hibernate.bytecode.use_reflection_optimizer", "false");
        hibernate.setProperty("hibernate.search.autoregister_listeners", "false");
        hibernate.setProperty("hibernate.current_session_context_class", "thread");
        hibernate.setProperty("hibernate.connection.driver_class", "org.mariadb.jdbc.Driver");
        hibernate.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLInnoDBDialect");

        hibernate.setProperty("hibernate.connection.url",
                "jdbc:mariadb://" + prop.getProp(PropertiesFile.MAIN_CONFIG, PropString.MASTER_DBHOST) + ":"
                        + prop.getProp(PropertiesFile.MAIN_CONFIG, PropString.MASTER_DBPORT)
                        + "?autoReconnect=true");

        hibernate.setProperty("hibernate.default_catalog",
                prop.getProp(PropertiesFile.MAIN_CONFIG, PropString.MASTER_DBNAME));
        hibernate.setProperty("hibernate.connection.username",
                prop.getProp(PropertiesFile.MAIN_CONFIG, PropString.MASTER_DBUSER));
        hibernate.setProperty("hibernate.connection.password",
                prop.getProp(PropertiesFile.MAIN_CONFIG, PropString.MASTER_DBPASS));

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

        return sessionFactory;
    } catch (Exception ex) {
        if (LOGGER != null) {
            LOGGER.error("Initial SessionFactory creation failed.", ex);
        }
        throw new ExceptionInInitializerError(ex);
    }
}

From source file:demo.fabric.HibernateFabric.java

License:Open Source License

/**
 * Configuration of session factory with Fabric integration.
 *//*from w  w w . j a  va  2  s  . co  m*/
public static SessionFactory createSessionFactory(String fabricUrl, String username, String password,
        String fabricUser, String fabricPassword) throws Exception {
    // creating this here allows passing needed params to the constructor
    FabricMultiTenantConnectionProvider connProvider = new FabricMultiTenantConnectionProvider(fabricUrl,
            "employees", "employees", username, password, fabricUser, fabricPassword);
    ServiceRegistryBuilder srb = new ServiceRegistryBuilder();
    srb.addService(org.hibernate.service.jdbc.connections.spi.MultiTenantConnectionProvider.class,
            connProvider);
    srb.applySetting("hibernate.dialect", "org.hibernate.dialect.MySQLInnoDBDialect");

    Configuration config = new Configuration();
    config.setProperty("hibernate.multiTenancy", "DATABASE");
    config.addResource("com/mysql/fabric/demo/employee.hbm.xml");
    return config.buildSessionFactory(srb.buildServiceRegistry());
}

From source file:eds.component.data.HibernateUtil.java

private Configuration createFullConfig() {
    Configuration cfg = createPartialConfig();
    cfg.setProperty("hibernate.current_session_context_class", "thread");
    cfg.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
    cfg.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
    cfg.setProperty("hibernate.connection.url",
            "jdbc:mysql://localhost:3306/seca2?rewriteBatchedStatements=true");
    cfg.setProperty("hibernate.connection.username", "seca2");
    cfg.setProperty("hibernate.connection.password", "seca2");
    cfg.setProperty("hibernate.c3p0.min_size", "5");
    cfg.setProperty("hibernate.c3p0.max_size", "20");
    cfg.setProperty("hibernate.c3p0.timeout", "30");
    cfg.setProperty("hibernate.c3p0.max_statements", "100");
    cfg.setProperty("hibernate.hbm2ddl.auto", "update");
    cfg.setProperty("hibernate.archive.autodetection", "class, hbm");
    cfg.setProperty("hibernate.show_sql", "true");
    cfg.setProperty("hibernate.connection.autocommit", "false");

    //cfg.setProperty("hibernate.session_factory_name","hi");

    return cfg;//  w w  w  . ja  v a 2  s. co  m
}

From source file:eds.component.data.HibernateUtil.java

private Configuration createJNDIConfig() {
    Configuration cfg = createPartialConfig();
    cfg.setProperty("hibernate.current_session_context_class", "thread");
    cfg.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
    cfg.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
    cfg.setProperty("hibernate.connection.datasource", "jdbc/__default");
    //cfg.setProperty("hibernate.connection.username","seca2");
    //cfg.setProperty("hibernate.connection.password","seca2");
    cfg.setProperty("hibernate.c3p0.min_size", "5");
    cfg.setProperty("hibernate.c3p0.max_size", "20");
    cfg.setProperty("hibernate.c3p0.timeout", "30");
    cfg.setProperty("hibernate.c3p0.max_statements", "100");
    cfg.setProperty("hibernate.hbm2ddl.auto", "update");
    cfg.setProperty("hibernate.archive.autodetection", "class, hbm");
    cfg.setProperty("hibernate.show_sql", "false");
    cfg.setProperty("hibernate.connection.autocommit", "false");

    //cfg.setProperty("hibernate.session_factory_name","hi");

    return cfg;//from  www.jav  a2s .  c om
}

From source file:edu.jhuapl.dorset.components.HibernateService.java

License:Open Source License

/**
 * Create the hibernate service which initializes the session factory
 *
 * @param conf  application configuration
 * @throws UnsupportedOperationException if a mapping file is invalid
 *///  w w w  .java2  s . c  o  m
public HibernateService(Config conf) {
    Configuration hibernateConf = new Configuration();
    Config hc = conf.getConfig(HIBERNATE_KEY).atPath(HIBERNATE_KEY);
    for (Map.Entry<String, ConfigValue> entry : hc.entrySet()) {
        String key = entry.getKey();
        if (key.startsWith(MAPPING_KEY)) {
            logger.info("Loading hibernate map from " + conf.getString(key));
            try {
                hibernateConf.addResource(conf.getString(key));
            } catch (MappingException e) {
                String msg = "Something wrong with mapping: " + conf.getString(key);
                throw new UnsupportedOperationException(msg, e);
            }
        } else {
            logger.info("Setting hibernate property: " + key + "=" + conf.getString(key));
            hibernateConf.setProperty(key, conf.getString(key));
        }
    }
    sessionFactory = hibernateConf.buildSessionFactory();
}

From source file:edu.jhuapl.dorset.reporting.SqlReporterTest.java

License:Open Source License

@Before
public void setup() {
    Configuration conf = new Configuration();
    conf.setProperty("hibernate.connection.driver_class", "org.h2.Driver");
    conf.setProperty("hibernate.connection.url", "jdbc:h2:mem:sql_reporter");
    conf.setProperty("hibernate.connection.pool_size", "1");
    conf.setProperty("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
    conf.setProperty("hibernate.hbm2ddl.auto", "create-drop");
    conf.addResource("report.hbm.xml");
    sessionFactory = conf.buildSessionFactory();
}

From source file:edu.ku.brc.dbsupport.HibernateUtil.java

License:Open Source License

/**
 * Sets up the hibernate configured params
 * /*from w ww  .  ja  va 2s. c  om*/
 * @param config the config object
 */
public static void setHibernateLogonConfig(final Configuration config) {
    DBConnection dbConn = DBConnection.getInstance();

    String userName = dbConn.getUserName();
    String password = dbConn.getPassword();
    String driver = dbConn.getDriver();

    String connection = dbConn.getConnectionStr();

    // I commented this out so the property in hibernate.cfg.xml could work.
    // A better way to show SQL output is to put the following line in
    // the hibernate.cfg.xml file.
    // <property name="hibernate.show_sql">true</property>
    //config.setProperty("hibernate.show_sql", "false");

    config.setProperty("hibernate.connection.username", userName); //$NON-NLS-1$
    config.setProperty("hibernate.connection.password", password); //$NON-NLS-1$

    config.setProperty("hibernate.connection.autoReconnect", "true"); //$NON-NLS-1$ //$NON-NLS-2$
    config.setProperty("hibernate.connection.autoReconnectForPools", "true"); //$NON-NLS-1$ //$NON-NLS-2$
    config.setProperty("hibernate.connection.is-connection-validation-required", "true"); //$NON-NLS-1$ //$NON-NLS-2$

    config.setProperty("connection.autoReconnect", "true"); //$NON-NLS-1$ //$NON-NLS-2$
    config.setProperty("connection.autoReconnectForPools", "true"); //$NON-NLS-1$ //$NON-NLS-2$
    config.setProperty("connection.is-connection-validation-required", "true"); //$NON-NLS-1$ //$NON-NLS-2$

    log.info("Using database [" + connection + "]"); //$NON-NLS-1$ //$NON-NLS-2$

    // if not MS SQLServer
    if (connection.indexOf("inetdae7") == -1) //$NON-NLS-1$
    {
        config.setProperty("hibernate.connection.url", connection); //$NON-NLS-1$
        config.setProperty("hibernate.dialect", dbConn.getDialect()); //$NON-NLS-1$
        config.setProperty("hibernate.connection.driver_class", driver); //$NON-NLS-1$

        // commenting out this line to avoid use of CGLIB
        // see http://www.hibernate.org/hib_docs/v3/reference/en/html/session-configuration.html
        // search for hibernate.cglib.use_reflection_optimizer (which is the old name of the prop)
        //config.setProperty("hibernate.bytecode.use_reflection_optimizer", "true");
    } else {
        throw new RuntimeException("Connection string does not support SQLServer!"); //$NON-NLS-1$

        /*config.setProperty("hibernate.connection.url", connection + "?database="+ databaseName);
        config.setProperty("hibernate.dialect","org.hibernate.dialect.SQLServerDialect");
        config.setProperty("hibernate.connection.driver_class","com.inet.tds.TdsDriver");
        */
    }
    //else if(hostName.indexOf("sqlserver")!=-1){//jdbc:inetdae7:localhost?database=KS_fish
    //  config.setProperty("hibernate.connection.url",hostName + ";DatabaseName="+databaseName);
    //  config.setProperty("hibernate.dialect","net.sf.hibernate.dialect.SQLServerDialect");
    //  config.setProperty("hibernate.connection.driver_class","com.microsoft.jdbc.sqlserver.SQLServerDriver");
    //} 

    // added to ease the transition from Hibernate 3.1 to 3.2
    config.addSqlFunction("count", new ClassicCountFunction()); //$NON-NLS-1$
    config.addSqlFunction("avg", new ClassicAvgFunction()); //$NON-NLS-1$
    config.addSqlFunction("sum", new ClassicSumFunction()); //$NON-NLS-1$
}