Guest User

Untitled

a guest
Apr 23rd, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.14 KB | None | 0 0
  1. @Entity
  2. @Table(name = "users")
  3. @Data
  4. public class User extends BaseEntity {
  5. private String username;
  6. private String email;
  7. private String password;
  8. private String mobile;
  9. @OneToOne
  10. @JoinColumn(name = "fk_role")
  11. private Role role;
  12. private String token;
  13.  
  14. }
  15.  
  16. @Data
  17. public class BaseEntity {
  18.  
  19. @Id
  20. @GeneratedValue(strategy = GenerationType.AUTO)
  21. @Column(name = "id")
  22. public Long id;
  23.  
  24. @Temporal(TemporalType.TIMESTAMP)
  25. @Column(name = "created_on")
  26. @CreatedDate
  27. public Date createdOn;
  28.  
  29. @Column(name = "created_by")
  30. @CreatedBy
  31. public String createdBy;
  32.  
  33. @Temporal(TemporalType.TIMESTAMP)
  34. @Column(name = "last_updated_on")
  35. @LastModifiedDate
  36. public Date lastUpdatedOn;
  37.  
  38. @Column(name = "last_updated_by")
  39. @LastModifiedBy
  40. public String lastUpdatedBy;
  41. }
  42.  
  43. package com.glaucus.fms.config;
  44.  
  45. import java.util.HashMap;
  46. import java.util.Map;
  47. import java.util.Properties;
  48.  
  49. import javax.persistence.EntityManagerFactory;
  50. import javax.sql.DataSource;
  51.  
  52. import org.hibernate.SessionFactory;
  53. import org.springframework.beans.factory.annotation.Autowired;
  54. import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
  55. import org.springframework.boot.context.properties.ConfigurationProperties;
  56. import org.springframework.context.annotation.Bean;
  57. import org.springframework.context.annotation.ComponentScan;
  58. import org.springframework.context.annotation.Configuration;
  59. import org.springframework.context.annotation.Primary;
  60. import org.springframework.context.annotation.PropertySource;
  61. import org.springframework.core.env.Environment;
  62. import org.springframework.orm.hibernate4.HibernateTransactionManager;
  63. import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
  64. import org.springframework.orm.jpa.JpaTransactionManager;
  65. import org.springframework.transaction.annotation.EnableTransactionManagement;
  66. import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
  67.  
  68. /**
  69. *
  70. * @author shahzad
  71. *
  72. */
  73. @Configuration
  74. @EnableTransactionManagement
  75. @ComponentScan({ "com.glaucus.fms.config" })
  76. @PropertySource(value = { "classpath:application.yml" })
  77. public class HibernateConfiguration extends WebMvcConfigurerAdapter {
  78. @Autowired
  79. private Environment environment;
  80.  
  81. @Bean
  82. public LocalSessionFactoryBean sessionFactory() {
  83. LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
  84. sessionFactory.setDataSource(dataSource());
  85. sessionFactory.setPackagesToScan(new String[] { "com.glaucus.fms.entity" });
  86. sessionFactory.setHibernateProperties(hibernateProperties());
  87. return sessionFactory;
  88. }
  89.  
  90. @Bean
  91. public DataSource dataSource() {
  92. Map<Object, Object> targetDataSources = new HashMap<>();
  93. DataSource clientADatasource = primaryDataSource();
  94. DataSource clientBDatasource = secondaryDataSource();
  95. DataSource clientCDatasource = thirdDataSource();
  96.  
  97. targetDataSources.put("fms", clientADatasource);
  98. targetDataSources.put("fms1", clientBDatasource);
  99. targetDataSources.put("fms2", clientCDatasource);
  100.  
  101. RoutingDataSource clientRoutingDatasource = new RoutingDataSource();
  102. clientRoutingDatasource.setTargetDataSources(targetDataSources);
  103. clientRoutingDatasource.setDefaultTargetDataSource(clientADatasource);
  104. return clientRoutingDatasource;
  105. }
  106.  
  107. // @Bean
  108. // public DataSource dataSource() {
  109. // DriverManagerDataSource dataSource = new DriverManagerDataSource();
  110. // dataSource.setDriverClassName(environment.getRequiredProperty("jdbc.driverClassName"));
  111. // dataSource.setUrl(environment.getRequiredProperty("jdbc.url"));
  112. // dataSource.setUsername(environment.getRequiredProperty("jdbc.username"));
  113. // dataSource.setPassword(environment.getRequiredProperty("jdbc.password"));
  114. // return dataSource;
  115. // }
  116.  
  117. @Bean
  118. @Primary
  119. @ConfigurationProperties(prefix = "spring.datasource")
  120. public DataSource primaryDataSource() {
  121. return DataSourceBuilder.create().build();
  122. }
  123.  
  124. @Bean
  125. @ConfigurationProperties(prefix = "spring.secondDatasource")
  126. public DataSource secondaryDataSource() {
  127. return DataSourceBuilder.create().build();
  128. }
  129.  
  130. @Bean
  131. @ConfigurationProperties(prefix = "spring.thirdDatasource")
  132. public DataSource thirdDataSource() {
  133. return DataSourceBuilder.create().build();
  134. }
  135.  
  136. private Properties hibernateProperties() {
  137. Properties properties = new Properties();
  138. properties.put("hibernate.dialect", environment.getRequiredProperty("spring.jpa.database-platform"));
  139. properties.put("hibernate.show_sql", environment.getRequiredProperty("spring.jpa.show-sql"));
  140. properties.put("hibernate.hbm2ddl.auto",
  141. environment.getRequiredProperty("spring.jpa.properties.hibernate.hbm2ddl.auto"));
  142. properties.put("hibernate.id.new_generator_mappings",
  143. environment.getRequiredProperty("spring.jpa.properties.hibernate.id.new_generator_mappings"));
  144.  
  145. return properties;
  146. }
  147.  
  148. @Bean
  149. @Autowired
  150. public HibernateTransactionManager transactionManager(SessionFactory s) {
  151. HibernateTransactionManager txManager = new HibernateTransactionManager();
  152. txManager.setSessionFactory(s);
  153. return txManager;
  154. }
  155.  
  156. @Bean
  157. @Autowired
  158. public JpaTransactionManager transactionManager(EntityManagerFactory entityManagerFactory,
  159. DataSource mainDataSource) {
  160. final JpaTransactionManager transactionManager = new JpaTransactionManager();
  161. transactionManager.setEntityManagerFactory(entityManagerFactory);
  162. transactionManager.setDataSource(mainDataSource);
  163. return transactionManager;
  164. }
  165.  
  166. }
  167.  
  168. #spring.datasource:
  169. # url: jdbc:mysql://localhost:3306/fms2?useUnicode=true&characterEncoding=utf8&useSSL=false
  170. # username: root
  171. # password: root
  172. # driver-class-name: com.mysql.jdbc.Driver
  173.  
  174. server:
  175. port: 8085
  176. spring.jpa:
  177. database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
  178. database: MYSQL
  179. show-sql: true
  180. generate-ddl: false
  181. hibernate:
  182. ddl-auto: none
  183. properties:
  184. hibernate.cache.use_second_level_cache: false
  185. hibernate.cache.use_query_cache: false
  186. hibernate.generate_statistics: true
  187. hibernate.hbm2ddl.auto: update
  188. hibernate.temp.use_jdbc_metadata_defaults: false
  189. hibernate.id.new_generator_mappings: false
  190. hibernate.cache.region.factory_class: org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory
  191. hibernate.current_session_context_class: org.springframework.orm.hibernate5.SpringSessionContext
  192.  
  193.  
  194.  
  195. spring.datasource:
  196. url: jdbc:mysql://localhost:3306/fms?useUnicode=true&characterEncoding=utf8&useSSL=false
  197. username: root
  198. password: root
  199. driver-class-name: com.mysql.jdbc.Driver
  200.  
  201.  
  202. spring.secondDatasource:
  203. url: jdbc:mysql://localhost:3306/fms1?useUnicode=true&characterEncoding=utf8&useSSL=false
  204. username: root
  205. password: root
  206. driver-class-name: com.mysql.jdbc.Driver
  207.  
  208.  
  209. spring.thirdDatasource:
  210. url: jdbc:mysql://localhost:3306/fms2?useUnicode=true&characterEncoding=utf8&useSSL=false
  211. username: root
  212. password: root
  213. driver-class-name: com.mysql.jdbc.Driver
Add Comment
Please, Sign In to add comment