Last active
April 3, 2021 15:55
-
-
Save amorales4u/36d845712c692869aacce46241acee64 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import dev.c20.test2.demo.stg.Entidad; | |
| import org.springframework.context.annotation.Bean; | |
| import org.springframework.context.annotation.Conditional; | |
| import org.springframework.context.annotation.Configuration; | |
| import javax.sql.DataSource; | |
| @Configuration | |
| public class DemoConfig { | |
| @Bean | |
| @Conditional( DemoApplication.class ) | |
| public Entidad getEntidad() { | |
| return new Entidad().setNombre("Ejemplo de entidad"); | |
| } | |
| @Autowired | |
| Environment env; | |
| @Bean | |
| @Conditional( DemoApplication.class ) | |
| public LocalContainerEntityManagerFactoryBean entityManagerFactory() throws NamingException { | |
| HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); | |
| logger.info("Set EntityManager to Oracle:" + Database.ORACLE); | |
| vendorAdapter.setGenerateDdl(true); | |
| vendorAdapter.setDatabasePlatform("org.hibernate.dialect.Oracle12cDialect"); | |
| LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean(); | |
| JndiTemplate jndiTemplate = new JndiTemplate(); | |
| DataSource dataSource = null; | |
| try { | |
| dataSource = (DataSource) jndiTemplate.lookup("jdbc/ejemplo"); | |
| logger.info("DataSource: jdbc/eucWeb"); | |
| } catch( Exception ex ) { | |
| try { | |
| dataSource = (DataSource) jndiTemplate.lookup("java:jboss/datasources/ejemplo"); | |
| logger.info("DataSource: java:jboss/datasources/ejemplo"); | |
| } catch( Exception exx) { | |
| } | |
| } | |
| em.setDataSource(dataSource); | |
| em.setPackagesToScan(this.getClass().getPackage().getName()); | |
| em.setJpaVendorAdapter(vendorAdapter); | |
| em.setJpaProperties(additionalProperties()); | |
| return em; | |
| } | |
| private Properties additionalProperties() { | |
| logger.info("Set hibernate props"); | |
| Properties properties = new Properties(); | |
| properties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("spring.jpa.hibernate.ddl-auto")); | |
| properties.setProperty("hibernate.dialect", env.getProperty("spring.jpa.properties.hibernate.dialect")); | |
| properties.setProperty("hibernate.current_session_context_class", env.getProperty("spring.jpa.properties.hibernate.current_session_context_class")); | |
| properties.setProperty("hibernate.show_sql", env.getProperty("spring.jpa.show-sql")); | |
| properties.setProperty("hibernate.format_sql", env.getProperty("spring.jpa.properties.hibernate.format_sql")); | |
| properties.setProperty("hibernate.SQL","DEBUG"); | |
| properties.setProperty("hibernate.type","trace"); | |
| return properties; | |
| } | |
| @Bean | |
| @Conditional( DemoApplication.class ) | |
| public PersistenceExceptionTranslationPostProcessor exceptionTranslation() { | |
| return new PersistenceExceptionTranslationPostProcessor(); | |
| } | |
| @Bean | |
| @Conditional( DemoApplication.class ) | |
| public PlatformTransactionManager transactionManager(EntityManagerFactory emf) { | |
| JpaTransactionManager transactionManager = new JpaTransactionManager(); | |
| transactionManager.setEntityManagerFactory(emf); | |
| return transactionManager; | |
| } | |
| @Bean | |
| @Conditional( DemoApplication.class ) | |
| public DataSource dataSource() throws Exception { | |
| JndiTemplate jndiTemplate = new JndiTemplate(); | |
| DataSource dataSource = null; | |
| try { | |
| dataSource = (DataSource) jndiTemplate.lookup("jdbc/ejemplo"); | |
| logger.info("Set DataSource: jdbc/ejemplo"); | |
| } catch( Exception ex ) { | |
| try { | |
| dataSource = (DataSource) jndiTemplate.lookup("java:jboss/datasources/ejemplo"); | |
| logger.info("Set DataSource: java:jboss/datasources/ejemplo"); | |
| } catch( Exception exx) { | |
| } | |
| } | |
| return dataSource; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment