@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
System.out.println("Ambiente: " + ApplicationConstrains.ENVIROMENT);
String datasourceName = null;
String debug = null;
switch (ApplicationConstrains.ENVIROMENT) {
case DEVELOPMENT:
datasourceName = "jdbc/vet_dev";
debug = "true";
break;
case PRODUCTION:
datasourceName = "jdbc/vet_production";
debug = "false";
break;
}
Properties properties = new Properties();
properties.setProperty(Environment.HBM2DDL_AUTO, "update");
properties.setProperty(Environment.STORAGE_ENGINE, "innodb");
properties.setProperty(Environment.JDBC_TIME_ZONE, "UTC");
properties.setProperty(Environment.DIALECT, "org.hibernate.dialect.MySQL57Dialect");
properties.setProperty(Environment.SHOW_SQL, debug);
properties.setProperty(Environment.FORMAT_SQL, debug);
properties.setProperty(Environment.USE_SQL_COMMENTS, debug);
LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
factoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
factoryBean.setDataSource(new JndiDataSourceLookup().getDataSource(datasourceName));
factoryBean.setJpaProperties(properties);
factoryBean.setPackagesToScan(Paciente.class.getPackage().getName());
return factoryBean;
}
@Bean
public PlatformTransactionManager transactionManager() {
JpaTransactionManager txManager = new JpaTransactionManager();
txManager.setValidateExistingTransaction(true);
txManager.setRollbackOnCommitFailure(true);
txManager.setEntityManagerFactory(entityManagerFactory().getObject());
return txManager;
}