下面列出了org.hibernate.cfg.Configuration#setProperty ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
protected Configuration constructConfiguration() {
Configuration configuration = new Configuration();
configuration.setProperty( AvailableSettings.CACHE_REGION_FACTORY, CachingRegionFactory.class.getName() );
configuration.setProperty( AvailableSettings.USE_NEW_ID_GENERATOR_MAPPINGS, "true" );
if ( createSchema() ) {
configuration.setProperty( Environment.HBM2DDL_AUTO, "update" );
final String secondSchemaName = createSecondSchema();
if ( StringHelper.isNotEmpty( secondSchemaName ) ) {
if ( !( getDialect() instanceof H2Dialect ) ) {
throw new UnsupportedOperationException( "Only H2 dialect supports creation of second schema." );
}
Helper.createH2Schema( secondSchemaName, configuration );
}
}
configuration.setImplicitNamingStrategy( ImplicitNamingStrategyLegacyJpaImpl.INSTANCE );
configuration.setProperty( Environment.DIALECT, getDialect().getClass().getName() );
return configuration;
}
public void configure(Configuration cfg) {
super.configure( cfg );
cfg.setProperty( Environment.CACHE_REGION_PREFIX, "" );
cfg.setProperty( Environment.USE_SECOND_LEVEL_CACHE, "true" );
cfg.setProperty( Environment.GENERATE_STATISTICS, "true" );
cfg.setProperty( Environment.USE_STRUCTURED_CACHE, "true" );
cfg.setProperty( Environment.CACHE_PROVIDER, getCacheProvider().getName() );
if ( getConfigResourceKey() != null ) {
cfg.setProperty( getConfigResourceKey(), getConfigResourceLocation() );
}
if ( useTransactionManager() ) {
cfg.setProperty( Environment.CONNECTION_PROVIDER, DummyConnectionProvider.class.getName() );
cfg.setProperty( Environment.TRANSACTION_MANAGER_STRATEGY, DummyTransactionManagerLookup.class.getName() );
}
else {
cfg.setProperty( Environment.TRANSACTION_STRATEGY, JDBCTransactionFactory.class.getName() );
}
}
private SchedulerDBManager createDatabase(boolean wipeOnStartup) throws URISyntaxException {
String configureFilename = "hibernate-update.cfg.xml";
if (wipeOnStartup) {
configureFilename = "hibernate.cfg.xml";
}
Configuration config = new Configuration().configure(new File(this.getClass()
.getResource("/functionaltests/config/" +
configureFilename)
.toURI()));
if (config.getProperty("hibernate.connection.url").contains(HsqldbServer.HSQLDB)) {
String jdbcUrl = "jdbc:hsqldb:file:" + dbFolder.getRoot().getAbsolutePath() +
";create=true;hsqldb.tx=mvcc;hsqldb.write_delay=false";
config.setProperty("hibernate.connection.url", jdbcUrl);
}
return new SchedulerDBManager(config, wipeOnStartup);
}
@Override
protected void configure(Configuration cfg) {
super.configure(cfg);
cfg.setProperty(Environment.DRIVER, org.h2.Driver.class.getName());
cfg.setProperty(Environment.URL, "jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;");
cfg.setProperty(Environment.USER, "sa");
cfg.setProperty(Environment.PASS, "");
cfg.setProperty(Environment.CACHE_REGION_PREFIX, "");
cfg.setProperty(Environment.GENERATE_STATISTICS, "true");
cfg.setProperty(Environment.USE_SECOND_LEVEL_CACHE, "true");
cfg.setProperty(Environment.USE_QUERY_CACHE, "true");
cfg.setProperty(Environment.CACHE_REGION_FACTORY, RedissonRegionFactory.class.getName());
}
private static Configuration getConfiguration() {
Configuration cfg = new Configuration();
cfg.setProperty(AvailableSettings.DIALECT,
"org.hibernate.dialect.H2Dialect");
cfg.setProperty(AvailableSettings.HBM2DDL_AUTO, "none");
cfg.setProperty(AvailableSettings.DRIVER, "org.h2.Driver");
cfg.setProperty(AvailableSettings.URL,
"jdbc:h2:mem:myexceptiondb2;DB_CLOSE_DELAY=-1;;INIT=RUNSCRIPT FROM 'src/test/resources/products.sql'");
cfg.setProperty(AvailableSettings.USER, "sa");
cfg.setProperty(AvailableSettings.PASS, "");
cfg.setProperty(AvailableSettings.CURRENT_SESSION_CONTEXT_CLASS, "thread");
return cfg;
}
@Test
public void givenMissingTable_whenSchemaValidated_thenSchemaManagementException() {
thrown.expect(SchemaManagementException.class);
thrown.expectMessage("Schema-validation: missing table");
Configuration cfg = getConfiguration();
cfg.setProperty(AvailableSettings.HBM2DDL_AUTO, "validate");
cfg.addAnnotatedClass(Product.class);
cfg.buildSessionFactory();
}
public void configure(Configuration cfg) {
super.configure( cfg );
cfg.setProperty( Environment.JPAQL_STRICT_COMPLIANCE, "true" );
cfg.setProperty( Environment.USE_SECOND_LEVEL_CACHE, "false" );
cfg.setEntityNotFoundDelegate( new JPAEntityNotFoundDelegate() );
cfg.getEventListeners().setPersistEventListeners( buildPersistEventListeners() );
cfg.getEventListeners().setPersistOnFlushEventListeners( buildPersisOnFlushEventListeners() );
cfg.getEventListeners().setAutoFlushEventListeners( buildAutoFlushEventListeners() );
cfg.getEventListeners().setFlushEventListeners( buildFlushEventListeners() );
cfg.getEventListeners().setFlushEntityEventListeners( buildFlushEntityEventListeners() );
}
@Override
protected void configure(Configuration cfg) {
super.configure(cfg);
cfg.setProperty(Environment.DRIVER, org.h2.Driver.class.getName());
cfg.setProperty(Environment.URL, "jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;");
cfg.setProperty(Environment.USER, "sa");
cfg.setProperty(Environment.PASS, "");
cfg.setProperty(Environment.CACHE_REGION_PREFIX, "");
cfg.setProperty(Environment.GENERATE_STATISTICS, "true");
cfg.setProperty(Environment.SHOW_SQL, "true");
cfg.setProperty(Environment.USE_SECOND_LEVEL_CACHE, "true");
cfg.setProperty(Environment.USE_QUERY_CACHE, "true");
cfg.setProperty(Environment.CACHE_REGION_FACTORY, RedissonRegionFactory.class.getName());
}
/**
* Try to figure out which Hibernate dialect to use.
*
* @param conf Actual Hibernate configuration
*/
private static void determineDialect(Configuration conf) {
String datasourceJndi = conf.getProperty(PROPERTY_DATASOURCE_JNDI);
if (datasourceJndi == null) {
throw new IllegalStateException("The property hibernate.connection.datasource is not set in file");
}
String figuredOutValue = getDialect(datasourceJndi);
logger.warn("Property hibernate.dialect set to " + figuredOutValue);
conf.setProperty(PROPERTY_DIALECT, figuredOutValue);
}
public void configure(Configuration cfg) {
cfg.setProperty(Environment.DEFAULT_BATCH_FETCH_SIZE, "1");
cfg.setProperty(Environment.GENERATE_STATISTICS, "true");
}
/**
* Constructor to initialize the hibernate configuration
*/
private HibernateConfig(){
logger.info("** Initializing Hibernate Connection **");
Configuration configuration = new Configuration();
configuration.addAnnotatedClass(com.tmobile.kardio.surveiller.db.entity.AlertSubscriptionEntity.class);
configuration.addAnnotatedClass(com.tmobile.kardio.surveiller.db.entity.ComponentEntity.class);
configuration.addAnnotatedClass(com.tmobile.kardio.surveiller.db.entity.ComponentFailureLogEntity.class);
configuration.addAnnotatedClass(com.tmobile.kardio.surveiller.db.entity.ComponentTypeEntity.class);
configuration.addAnnotatedClass(com.tmobile.kardio.surveiller.db.entity.CounterEntity.class);
configuration.addAnnotatedClass(com.tmobile.kardio.surveiller.db.entity.CounterMetricEntity.class);
configuration.addAnnotatedClass(com.tmobile.kardio.surveiller.db.entity.CounterMetricHistoryEntity.class);
configuration.addAnnotatedClass(com.tmobile.kardio.surveiller.db.entity.CounterMetricTypeEntity.class);
configuration.addAnnotatedClass(com.tmobile.kardio.surveiller.db.entity.DaillyCompStatusEntity.class);
configuration.addAnnotatedClass(com.tmobile.kardio.surveiller.db.entity.EnvCounterEntity.class);
configuration.addAnnotatedClass(com.tmobile.kardio.surveiller.db.entity.EnvironmentEntity.class);
configuration.addAnnotatedClass(com.tmobile.kardio.surveiller.db.entity.HealthCheckEntity.class);
configuration.addAnnotatedClass(com.tmobile.kardio.surveiller.db.entity.HealthCheckParamEntity.class);
configuration.addAnnotatedClass(com.tmobile.kardio.surveiller.db.entity.HealthCheckTypeEntity.class);
configuration.addAnnotatedClass(com.tmobile.kardio.surveiller.db.entity.RegionEntity.class);
configuration.addAnnotatedClass(com.tmobile.kardio.surveiller.db.entity.StatusEntity.class);
configuration.addAnnotatedClass(com.tmobile.kardio.surveiller.db.entity.ContainerStatsEntity.class);
configuration.addAnnotatedClass(com.tmobile.kardio.surveiller.db.entity.ApiStatusEntity.class);
configuration.addAnnotatedClass(com.tmobile.kardio.surveiller.db.entity.PromLookupEntity.class);
configuration.addAnnotatedClass(com.tmobile.kardio.surveiller.db.entity.TpsServiceEntity.class);
configuration.addAnnotatedClass(com.tmobile.kardio.surveiller.db.entity.TpsLatHistoryEntity.class);
configuration.addAnnotatedClass(com.tmobile.kardio.surveiller.db.entity.K8sApiStatusEntity.class);
configuration.addAnnotatedClass(com.tmobile.kardio.surveiller.db.entity.K8sTpsLatHistoryEntity.class);
configuration.addAnnotatedClass(com.tmobile.kardio.surveiller.db.entity.K8sObjectPodsEntity.class);
configuration.addAnnotatedClass(com.tmobile.kardio.surveiller.db.entity.K8sPodsContainersEntity.class);
configuration.setProperty("hibernate.connection.driver_class", propertyUtil.getValue(SurveillerConstants.CONFIG_DB_DRIVER_CLASS) );
configuration.setProperty("hibernate.connection.url", propertyUtil.getValue(SurveillerConstants.CONFIG_DB_URL) );
configuration.setProperty("hibernate.connection.username", propertyUtil.getValue(SurveillerConstants.CONFIG_DB_USERNAME) );
configuration.setProperty("hibernate.connection.password", propertyUtil.getValue(SurveillerConstants.CONFIG_DB_PASSWORD) );
configuration.setProperty("hibernate.dialect", propertyUtil.getValue(SurveillerConstants.CONFIG_DB_HIBERNATE_DIALECT) );
configuration.setProperty("hibernate.hbm2ddl.auto", propertyUtil.getValue(SurveillerConstants.CONFIG_DB_HIBERNATE_HBM2DDL) );
configuration.setProperty("hibernate.show_sql", propertyUtil.getValue(SurveillerConstants.CONFIG_DB_HIBERNATE_SHOWSQL) );
configuration.setProperty("hibernate.connection.pool_size", propertyUtil.getValue(SurveillerConstants.CONFIG_DB_HIBERNATE_POOLSIZE) );
configuration.setProperty("hibernate.enhancer.enableLazyInitialization", propertyUtil.getValue(SurveillerConstants.CONFIG_DB_HIBERNATE_LX_INIT) );
configuration.setProperty("hibernate.current_session_context_class", "org.hibernate.context.internal.ThreadLocalSessionContext");
StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
sessionFactory = configuration.buildSessionFactory(builder.build());
logger.info("** Initializing Hibernate Connection Completed **");
}
public void configure(Configuration cfg) {
cfg.setProperty( Environment.USE_SECOND_LEVEL_CACHE, "false");
cfg.setProperty(Environment.GENERATE_STATISTICS, "true");
}
public void configure(Configuration cfg) {
cfg.setProperty(Environment.USE_SECOND_LEVEL_CACHE, "false");
cfg.setProperty(Environment.GENERATE_STATISTICS, "true");
}
protected void configure(Configuration cfg) {
super.configure( cfg );
cfg.setProperty( Environment.GENERATE_STATISTICS, "true" );
}
public void configure(Configuration cfg) {
super.configure( cfg );
cfg.setProperty( Environment.GENERATE_STATISTICS, "true" );
}
public void configure(Configuration cfg) {
super.configure( cfg );
cfg.setProperty( Environment.GENERATE_STATISTICS, "true" );
}
public void configure(Configuration cfg) {
cfg.setProperty(Environment.GENERATE_STATISTICS, "true");
}
public void configure(Configuration cfg) {
super.configure( cfg );
cfg.setProperty( Environment.QUERY_TRANSLATOR, ClassicQueryTranslatorFactory.class.getName() );
}
public void configure(Configuration cfg) {
cfg.setProperty( Environment.GENERATE_STATISTICS, "true" );
}
private SessionFactory getSQLSessionMessung() {
try {
String url = dbURL.substring(0, dbURL.lastIndexOf("/")) + "/messungen";
Configuration configuration = new Configuration();
configuration.setProperty("hibernate.connection.url", url);
configuration.setProperty("hibernate.connection.username", username);
if (this.password != null) {
configuration.setProperty("hibernate.connection.password", this.password);
} else {
configuration.setProperty("hibernate.connection.password", "");
}
configuration.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLInnoDBDialect");
configuration.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
configuration.setProperty("connection.provider_class", "org.hibernate.connection.C3P0ConnectionProvider");
configuration.setProperty("hibernate.transaction.factory_class", "org.hibernate.transaction.JDBCTransactionFactory");
configuration.setProperty("hibernate.current_session_context_class", "thread");
configuration.setProperty("hibernate.initialPoolSize", "20");
configuration.setProperty("hibernate.c3p0.min_size", "5");
configuration.setProperty("hibernate.c3p0.max_size", "1000");
configuration.setProperty("hibernate.maxIdleTime", "3600");
configuration.setProperty("hibernate.c3p0.maxIdleTimeExcessConnections", "300");
configuration.setProperty("hibernate.c3p0.timeout", "330");
configuration.setProperty("hibernate.c3p0.idle_test_period", "300");
configuration.setProperty("hibernate.c3p0.max_statements", "13000");
configuration.setProperty("hibernate.c3p0.maxStatementsPerConnection", "30");
configuration.setProperty("hibernate.c3p0.acquire_increment", "10");
configuration.addAnnotatedClass(de.huberlin.hiwaydb.dal.Accesstime.class);
StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
SessionFactory sessionFactory = configuration.buildSessionFactory(builder.build());
return sessionFactory;
} catch (Throwable ex) {
System.err.println("Failed to create sessionFactory object." + ex);
throw new ExceptionInInitializerError(ex);
}
}