Advertisement
Guest User

Untitled

a guest
Dec 27th, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.65 KB | None | 0 0
  1. //Spring Data
  2.  
  3. //add this to your MyThingApplication.java file
  4.  
  5.  
  6.  
  7. @Autowired
  8. Environment env;
  9.  
  10. @Autowired
  11. private DataSource dataSource;
  12.  
  13. @Autowired
  14. private LocalContainerEntityManagerFactoryBean entityManagerFactory;
  15.  
  16. /**
  17. * Declare the JPA entity manager factory.
  18. */
  19. @Bean
  20. public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
  21. LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean();
  22.  
  23. entityManagerFactory.setDataSource(dataSource);
  24.  
  25. // Classpath scanning of @Component, @Service, etc annotated class
  26. entityManagerFactory.setPackagesToScan(env.getProperty("entitymanager.packagesToScan"));
  27.  
  28. // Vendor adapter
  29. HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
  30. entityManagerFactory.setJpaVendorAdapter(vendorAdapter);
  31.  
  32. // Hibernate properties
  33. Properties additionalProperties = new Properties();
  34. additionalProperties.put("hibernate.dialect", env.getProperty("hibernate.dialect"));
  35. additionalProperties.put("hibernate.show_sql", env.getProperty("hibernate.show_sql"));
  36. additionalProperties.put("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
  37. entityManagerFactory.setJpaProperties(additionalProperties);
  38.  
  39. return entityManagerFactory;
  40. }
  41.  
  42. /**
  43. * Declare the transaction manager.
  44. */
  45. @Bean
  46. public JpaTransactionManager transactionManager() {
  47. JpaTransactionManager transactionManager = new JpaTransactionManager();
  48. transactionManager.setEntityManagerFactory(entityManagerFactory.getObject());
  49. return transactionManager;
  50. }
  51.  
  52. /**
  53. * PersistenceExceptionTranslationPostProcessor is a bean post processor
  54. * which adds an advisor to any bean annotated with Repository so that any
  55. * platform-specific exceptions are caught and then rethrown as one Spring's
  56. * unchecked data access exceptions (i.e. a subclass of
  57. * DataAccessException).
  58. */
  59. @Bean
  60. public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
  61. return new PersistenceExceptionTranslationPostProcessor();
  62. }
  63.  
  64.  
  65. //application.properties
  66.  
  67.  
  68. spring.datasource.url=jdbc:mysql://localhost/twitterbot
  69. spring.datasource.username=root
  70. spring.datasource.password=pass
  71. spring.datasource.driver-class-name=com.mysql.jdbc.Driver
  72.  
  73. hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
  74. hibernate.show_sql=false
  75.  
  76. #change to create for adding new entities
  77. hibernate.hbm2ddl.auto=update #set to create on your first run, change to update so it doesn't wipe your db each run
  78.  
  79. 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