Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Spring Data
- //add this to your MyThingApplication.java file
- @Autowired
- Environment env;
- @Autowired
- private DataSource dataSource;
- @Autowired
- private LocalContainerEntityManagerFactoryBean entityManagerFactory;
- /**
- * Declare the JPA entity manager factory.
- */
- @Bean
- public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
- LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean();
- entityManagerFactory.setDataSource(dataSource);
- // Classpath scanning of @Component, @Service, etc annotated class
- entityManagerFactory.setPackagesToScan(env.getProperty("entitymanager.packagesToScan"));
- // Vendor adapter
- HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
- entityManagerFactory.setJpaVendorAdapter(vendorAdapter);
- // Hibernate properties
- Properties additionalProperties = new Properties();
- additionalProperties.put("hibernate.dialect", env.getProperty("hibernate.dialect"));
- additionalProperties.put("hibernate.show_sql", env.getProperty("hibernate.show_sql"));
- additionalProperties.put("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
- entityManagerFactory.setJpaProperties(additionalProperties);
- return entityManagerFactory;
- }
- /**
- * Declare the transaction manager.
- */
- @Bean
- public JpaTransactionManager transactionManager() {
- JpaTransactionManager transactionManager = new JpaTransactionManager();
- transactionManager.setEntityManagerFactory(entityManagerFactory.getObject());
- return transactionManager;
- }
- /**
- * PersistenceExceptionTranslationPostProcessor is a bean post processor
- * which adds an advisor to any bean annotated with Repository so that any
- * platform-specific exceptions are caught and then rethrown as one Spring's
- * unchecked data access exceptions (i.e. a subclass of
- * DataAccessException).
- */
- @Bean
- public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
- return new PersistenceExceptionTranslationPostProcessor();
- }
- //application.properties
- spring.datasource.url=jdbc:mysql://localhost/twitterbot
- spring.datasource.username=root
- spring.datasource.password=pass
- spring.datasource.driver-class-name=com.mysql.jdbc.Driver
- hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
- hibernate.show_sql=false
- #change to create for adding new entities
- hibernate.hbm2ddl.auto=update #set to create on your first run, change to update so it doesn't wipe your db each run
- entitymanager.packagesToScan=com.mypackage.entity #create an entity package in your project even if you don't want to use hibernate
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement