List of usage examples for org.hibernate.cfg Configuration setProperty
public Configuration setProperty(String propertyName, String value)
From source file:com.tremolosecurity.unison.k8s.dataobjects.CreateLocalUsers.java
License:Apache License
@Override public void configure(String name, Properties props, NameSpace nameSpace) throws LDAPException { StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder(); Configuration config = new Configuration(); config.setProperty("hibernate.connection.driver_class", props.getProperty("driver")); config.setProperty("hibernate.connection.password", props.getProperty("password")); config.setProperty("hibernate.connection.url", props.getProperty("url")); config.setProperty("hibernate.connection.username", props.getProperty("user")); config.setProperty("hibernate.dialect", props.getProperty("dialect")); config.setProperty("hibernate.hbm2ddl.auto", "update"); config.setProperty("show_sql", "true"); config.setProperty("hibernate.current_session_context_class", "thread"); config.setProperty("hibernate.c3p0.max_size", Integer.toString(10)); config.setProperty("hibernate.c3p0.maxIdleTimeExcessConnections", Integer.toString(10)); JaxbCfgHibernateConfiguration jaxbCfg = new JaxbCfgHibernateConfiguration(); jaxbCfg.setSessionFactory(new JaxbCfgSessionFactory()); JaxbCfgMappingReferenceType mrt = new JaxbCfgMappingReferenceType(); mrt.setClazz(LocalUser.class.getName()); jaxbCfg.getSessionFactory().getMapping().add(mrt); mrt = new JaxbCfgMappingReferenceType(); mrt.setClazz(LocalGroup.class.getName()); jaxbCfg.getSessionFactory().getMapping().add(mrt); LoadedConfig lc = LoadedConfig.consume(jaxbCfg); StandardServiceRegistry registry = builder.configure(lc).applySettings(config.getProperties()).build(); SessionFactory sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory(); Session session = sessionFactory.openSession(); List<LocalGroup> groups = session.createCriteria(LocalGroup.class).list(); if (groups.size() == 0) { session.beginTransaction();/* w ww .j av a 2s .c o m*/ LocalGroup admins = new LocalGroup(); admins.setName("administrators"); admins.setDescription( "System administrators with approval access for new projects and new cluster admins"); session.save(admins); LocalGroup sys = new LocalGroup(); sys.setName("System"); sys.setDescription("System level groups not assigned to local users"); session.save(sys); LocalGroup users = new LocalGroup(); users.setName("users"); users.setDescription("All users are members"); session.save(users); LocalUser sysUser = new LocalUser(); sysUser.setSub("system"); sysUser.setMail(""); sysUser.setGroups(new ArrayList<LocalGroup>()); sysUser.getGroups().add(sys); session.save(sysUser); session.getTransaction().commit(); } sessionFactory.close(); }
From source file:com.trifork.stamdata.persistence.PersistenceModule.java
License:Mozilla Public License
@Provides @Singleton/*from ww w.j a va2 s. c o m*/ protected SessionFactory provideSessionFactory(@Persistent Set<Object> entities, @Named(JDBC_URL_PROP) String jdbcURL, @Named(DB_USERNAME_PROP) String username, @Nullable @Named(DB_PASSWORD_PROP) String password) { Configuration config = new Configuration(); config.setProperty("hibernate.connection.url", jdbcURL); config.setProperty("hibernate.connection.username", username); config.setProperty("hibernate.connection.password", password); // TODO: These can be configurable to e.g. allow in-memory databases. config.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver"); config.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLInnoDBDialect"); config.setProperty("hibernate.connection.characterEncoding", "utf8"); // Set the default behavior of dates fetched from the database to be // converted to null. The alternative 00-00-0000 or similar is strange. config.setProperty("hibernate.connection.zeroDateTimeBehavior", "convertToNull"); config.setProperty("hibernate.transaction.factory_class", "org.hibernate.transaction.JDBCTransactionFactory"); config.setProperty("hibernate.current_session_context_class", "thread"); // TODO: Look into caching. config.setProperty("hibernate.cache.provider_class", "org.hibernate.cache.NoCacheProvider"); // Since we are not using the exact same version as provided // on JBoss we need to disable search and validator registration. // // If we don't do this Hibernate will use the provided version // of search and validator which do not match the ones in the war. config.setProperty("hibernate.validator.autoregister_listeners", "false"); config.setProperty("hibernate.search.autoregister_listeners", "false"); // Use a C3P0 connection pool. // The default connection pool is not meant for production use. // // TODO: Make this configurable. config.setProperty("hibernate.c3p0.min_size", "5"); config.setProperty("hibernate.c3p0.max_size", "100"); config.setProperty("hibernate.c3p0.timeout", "200"); // See debug information // config.setProperty("hibernate.c3p0.unreturnedConnectionTimeout", "2"); // config.setProperty("hibernate.c3p0.debugUnreturnedConnectionStackTraces", "true"); // Lastly register all the entities. for (Object entity : entities) { config.addAnnotatedClass(entity.getClass()); } return config.buildSessionFactory(); }
From source file:com.vmware.photon.controller.apife.db.HibernateTestModule.java
License:Open Source License
@Provides @Singleton/*from w w w . j a v a 2 s . com*/ public SessionFactory getSessionFactory() { Configuration configuration = new Configuration(); Reflections reflections = new Reflections("com.vmware.photon.controller.apife.entities"); Set<Class<?>> classes = reflections.getTypesAnnotatedWith(Entity.class); Reflections baseReflections = new Reflections("com.vmware.photon.controller.apife.entities.base"); classes.addAll(baseReflections.getTypesAnnotatedWith(Entity.class)); Reflections commonReflections = new Reflections("com.vmware.photon.controller.api.common"); classes.addAll(commonReflections.getTypesAnnotatedWith(Entity.class)); for (final Class<?> clazz : classes) { configuration.addAnnotatedClass(clazz); } configuration.setProperty(AvailableSettings.CURRENT_SESSION_CONTEXT_CLASS, "managed"); configuration.setProperty(AvailableSettings.DIALECT, CustomH2Dialect.class.getName()); configuration.setProperty(AvailableSettings.DRIVER, "org.h2.Driver"); // in memory DB, wait up to 10 seconds after last connection closed before deleting data configuration.setProperty(AvailableSettings.URL, "jdbc:h2:mem:test;DB_CLOSE_DELAY=10"); configuration.setProperty(AvailableSettings.HBM2DDL_AUTO, "create"); configuration.setProperty(AvailableSettings.SHOW_SQL, "true"); configuration.setNamingStrategy(ImprovedNamingStrategy.INSTANCE); ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()) .build(); return configuration.buildSessionFactory(serviceRegistry); }
From source file:com.vmware.photon.controller.apife.db.MigrationTest.java
License:Open Source License
@Test public void testMigrations() throws SQLException, LiquibaseException { try (Connection connection = DriverManager.getConnection("jdbc:h2:mem:migrations", "sa", "")) { Liquibase liquibase = new Liquibase("migrations.xml", new ClassLoaderResourceAccessor(), new JdbcConnection(connection)); liquibase.update(""); Configuration configuration = new Configuration(); Reflections reflections = new Reflections("com.vmware.photon.controller.apife.entities"); Set<Class<?>> classes = reflections.getTypesAnnotatedWith(Entity.class); Reflections commonReflections = new Reflections("com.vmware.photon.controller.api.common"); classes.addAll(commonReflections.getTypesAnnotatedWith(Entity.class)); for (final Class<?> clazz : classes) { configuration.addAnnotatedClass(clazz); }/*from w w w . j ava2 s. c o m*/ configuration.setProperty(AvailableSettings.CURRENT_SESSION_CONTEXT_CLASS, "thread"); configuration.setProperty(AvailableSettings.DIALECT, "org.hibernate.dialect.H2Dialect"); configuration.setProperty(AvailableSettings.DRIVER, "org.h2.Driver"); configuration.setProperty(AvailableSettings.URL, "jdbc:h2:mem:migrations"); configuration.setProperty(AvailableSettings.USER, "sa"); configuration.setProperty(AvailableSettings.PASS, ""); configuration.setProperty(AvailableSettings.HBM2DDL_AUTO, "validate"); configuration.setNamingStrategy(ImprovedNamingStrategy.INSTANCE); ServiceRegistry serviceRegistry = new ServiceRegistryBuilder() .applySettings(configuration.getProperties()).build(); SessionFactory factory = configuration.buildSessionFactory(serviceRegistry); factory.close(); } }
From source file:com.xgestion2.manager.SessionFactoryManager.java
public SessionFactory getSessionFactory() { if (SessionFactoryManager.sessionFactory == null) { try {//from w w w .j a v a2 s .com Configuration config = new AnnotationConfiguration().configure(new File("src/hibernate.cfg.xml")); config.setProperty("hibernate.connection.password", Encriptador.Desencriptar(config.getProperty("hibernate.connection.password"))); config.setProperty("hibernate.connection.username", Encriptador.Desencriptar(config.getProperty("hibernate.connection.username"))); sessionFactory = config.buildSessionFactory(); } catch (Exception ex) { FormPrincipal.logger.error("Error: " + ex.toString()); } } return sessionFactory; }
From source file:com.yahoo.elide.contrib.dropwizard.elide.SessionFactoryFactory.java
License:Apache License
private SessionFactory buildSessionFactory(ElideBundle<?> bundle, PooledDataSourceFactory dbConfig, ConnectionProvider connectionProvider, Map<String, String> properties, List<Class<?>> entities) { final Configuration configuration = new Configuration(); configuration.setProperty(AvailableSettings.CURRENT_SESSION_CONTEXT_CLASS, "managed"); configuration.setProperty(AvailableSettings.USE_SQL_COMMENTS, Boolean.toString(dbConfig.isAutoCommentsEnabled())); configuration.setProperty(AvailableSettings.USE_GET_GENERATED_KEYS, "true"); configuration.setProperty(AvailableSettings.GENERATE_STATISTICS, "true"); configuration.setProperty(AvailableSettings.USE_REFLECTION_OPTIMIZER, "true"); configuration.setProperty(AvailableSettings.ORDER_UPDATES, "true"); configuration.setProperty(AvailableSettings.ORDER_INSERTS, "true"); configuration.setProperty(AvailableSettings.USE_NEW_ID_GENERATOR_MAPPINGS, "true"); configuration.setProperty("jadira.usertype.autoRegisterUserTypes", "true"); for (Map.Entry<String, String> property : properties.entrySet()) { configuration.setProperty(property.getKey(), property.getValue()); }/*from w w w . j a v a2s . com*/ addAnnotatedClasses(configuration, entities); bundle.configure(configuration); final ServiceRegistry registry = new StandardServiceRegistryBuilder() .addService(ConnectionProvider.class, connectionProvider) .applySettings(configuration.getProperties()).build(); configure(configuration, registry); return configuration.buildSessionFactory(registry); }
From source file:cz.filmtit.userspace.USHibernateUtil.java
License:Open Source License
/** * A path to the Hibernate configuration file. If it's necessary to change it (e.g. for unit testing), * it has to be done using reflection before the getSessionFactory method is called for the first time. *//*from w ww. ja va 2 s . c om*/ protected SessionFactory buildSessionFactory() { try { cz.filmtit.core.Configuration projectConfiguration = ConfigurationSingleton.conf(); // Create the SessionFactory from core.cfg.xml Configuration hibernateConfiguration = new Configuration(); hibernateConfiguration.configure(getConfigurationFile()); hibernateConfiguration.setProperty("hibernate.connection.username", projectConfiguration.dbUser()); hibernateConfiguration.setProperty("hibernate.connection.password", projectConfiguration.dbPassword()); hibernateConfiguration.setProperty("hibernate.connection.url", projectConfiguration.dbConnector()); serviceRegistry = new ServiceRegistryBuilder().applySettings(hibernateConfiguration.getProperties()) .buildServiceRegistry(); sessionFactory = hibernateConfiguration.buildSessionFactory(serviceRegistry); return sessionFactory; } catch (Throwable ex) { // Make sure you log the exception, as it might be swallowed logger.error("Initial SessionFactory creation failed." + ex); throw new ExceptionInInitializerError(ex); } }
From source file:cz.jirutka.rsql.hibernate.SessionFactoryInitializer.java
License:Open Source License
public static SessionFactory getSessionFactory() { if (instance != null) return instance; Configuration configuration = new Configuration(); configuration.setProperty(Environment.DRIVER, "org.hsqldb.jdbcDriver"); configuration.setProperty(Environment.URL, "jdbc:hsqldb:mem:ProductDAOTest"); configuration.setProperty(Environment.USER, "sa"); configuration.setProperty(Environment.DIALECT, HSQLDialect.class.getName()); configuration.setProperty(Environment.SHOW_SQL, "true"); configuration.addAnnotatedClass(Course.class); configuration.addAnnotatedClass(Department.class); configuration.addAnnotatedClass(Person.class); instance = configuration.buildSessionFactory(); return instance; }
From source file:database.service.DataBaseService.java
private Configuration getSqliteConfiguration() { Configuration configuration = new Configuration(); configuration.addAnnotatedClass(User.class); configuration.setProperty("hibernate.dialect", HIBERNATE_DIALECT); configuration.setProperty("hibernate.connection.driver_class", HIBERNATE_CONNECTION_DRIVER); configuration.setProperty("hibernate.connection.url", CONNECTION_URL); configuration.setProperty("hibernate.connection.username", DATABASE_USERNAME); configuration.setProperty("hibernate.connection.password", DATABASE_PASSWORD); configuration.setProperty("hibernate.show_sql", HIBERNATE_SHOW_SQL); configuration.setProperty("hibernate.hbm2ddl.auto", HIBERNATE_HBM2DDL_AUTO); configuration.setProperty("format_sql", "true"); return configuration; }
From source file:de.arago.data.util.ConfigHelper.java
License:Open Source License
static SessionFactory makeFactory(String datasource, Properties p) { final Configuration configuration = new Configuration(); configuration.configure();/*from w w w. j a v a 2s. c o m*/ String jdbcUrl = System.getProperty(PREFIX + datasource); if (jdbcUrl == null || jdbcUrl.isEmpty()) { jdbcUrl = "jdbc:mysql://127.0.0.1/rike?user=rike&password=rike&useUnicode=true&characterEncoding=UTF-8"; } configuration.setProperty("hibernate.connection.url", jdbcUrl); setCredentials(configuration, jdbcUrl); setDebug(configuration); setAdditionalProperties(configuration, p); return configuration.buildSessionFactory(); }