通过配置类创建transactionmanager和实体管理器

时间:2014-03-28 06:42:15

标签: java spring jpa spring-data-jpa

我是相对较新的春天,并希望用mysql实现spring jpa。下面提到的是我的配置......

<beans ...
<context:component-scan base-package="com.pg" />

<mvc:annotation-driven />

<tx:annotation-driven/>

<aop:aspectj-autoproxy />

<jpa:repositories base-package="com.pg.dao" /> ...

我在下面列出了用于创建数据源,transactionmanager和entitymanager的配置类。

package com.pg.conf;

import java.util.Properties;

import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import javax.transaction.TransactionManager;

import org.apache.tomcat.dbcp.dbcp.BasicDataSource;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;

@Configuration
public class JPAConfiguration {

    @Bean
    public DataSource dataSource(){
        BasicDataSource datasource = new BasicDataSource();
        datasource.setDriverClassName(com.mysql.jdbc.Driver.class.getName());
        datasource.setUrl("jdbc:mydql://localhost:3306/pg1");
        datasource.setUsername("root");
        datasource.setPassword("root");
        return datasource;
    }

    @Bean
    public **FactoryBean<EntityManagerFactory>** entityManagerFactory(){
        LocalContainerEntityManagerFactoryBean containerEntityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
        containerEntityManagerFactoryBean.setDataSource(dataSource());
        HibernateJpaVendorAdapter adaptor = new HibernateJpaVendorAdapter();
        containerEntityManagerFactoryBean.setJpaVendorAdapter(adaptor);
        containerEntityManagerFactoryBean.setPackagesToScan("com.pg.entity");
        Properties props = new Properties();        
        props.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
        props.setProperty("hibernate.show_sql", "true");
        containerEntityManagerFactoryBean.setJpaProperties(props);
        return containerEntityManagerFactoryBean;
    }

    @Bean
    public TransactionManager transactionManager(){
        JpaTransactionManager jpaTransactionManager = new JpaTransactionManager();
        jpaTransactionManager.setDataSource(dataSource());
        **jpaTransactionManager.setEntityManagerFactory(entityManagerFactory());**
        return transactionManager();
    }

}

我的行 jpaTransactionManager.setEntityManagerFactory(entityManagerFactory());

我认为entitymanager方法的返回类型不正确...即使它没有显示错误....

感谢任何帮助。

2 个答案:

答案 0 :(得分:4)

首先阅读Spring参考指南并阅读FactoryBeans

然后相应地更改您的代码。

@Bean
public TransactionManager transactionManager(EntityManagerFactory entityManagerFactory){
    JpaTransactionManager jpaTransactionManager = new JpaTransactionManager();
    jpaTransactionManager.setDataSource(dataSource());
    jpaTransactionManager.setEntityManagerFactory(entityManagerFactory);
    return jpaTransactionManager;
}

答案 1 :(得分:0)

替换

jpaTransactionManager.setEntityManagerFactory(entityManagerFactory());

使用

jpaTransactionManager.setEntityManagerFactory((EntityManagerFactory) entityManagerFactory());