net.kamhon.ieagle.function.config.JpaAppConfig.java Source code

Java tutorial

Introduction

Here is the source code for net.kamhon.ieagle.function.config.JpaAppConfig.java

Source

/*
 * Copyright 2012 Eng Kam Hon (kamhon@gmail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.kamhon.ieagle.function.config;

import java.util.Properties;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.Database;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.support.ResourceTransactionManager;

@Configuration
public class JpaAppConfig {
    @Autowired
    protected Environment env;

    @Autowired
    protected DataSource dataSource;

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
        LocalContainerEntityManagerFactoryBean entityManager = new LocalContainerEntityManagerFactoryBean();
        entityManager.setDataSource(dataSource);
        entityManager.setLoadTimeWeaver(new InstrumentationLoadTimeWeaver());

        HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
        jpaVendorAdapter.setShowSql(env.getProperty("hibernate.show_sql", Boolean.class));

        String generateDdlMode = env.getProperty("hibernate.hbm2ddl.auto");
        boolean isGenerateDdl = generateDdlMode != null && !"validate".equalsIgnoreCase(generateDdlMode);
        jpaVendorAdapter.setGenerateDdl(isGenerateDdl);

        jpaVendorAdapter.setDatabase(Database.valueOf(env.getProperty("jpa.vendor")));
        entityManager.setJpaVendorAdapter(jpaVendorAdapter);

        // hibernate tuning
        Properties prop = new Properties();
        prop.put("hibernate.max_fetch_depth", "3");
        prop.put("hibernate.jdbc.fetch_size", "50");
        prop.put("hibernate.jdbc.batch_size", "10");
        entityManager.setJpaProperties(prop);

        // entityManager.setPersistenceUnitName("persistenceUnit");

        return entityManager;
    }

    @Bean
    public ResourceTransactionManager transactionManager() {
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
        transactionManager.setDataSource(dataSource);
        return transactionManager;
    }

    /*@Bean
    public ProxyFactoryBean genericDao() {
    JpaDao<Object> jpaDao = new JpaDao<Object>();
    jpaDao.setEntityManagerFactory(entityManagerFactory().getObject());
        
    ProxyFactoryBean proxy = new ProxyFactoryBean();
    proxy.setTarget(jpaDao);
    proxy.setInterceptorNames(new String[] { "appDetailsBaseAdvice" });
    return proxy;
    }*/
}