Spring + HIBERNATE:没有实体经理 - >无法可靠地处理“持久”调用

时间:2018-03-20 16:00:08

标签: java hibernate spring-data entitymanager spring-transactions

我知道这个问题已经在很大程度上进行了讨论,但这里提出的其他主题解决方案都没有真正有用。

上下文:在工作中我们只是在我们的spring项目中设置hibernate。该项目已经很老了,所以我们正在混合使用XML和Java配置。

尝试保留对象时出现以下错误:

javax.persistence.TransactionRequiredException: No EntityManager with actual transaction available for current thread - cannot reliably process 'persist' call

我的web.xml有

<context-param>
        <param-name>contextClass</param-name>
        <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
    </context-param>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>com.xxx.y.configuration.AppConfig</param-value>
    </context-param>

我的AppConfig.java

@Configuration
@Import({ JpaConfig.class })
@ImportResource( { "/WEB-INF/config/application-context-spring.xml", "/WEB-INF/config/application-wscontext-service.xml" })
@ComponentScan(basePackages={"com.xxx.y.*"}, excludeFilters = @Filter(type = FilterType.ANNOTATION, value = Configuration.class))
public class AppConfig {

    @Bean
    public DozerBeanMapper dozerBeanMapper() {
        return new DozerBeanMapper();
    }
}

我的JpaConfig.java

@Configuration
@EnableTransactionManagement 
@EnableJpaRepositories(basePackages = {"com.xxx.y.rest.dao.impl"}, entityManagerFactoryRef = "entityManagerFactory", transactionManagerRef = "transactionManager")
@ComponentScan(basePackages={"com.xxx.y.rest"}, excludeFilters = @Filter(type = FilterType.ANNOTATION, value = Configuration.class))
public class JpaConfig {

    @PersistenceContext(unitName = "PersistenceContext")
    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() throws NamingException {
        LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean();
        entityManagerFactory.setDataSource(dataSource());
        entityManagerFactory.setPersistenceUnitName("PersistenceContext");
        entityManagerFactory.setPackagesToScan(new String[] { "com.xxx.y.entity" });
        entityManagerFactory.setJpaVendorAdapter(jpaAdapter());
        entityManagerFactory.setJpaProperties(additionalProperties());

        return entityManagerFactory;
    }     

    @Bean
    public DataSource dataSource() throws NamingException {   
        return (DataSource) new JndiTemplate().lookup("java:comp/env/jdbc/XYZ");
    }

    @Bean
    public PlatformTransactionManager transactionManager(EntityManagerFactory emf){
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(emf);
        return transactionManager;
    }

    Properties additionalProperties() {
        Properties properties = new Properties();
        properties.setProperty("hibernate.dialect", org.hibernate.dialect.Oracle10gDialect.class.getName());
        return properties;
    }

    @Bean
    public String jpaDialect() {
        return org.hibernate.dialect.Oracle10gDialect.class.getName();
    }

    @Bean
    public HibernateJpaVendorAdapter jpaAdapter() {
        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        vendorAdapter.setDatabase(Database.ORACLE);
        vendorAdapter.setDatabasePlatform(jpaDialect());
        vendorAdapter.setGenerateDdl(true);
        vendorAdapter.setShowSql(true);
        return vendorAdapter;
    }   
}

这是DAO

@Repository
public class DAOImpl extends CommonDAO implements DAO{

    @PersistenceContext(unitName = "PersistenceContext")
    EntityManager entityManager;

    @Override
    @Transactional
    public void create(StationFare fare) {
            entityManager.persist(fare);
    }

创建方法是导致此问题的方法。

这就是我们在application-context-spring.xml

中的内容
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-2.5.xsd 
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-2.5.xsd 
        http://www.springframework.org/schema/jee 
        http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">


        <!-- Enable the annotation usage (bean injection for instance) -->
        <context:annotation-config />

</beans>

旧xml配置中的一些遗产,不确定是否仍然需要。

所以,就是这样。 我想知道我的@ComponentScan注释是否有正确的路径。我尝试了所有这些,所以如果有人能提供帮助我会很高兴,如果需要,可以毫不犹豫地询问更多细节。 非常感谢!

0 个答案:

没有答案