Advertisement
Guest User

Untitled

a guest
Jun 4th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.40 KB | None | 0 0
  1. package net.mainseek.wlw.config;
  2.  
  3. import org.apache.commons.dbcp.BasicDataSource;
  4. import org.apache.log4j.Logger;
  5. import org.hibernate.SessionFactory;
  6. import org.springframework.beans.factory.annotation.Value;
  7. import org.springframework.context.annotation.Bean;
  8. import org.springframework.context.annotation.Configuration;
  9. import org.springframework.orm.hibernate3.HibernateTransactionManager;
  10. import org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean;
  11.  
  12. import javax.sql.DataSource;
  13. import java.util.Properties;
  14.  
  15. @Configuration
  16. public class HibernateConfig {
  17.  
  18.     private static final Logger LOG = Logger.getLogger(HibernateConfig.class);
  19.  
  20.     private static final String[] PACKAGES_TO_SCAN = {
  21.             "net.mainseek.wlw.user.entity",
  22.             "net.mainseek.wlw.location.entity",
  23.             "net.mainseek.wlw.company.entity"
  24.     };
  25.  
  26.     @Bean
  27.     public SessionFactory sessionFactory(DataSource dataSource,
  28.                                          @Value("${hibernate.dialect}") String dialect,
  29.                                          @Value("${hibernate.hbm2ddl.auto}") String hbm2ddl) throws Exception {
  30.  
  31.         AnnotationSessionFactoryBean factory = new AnnotationSessionFactoryBean();
  32.         factory.setDataSource(dataSource);
  33.         factory.setPackagesToScan(PACKAGES_TO_SCAN);
  34.         factory.setHibernateProperties(createHibernateProperties(dialect, hbm2ddl));
  35.         factory.afterPropertiesSet();
  36.         return factory.getObject();
  37.     }
  38.  
  39.     private Properties createHibernateProperties(String dialect, String hbm2ddl) {
  40.         Properties properties = new Properties();
  41.         properties.setProperty("hibernate.dialect", dialect);
  42.         properties.setProperty("hibernate.hbm2ddl.auto", hbm2ddl);
  43.         return properties;
  44.     }
  45.  
  46.     @Bean
  47.     public DataSource dataSource(@Value("${jdbc.driver}") String driverClassName,
  48.                                  @Value("${jdbc.url}") String url,
  49.                                  @Value("${jdbc.username}") String username,
  50.                                  @Value("${jdbc.password}") String password) {
  51.         BasicDataSource dataSource = new BasicDataSource();
  52.         LOG.debug("driverClassName = " + driverClassName);
  53.         LOG.debug("url = " + url);
  54.         LOG.debug("username = " + username);
  55.         LOG.debug("password = " + password);
  56.         dataSource.setDriverClassName(driverClassName);
  57.         dataSource.setUrl(url);
  58.         dataSource.setUsername(username);
  59.         dataSource.setPassword(password);
  60.         return dataSource;
  61.     }
  62.  
  63.     @Bean
  64.     public HibernateTransactionManager transactionManager(SessionFactory sessionFactory) {
  65.         return new HibernateTransactionManager(sessionFactory);
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement