dbc

 package com.booksxgames.configuration;


import java.util.Properties;


import org.apache.commons.dbcp.BasicDataSource;

import org.hibernate.SessionFactory;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.boot.autoconfigure.domain.EntityScan;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.core.env.Environment;

import org.springframework.orm.hibernate5.HibernateTransactionManager;

import org.springframework.orm.hibernate5.LocalSessionFactoryBean;

import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;

import org.springframework.orm.jpa.LocalEntityManagerFactoryBean;

import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;

import org.springframework.transaction.annotation.EnableTransactionManagement;


@Configuration

@EnableTransactionManagement

@EntityScan("com.booksxgames.entity")


public class DBConfiguration {     


@Autowired

private Environment env;

private String [] packagesToScan = new String[] {"com.booksxgames.entity"};

@Bean

public BasicDataSource dataSource() {

BasicDataSource dataSource = new BasicDataSource();

//dataSource.setDriverClassName(env.getProperty("spring.datasource.driverClassName"));

dataSource.setUrl(env.getProperty("spring.datasource.url"));

dataSource.setUsername(env.getProperty("spring.datasource.username"));

dataSource.setPassword(env.getProperty("spring.datasource.password"));

dataSource.setTestWhileIdle(true);

dataSource.setValidationQuery("select 1");

return dataSource;

}

@Bean

public LocalSessionFactoryBean sessionFactory() {

LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();

sessionFactory.setDataSource(dataSource());

sessionFactory.setHibernateProperties(hibernateProperties());

sessionFactory.setPackagesToScan(packagesToScan);

return sessionFactory;

}

private Properties hibernateProperties() {

Properties properties = new Properties();

properties.put("hibernate.dialect", env.getProperty("hibernate.dialect"));

properties.put("hibernate.show_sql", env.getProperty("hibernate.show_sql"));

properties.put("hibernate.format_sql", env.getProperty("hibernate.format_sql"));

properties.put("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));

return properties;

}

@Bean

@Autowired

public HibernateTransactionManager txnManager(SessionFactory factory) {

HibernateTransactionManager txnManager = new HibernateTransactionManager();

txnManager.setSessionFactory(factory);

return txnManager;

}

//For JPA cofiguration

@Bean(name ="entityManagerFactory")

public LocalContainerEntityManagerFactoryBean entityManagerFactory() {

HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();

vendorAdapter.setGenerateDdl(false);

LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();

factory.setJpaProperties(hibernateProperties());

factory.setJpaVendorAdapter(vendorAdapter);

factory.setPackagesToScan(packagesToScan);

factory.setDataSource(dataSource());

return factory;

}

}


Comments

Popular posts from this blog

Redmine - Spent Time Installation

Redmine Compatibility Issues Before_filter

Redmine with Rails 5 Compatibility Issues