Guest User

Untitled

a guest
Nov 4th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. @Configuration
  2. @EnableTransactionManagement
  3. public class HibernateConfig {
  4.  
  5. @Bean
  6. public LocalContainerEntityManagerFactoryBean entityManagerF() {
  7. LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
  8. em.setDataSource(dataSource());
  9. em.setPackagesToScan(new String[] {"com.gitreporter"});
  10. JpaVendorAdapter jpaAdapter = new HibernateJpaVendorAdapter();
  11. em.setJpaVendorAdapter(jpaAdapter);
  12. em.setJpaProperties(jpaProperties());
  13.  
  14. return em;
  15. }
  16.  
  17. @Bean
  18. public PlatformTransactionManager jpaTransactionManager(EntityManagerFactory emf) {
  19. JpaTransactionManager jpaTransactionManager = new JpaTransactionManager();
  20. jpaTransactionManager.setEntityManagerFactory(emf);
  21.  
  22. return jpaTransactionManager;
  23. }
  24.  
  25. private final Properties jpaProperties() {
  26. Properties properties = new Properties();
  27. properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
  28.  
  29. return properties;
  30. }
  31.  
  32.  
  33. @Bean
  34. public DataSource dataSource() {
  35. BasicDataSource dataSource = new BasicDataSource();
  36. dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
  37. dataSource.setUrl("jdbc:mysql://localhost:3306/MyDBNAME?useSSL=false");
  38. dataSource.setUsername("username");
  39. dataSource.setPassword("password");
  40.  
  41. return dataSource;
  42. }
  43.  
  44. @Bean
  45. public LocalContainerEntityManagerFactoryBean entityManagerF() {
  46.  
  47. @Bean
  48. public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
  49.  
  50. @NoRepositoryBean
  51. public interface GenericRepository<T, ID extends Serializable> extends JpaRepository<T, ID> {
  52.  
  53. public List<T> findByAttributeContainsText(String attributeName, String text);
  54.  
  55. }
  56.  
  57. public class GenericRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRepository<T, ID>
  58. implements GenericRepository<T, ID> {
  59.  
  60. private EntityManager entityManager;
  61.  
  62. public GenericRepositoryImpl(JpaEntityInformation<T, ?> entityInformation, EntityManager entityManager) {
  63. super(entityInformation, entityManager);
  64. this.entityManager = entityManager;
  65. }
  66. }
  67.  
  68.  
  69. public interface RoleRepository extends GenericRepository<Role, Long> {
  70.  
  71. }
Add Comment
Please, Sign In to add comment