public LocalContainerEntityManagerFactoryBean build() { LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean(); if (EntityManagerFactoryBuilder.this.persistenceUnitManager != null) { entityManagerFactoryBean.setPersistenceUnitManager( EntityManagerFactoryBuilder.this.persistenceUnitManager); } if (this.persistenceUnit != null) { entityManagerFactoryBean.setPersistenceUnitName(this.persistenceUnit); } entityManagerFactoryBean.setJpaVendorAdapter( EntityManagerFactoryBuilder.this.jpaVendorAdapter); if (this.jta) { entityManagerFactoryBean.setJtaDataSource(this.dataSource); } else { entityManagerFactoryBean.setDataSource(this.dataSource); } entityManagerFactoryBean.setPackagesToScan(this.packagesToScan); entityManagerFactoryBean .getJpaPropertyMap() .putAll(EntityManagerFactoryBuilder.this.jpaProperties); entityManagerFactoryBean.getJpaPropertyMap().putAll(this.properties); if (EntityManagerFactoryBuilder.this.callback != null) { EntityManagerFactoryBuilder.this.callback.execute(entityManagerFactoryBean); } return entityManagerFactoryBean; }
@Bean public LocalContainerEntityManagerFactoryBean entityManagerFactory() { LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean(); factory.setDataSource(dataSource()); factory.setPackagesToScan(MyVaadinUI.class.getPackage().getName()); factory.setPackagesToScan(PrisonStayDAO.class.getPackage().getName()); factory.setPackagesToScan(Prisoner.class.getPackage().getName()); factory.setPersistenceProviderClass(org.hibernate.jpa.HibernatePersistenceProvider.class); HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter(); adapter.setShowSql(true); factory.setJpaVendorAdapter(adapter); factory.getJpaPropertyMap().put("hibernate.format_sql", true); factory.getJpaPropertyMap().put("hibernate.use_sql_comments", true); return factory; }
/** * @return * @throws DataServiceException * @throws EncryptionException */ @Bean(name = "rgbycchEmf") public LocalContainerEntityManagerFactoryBean configureEntityManagerFactory() { LOG.debug("creating instance of LocalContainerEntityManagerFactoryBean (emf)"); LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean(); entityManagerFactoryBean.setDataSource(dataSource()); entityManagerFactoryBean.setPackagesToScan("com.gffny.rgbycch.model"); LOG.debug("creating instance of HibernateJpaVendorAdapter with dialect {}", dialect); HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter(); hibernateJpaVendorAdapter.setDatabasePlatform(dialect); entityManagerFactoryBean.setJpaVendorAdapter(hibernateJpaVendorAdapter); entityManagerFactoryBean.setPersistenceUnitName("rgbycchPU"); Map<String, Object> jpaProperties = entityManagerFactoryBean.getJpaPropertyMap(); jpaProperties.put(org.hibernate.cfg.Environment.DIALECT, dialect); jpaProperties.put(org.hibernate.cfg.Environment.HBM2DDL_AUTO, hbm2ddlAuto); jpaProperties.put(org.hibernate.cfg.Environment.SHOW_SQL, showSql); jpaProperties.put(org.hibernate.cfg.Environment.FORMAT_SQL, formatSql); jpaProperties.put(org.hibernate.cfg.Environment.ENABLE_LAZY_LOAD_NO_TRANS, true); // enable ordered and batches inserts and updates to improve performance // see // http://vladmihalcea.com/2015/03/18/how-to-batch-insert-and-update-statements-with-hibernate/ jpaProperties.put(org.hibernate.cfg.Environment.ORDER_INSERTS, true); jpaProperties.put(org.hibernate.cfg.Environment.ORDER_UPDATES, true); jpaProperties.put( org.hibernate.cfg.Environment.STATEMENT_BATCH_SIZE, Dialect.DEFAULT_BATCH_SIZE); jpaProperties.put(org.hibernate.cfg.Environment.BATCH_VERSIONED_DATA, true); // 'hibernate.temp.use_jdbc_metadata_defaults' is a temporary magic // value. The need for it is intended to be alleviated with future // development, thus it is not defined as an Environment constant... // // it is used to control whether we should consult the JDBC metadata to // determine certain Settings default values; it is useful to *not* do // this when // the database may not be available (mainly in tools usage). jpaProperties.put("hibernate.temp.use_jdbc_metadata_defaults", "false"); LOG.debug("returning instance of entity manager factory bean"); return entityManagerFactoryBean; }