Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Entity
- @Table(name = "users")
- @Data
- public class User extends BaseEntity {
- private String username;
- private String email;
- private String password;
- private String mobile;
- @OneToOne
- @JoinColumn(name = "fk_role")
- private Role role;
- private String token;
- }
- @Data
- public class BaseEntity {
- @Id
- @GeneratedValue(strategy = GenerationType.AUTO)
- @Column(name = "id")
- public Long id;
- @Temporal(TemporalType.TIMESTAMP)
- @Column(name = "created_on")
- @CreatedDate
- public Date createdOn;
- @Column(name = "created_by")
- @CreatedBy
- public String createdBy;
- @Temporal(TemporalType.TIMESTAMP)
- @Column(name = "last_updated_on")
- @LastModifiedDate
- public Date lastUpdatedOn;
- @Column(name = "last_updated_by")
- @LastModifiedBy
- public String lastUpdatedBy;
- }
- package com.glaucus.fms.config;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.Properties;
- import javax.persistence.EntityManagerFactory;
- import javax.sql.DataSource;
- import org.hibernate.SessionFactory;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
- import org.springframework.boot.context.properties.ConfigurationProperties;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.ComponentScan;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.context.annotation.Primary;
- import org.springframework.context.annotation.PropertySource;
- import org.springframework.core.env.Environment;
- import org.springframework.orm.hibernate4.HibernateTransactionManager;
- import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
- import org.springframework.orm.jpa.JpaTransactionManager;
- import org.springframework.transaction.annotation.EnableTransactionManagement;
- import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
- /**
- *
- * @author shahzad
- *
- */
- @Configuration
- @EnableTransactionManagement
- @ComponentScan({ "com.glaucus.fms.config" })
- @PropertySource(value = { "classpath:application.yml" })
- public class HibernateConfiguration extends WebMvcConfigurerAdapter {
- @Autowired
- private Environment environment;
- @Bean
- public LocalSessionFactoryBean sessionFactory() {
- LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
- sessionFactory.setDataSource(dataSource());
- sessionFactory.setPackagesToScan(new String[] { "com.glaucus.fms.entity" });
- sessionFactory.setHibernateProperties(hibernateProperties());
- return sessionFactory;
- }
- @Bean
- public DataSource dataSource() {
- Map<Object, Object> targetDataSources = new HashMap<>();
- DataSource clientADatasource = primaryDataSource();
- DataSource clientBDatasource = secondaryDataSource();
- DataSource clientCDatasource = thirdDataSource();
- targetDataSources.put("fms", clientADatasource);
- targetDataSources.put("fms1", clientBDatasource);
- targetDataSources.put("fms2", clientCDatasource);
- RoutingDataSource clientRoutingDatasource = new RoutingDataSource();
- clientRoutingDatasource.setTargetDataSources(targetDataSources);
- clientRoutingDatasource.setDefaultTargetDataSource(clientADatasource);
- return clientRoutingDatasource;
- }
- // @Bean
- // public DataSource dataSource() {
- // DriverManagerDataSource dataSource = new DriverManagerDataSource();
- // dataSource.setDriverClassName(environment.getRequiredProperty("jdbc.driverClassName"));
- // dataSource.setUrl(environment.getRequiredProperty("jdbc.url"));
- // dataSource.setUsername(environment.getRequiredProperty("jdbc.username"));
- // dataSource.setPassword(environment.getRequiredProperty("jdbc.password"));
- // return dataSource;
- // }
- @Bean
- @Primary
- @ConfigurationProperties(prefix = "spring.datasource")
- public DataSource primaryDataSource() {
- return DataSourceBuilder.create().build();
- }
- @Bean
- @ConfigurationProperties(prefix = "spring.secondDatasource")
- public DataSource secondaryDataSource() {
- return DataSourceBuilder.create().build();
- }
- @Bean
- @ConfigurationProperties(prefix = "spring.thirdDatasource")
- public DataSource thirdDataSource() {
- return DataSourceBuilder.create().build();
- }
- private Properties hibernateProperties() {
- Properties properties = new Properties();
- properties.put("hibernate.dialect", environment.getRequiredProperty("spring.jpa.database-platform"));
- properties.put("hibernate.show_sql", environment.getRequiredProperty("spring.jpa.show-sql"));
- properties.put("hibernate.hbm2ddl.auto",
- environment.getRequiredProperty("spring.jpa.properties.hibernate.hbm2ddl.auto"));
- properties.put("hibernate.id.new_generator_mappings",
- environment.getRequiredProperty("spring.jpa.properties.hibernate.id.new_generator_mappings"));
- return properties;
- }
- @Bean
- @Autowired
- public HibernateTransactionManager transactionManager(SessionFactory s) {
- HibernateTransactionManager txManager = new HibernateTransactionManager();
- txManager.setSessionFactory(s);
- return txManager;
- }
- @Bean
- @Autowired
- public JpaTransactionManager transactionManager(EntityManagerFactory entityManagerFactory,
- DataSource mainDataSource) {
- final JpaTransactionManager transactionManager = new JpaTransactionManager();
- transactionManager.setEntityManagerFactory(entityManagerFactory);
- transactionManager.setDataSource(mainDataSource);
- return transactionManager;
- }
- }
- #spring.datasource:
- # url: jdbc:mysql://localhost:3306/fms2?useUnicode=true&characterEncoding=utf8&useSSL=false
- # username: root
- # password: root
- # driver-class-name: com.mysql.jdbc.Driver
- server:
- port: 8085
- spring.jpa:
- database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
- database: MYSQL
- show-sql: true
- generate-ddl: false
- hibernate:
- ddl-auto: none
- properties:
- hibernate.cache.use_second_level_cache: false
- hibernate.cache.use_query_cache: false
- hibernate.generate_statistics: true
- hibernate.hbm2ddl.auto: update
- hibernate.temp.use_jdbc_metadata_defaults: false
- hibernate.id.new_generator_mappings: false
- hibernate.cache.region.factory_class: org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory
- hibernate.current_session_context_class: org.springframework.orm.hibernate5.SpringSessionContext
- spring.datasource:
- url: jdbc:mysql://localhost:3306/fms?useUnicode=true&characterEncoding=utf8&useSSL=false
- username: root
- password: root
- driver-class-name: com.mysql.jdbc.Driver
- spring.secondDatasource:
- url: jdbc:mysql://localhost:3306/fms1?useUnicode=true&characterEncoding=utf8&useSSL=false
- username: root
- password: root
- driver-class-name: com.mysql.jdbc.Driver
- spring.thirdDatasource:
- url: jdbc:mysql://localhost:3306/fms2?useUnicode=true&characterEncoding=utf8&useSSL=false
- username: root
- password: root
- driver-class-name: com.mysql.jdbc.Driver
Add Comment
Please, Sign In to add comment