发生意外错误:目前没有可用于当前线程的实际事务的EntityManager-无法可靠地处理“持久”调用

时间:2018-11-04 08:22:25

标签: java spring hibernate jpa persistence.xml

我正在尝试使用Hibernate和JPA设置Spring,但是当尝试持久化对象时,会出现一个异常,例如“没有EntityManager可以用于当前线程的实际事务-无法可靠地处理'persist'调用”。 我仅在引用一些现有建议后才在服务级别添加@ Transactional.Still我也遇到了同样的异常。 下面是spring配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
      http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
      http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
      http://www.springframework.org/schema/data/repository http://www.springframework.org/schema/data/repository/spring-repository-1.5.xsd">

    <context:component-scan base-package="com.wdw.guestaccesscontrol" />

    <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" lazy-init="false"/>

    <jpa:repositories base-package="com.wdw.repositories" 
        query-lookup-strategy="create-if-not-found" entity-manager-factory-ref="myEntityManagerFactory" />  

    <bean id="jpaTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="myEntityManagerFactory" />
        <property name="defaultTimeout" value="295"/>
        <qualifier value="jpaTransactionManager"/>
    </bean>

    <bean id="sequenceGidHelper" class="com.wdpr.dataaccess.helper.SequenceGidHelper" factory-method="getInstance" />
    <bean id="generatedIdHelper" class="com.wdpr.dataaccess.helper.GeneratedIdHelper" factory-method="getInstance" />

    <tx:annotation-driven transaction-manager="jpaTransactionManager" proxy-target-class="true" />
    <context:annotation-config />

    <bean id="myEntityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
        lazy-init="false">
        <property name="persistenceUnitName" value="myPersistenceUnit" />
        <property name="dataSource" ref="jpaDataSource" />
        <property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml" />       
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
        </property>
        <property name="persistenceProviderClass" value="org.hibernate.ejb.HibernatePersistence"></property>
        <property name="loadTimeWeaver">
            <bean
                class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
        </property>
    </bean>

    <bean id="jpaDataSource" class="org.apache.tomcat.jdbc.pool.DataSource" destroy-method="close">
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
        <property name="url" value="${oracledb.jdbc.url}" />
        <property name="username" value="${oracledb.jdbc.username}" />
        <property name="password" value="${oracledb.jdbc.password}" />
        <property name="validationQuery" value="${oracledb.jdbc.validationQuery:SELECT 1 FROM DUAL}" />
        <property name="testOnBorrow" value="${oracledb.jdbc.testOnBorrow:true}" />
        <property name="maxActive" value="${oracledb.jdbc.maxActive:300}" />
        <property name="maxIdle" value="${oracledb.jdbc.maxIdle:60}" />
        <property name="minIdle" value="${oracledb.jdbc.minIdle:10}" />
        <property name="maxWait" value="${oracledb.jdbc.maxWait:10000}" />
        <property name="maxAge" value="${oracledb.jdbc.maxAge:0}" />
        <property name="testWhileIdle" value="${oracledb.jdbc.testWhileIdle:true}" />
        <property name="jmxEnabled" value="${oracledb.jdbc.jmxEnabled:true}" />
        <property name="validationInterval" value="${oracledb.jdbc.validationInterval:3000}" />
    </bean>

   <bean id="myRepoProvider"
        class="com.wdw.guestaccesscontrol.provider.GuestAccessControlRepoProvider" factory-method="getInstance"/>

</beans>

Persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
        http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
    version="2.0">

    <persistence-unit name="myPersistenceUnit"
        transaction-type="RESOURCE_LOCAL">
            <mapping-file>META-INF/gac-orm.xml</mapping-file>
        <properties>
            <property name="hibernate.archive.autodetection" value="class" />
            <property name="hibernate.current_session_context_class"
                value="jta" />
            <property name="hibernate.format_sql" value="false" />
            <property name="hibernate.show_sql" value="false" />
            <property name="hibernate.use_outer_join" value="true" />
            <property name="hibernate.cache.use_second_level_cache"
                value="false" />
            <property name="hibernate.transaction.flush_before_completion"
                value="true" />
            <property name="hibernate.default_schema" value="GUESTSCHEMA" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />
            <property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy" />
            <property name="hibernate.jdbc.factory_class" value="com.wdpr.dataaccess.CustomHibernateBatcherFactory" />
            <property name="hibernate.enable_lazy_load_no_trans" value="true" /> 
            <property name="javax.persistence.query.timeout" value="50000" />
        </properties>
        <validation-mode>NONE</validation-mode>
    </persistence-unit>
</persistence>

orm.xml:

<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm orm_2_0.xsd"
    version="1.0">
    <persistence-unit-metadata>
                <persistence-unit-defaults>
                    <entity-listeners>
                        <entity-listener
                        class="com.wdpr.dataaccess.jpa.EntityAuditListener" />
                    </entity-listeners>
                </persistence-unit-defaults>
    </persistence-unit-metadata>
    <package>com.wdw.guestaccesscontrol.entities</package>

    <entity class="Entitlement">
      </entity>   


</entity-mappings>

服务Impl:

@Transactional(transactionManager = "jpaTransactionManager", propagation = Propagation.REQUIRED, rollbackFor = {
            com.service.exception.ServiceException.class })
    public List<CreateResponse> create(final List<CreateRequest> request,
            final ServiceContext serviceContext) throws ServiceException, RemoteException {
        return guestAccessController.create(request, serviceContext);
    }

尝试了较早提供的其他解决方案,但未能成功。有人可以让我知道我在这里还缺少什么

0 个答案:

没有答案