Package ua.ck.geekhub

Source Code of ua.ck.geekhub.HibernateConfig

package ua.ck.geekhub;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.hibernate4.HibernateTransactionManager;
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.util.Properties;

/**
* @author vladimirb
* @since 3/12/14
*/
@Configuration
@EnableTransactionManagement
@PropertySource("classpath:hibernate.properties")
public class HibernateConfig {

  static final String DRIVER_CLASS_NAME = "com.mysql.jdbc.Driver";

  static final Properties hibernateProperties = new Properties() {
    {
      setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
      setProperty("hibernate.hbm2ddl.auto", "update");
      setProperty("hibernate.show_sql", "true");
      setProperty("hibernate.current_session_context_class", "org.springframework.orm.hibernate4.SpringSessionContext");
    }
  };

  @Value("${connection.url}")
  String dbUrl;

  @Value("${connection.username}")
  String dbUser;

  @Value("${connection.password}")
  String dbPass;

  @Bean
  public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
    return new PropertySourcesPlaceholderConfigurer();
  }

  @Bean
  public LocalSessionFactoryBean sessionFactory() {
    final LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
    sessionFactory.setDataSource(dataSource());
    sessionFactory.setPackagesToScan("ua.ck.geekhub.entity");
    sessionFactory.setHibernateProperties( hibernateProperties );
    return sessionFactory;
  }

  @Bean
  public HibernateTransactionManager transactionManager() {
    HibernateTransactionManager txManager = new HibernateTransactionManager();
    txManager.setSessionFactory(sessionFactory().getObject());
    return txManager;
  }

  @Bean
  public DataSource dataSource() {
    DriverManagerDataSource dataSource = new DriverManagerDataSource(dbUrl, dbUser, dbPass);
    dataSource.setDriverClassName( DRIVER_CLASS_NAME );
    return dataSource;
  }
}
TOP

Related Classes of ua.ck.geekhub.HibernateConfig

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.