daily pastebin goal
63%
SHARE
TWEET

Untitled

a guest Mar 17th, 2018 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. @Configuration
  2. @EnableTransactionManagement
  3. @EnableJpaRepositories("<where_repos_are_found>")
  4. public class DatabaseConfiguration {
  5.  
  6.     private static final String[] PACKAGES_TO_SCAN = { "<packages_locations>" };
  7.  
  8.     private static final String PROPERTY_NAME_DATABASE_PASSWORD = "spring.datasource.password";
  9.     private static final String PROPERTY_NAME_DATABASE_URL = "spring.datasource.url";
  10.     private static final String PROPERTY_NAME_DATABASE_USERNAME = "spring.datasource.username";
  11.     private static final String PROPERTY_NAME_HIBERNATE_DIALECT = "hibernate.dialect";
  12.  
  13.     private static final String PROPERTY_NAME_HIBERNATE_DATABASE_DRIVER = "hibernate.connection.driver_class";
  14.     private static final String PROPERTY_NAME_HIBERNATE_DDL = "hibernate.hbm2ddl.auto";
  15.     private static final String PROPERTY_NAME_HIBERNATE_SHOW_SQL = "hibernate.show_sql";
  16.     private static final String PROPERTY_NAME_HIBERNATE_FORMAT_SQL = "hibernate.format_sql";
  17.     private static final String PROPERTY_NAME_HIBERNATE_USE_SQL_COMMENTS = "hibernate.use_sql_comments";
  18.  
  19.     private Environment env;
  20.  
  21.     @Autowired
  22.     public DatabaseConfiguration(Environment env) { this.env = env; }
  23.  
  24.     @Bean
  25.     public DataSource getDatasource() {
  26.         DriverManagerDataSource dataSource = new DriverManagerDataSource();
  27.         dataSource.setUrl(env.getRequiredProperty(PROPERTY_NAME_DATABASE_URL));
  28.         dataSource.setUsername(env.getRequiredProperty(PROPERTY_NAME_DATABASE_USERNAME));
  29.         dataSource.setPassword(env.getRequiredProperty(PROPERTY_NAME_DATABASE_PASSWORD));
  30.         return dataSource;
  31.     }
  32.  
  33.     private Properties getHibernateProperties() {
  34.         Properties properties = new Properties();
  35.         properties.put(PROPERTY_NAME_HIBERNATE_DATABASE_DRIVER, env.getRequiredProperty(PROPERTY_NAME_HIBERNATE_DATABASE_DRIVER));
  36.         properties.put(PROPERTY_NAME_HIBERNATE_DIALECT, env.getProperty(PROPERTY_NAME_HIBERNATE_DIALECT));
  37.         properties.put(PROPERTY_NAME_HIBERNATE_SHOW_SQL, env.getProperty(PROPERTY_NAME_HIBERNATE_SHOW_SQL));
  38.         properties.put(PROPERTY_NAME_HIBERNATE_FORMAT_SQL, env.getProperty(PROPERTY_NAME_HIBERNATE_FORMAT_SQL));
  39.         properties.put(PROPERTY_NAME_HIBERNATE_USE_SQL_COMMENTS, env.getProperty(PROPERTY_NAME_HIBERNATE_USE_SQL_COMMENTS));
  40.         properties.put(PROPERTY_NAME_HIBERNATE_DDL, env.getRequiredProperty(PROPERTY_NAME_HIBERNATE_DDL));
  41.         return properties;
  42.     }
  43.  
  44.     @Bean(name = "entityManagerFactory")
  45.     public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean() {
  46.         LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
  47.         entityManagerFactoryBean.setDataSource(getDatasource());
  48.         entityManagerFactoryBean.setPackagesToScan(PACKAGES_TO_SCAN);
  49.         JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
  50.         entityManagerFactoryBean.setJpaVendorAdapter(vendorAdapter);
  51.         entityManagerFactoryBean.setJpaProperties(getHibernateProperties());
  52.         return entityManagerFactoryBean;
  53.     }
  54.  
  55.     @Bean
  56.     public JpaTransactionManager transactionManager() {
  57.         JpaTransactionManager transactionManager = new JpaTransactionManager();
  58.         transactionManager.setEntityManagerFactory(entityManagerFactoryBean().getObject());
  59.         return transactionManager;
  60.     }
  61.  
  62. }
  63.    
  64. hibernate.hbm2ddl.auto=create
  65. hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
  66.  
  67. hibernate.show_sql=true
  68. hibernate.format_sql=true
  69. hibernate.use_sql_comments=true
  70. hibernate.connection.driver_class=com.mysql.jdbc.Driver
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top