javax.persistence.TransactionRequiredException:没有可用于当前线程的实际事务的EntityManager无法可靠地处理

时间:2017-12-11 10:30:52

标签: spring hibernate jpa javax

我正在尝试将Spring从4.0.6升级到4.3.9版本。在这样做的过程中,我收到以下错误“ javax.persistence.TransactionRequiredException:没有实体事务可用于当前线程的EntityManager ”。整个代码工作得很早,但我只是因为库升级而得到这个错误。在我的项目中使用的Hibernate版本是4.3.7。

这是我的applicationcontext.xml

<beans default-autowire="byName"
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:jaxws="http://cxf.apache.org/jaxws"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
                        http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">

    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="persistenceUnitName" value="premier" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="showSql" value="false" />
            </bean>
        </property>
    </bean>

    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
        <property name="url" value="jdbc:oracle:thin:@***:1521:PSPRODDB" />
        <property name="username" value="**" />
        <property name="password" value="*****" />
    </bean>

    <!-- entity manager configuration -->
    <!--<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />-->


    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager" />

    <!-- externals -->
    <bean id="mapperFactory" class="org.dozer.spring.DozerBeanMapperFactoryBean">
        <property name="mappingFiles" value="classpath*:dozerBeanMapping.xml" />
    </bean>

</beans>

我的实体类

@SuppressWarnings("serial")
@Entity
@Table(name = "ROLES")
@NamedQueries({
      @NamedQuery(name = "RoleModuleMO.findById",        query = "SELECT rolemodule FROM RoleModuleMO rolemodule WHERE rolemodule.id =:id");    
@Proxy(lazy = false)
@EntityListeners(value = SimpleAuditListener.class)

public class RoleModuleMO implements Serializable, SimpleAuditable {
    public RoleModuleMO () {
    }

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "ROLEMODULE_ID_SQ")
    @SequenceGenerator(name = "ROLEMODULE_ID_SQ", sequenceName = "ROLEMODULE_ID_SQ")
    private Long id;

    @Column(name="ISVISIBLE", nullable = false)
    private Boolean isVisible;

    @Column(name="ISENABLED", nullable = false)
    private Boolean isEnabled;

    @Column(name="NAME", nullable = false)
    private String name;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }


    public Boolean getVisible() {
        return isVisible;
    }

    public void setVisible(Boolean visible) {
        isVisible = visible;
    }

    public Boolean getEnabled() {
        return isEnabled;
    }

    public void setEnabled(Boolean enabled) {
        isEnabled = enabled;
    }

}

我的服务类

@Transactional
public class RolesServiceImpl implements RolesService {
    @PersistenceContext
    private EntityManager em;
    private RolesDAO rolesDAO;
    private Mapper mapper;

    public RolesServiceImpl(RolesDAO rolesDAO, Mapper mapper) {
        this.rolesDAO = rolesDAO;
        this.mapper = mapper;
    }

    @Transactional
    public RoleModule saveOrUpdateModule(RoleModule vo) {
        RoleModuleMO mo = new RoleModuleMO();

        mapper.map(vo, mo);
        mo = rolesDAO.saveOrUpdateModule(mo);
        vo = mapper.map(mo, RoleModule.class);

        return vo;
    }
}

这是在春季4.0.6和升级到4.3.9后面临错误。 Spring社区是否有任何更改导致此错误,或者还有其他需要更改的内容?

提前致谢。

0 个答案:

没有答案