Java 调用super';s法

Java 调用super';s法,java,hibernate,jpa,annotations,spring-transactions,Java,Hibernate,Jpa,Annotations,Spring Transactions,我有这样一种方法: package com.abc.pkg.service.db.impl; public class OperationServiceImpl extends BaseService implements OperationService { @Transactional @Override public String update(Operation operation,User user) { BigDe

我有这样一种方法:

package com.abc.pkg.service.db.impl;



public class OperationServiceImpl extends BaseService implements OperationService {



    @Transactional
        @Override
        public String update(Operation operation,User user) {

        BigDecimal count=(BigDecimal)em.createNativeQuery("select count(*) from RBMCORE.T_RBM_OPSCREENS_OPERATIONS where s_name= ? and id!= ?").setParameter(1, operation.getName()).setParameter(2, operation.getId()).getSingleResult();

        if(count.intValue()>0)
            return "This operation name is used by another operation. Please change it";

        super.removeOpAppRelation(operation.getId(), -1);

        Operation oldOperation=operationRepository.save(operation);
        List<Operation> operations=new ArrayList<Operation>();
        operations.add(operation);
}
}
并触发removeOpAppRelation方法,则引发此异常:

javax.persistence.TransactionRequiredException:执行 更新/删除查询

在我的appcontext.xml中,我有以下内容:

<tx:annotation-driven transaction-manager="transactionManager"/>
<context:component-scan base-package="com.abc.pck"/>

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="rbmDataSource"/>
    <property name="packagesToScan" value="com.ttech.rbm.model"/>
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="databasePlatform" value="org.hibernate.dialect.Oracle10gDialect"/>
            <property name="showSql" value="true"/>
            <property name="generateDdl" value="true"/>
        </bean>
    </property>
    <property name="jpaProperties">
        <props>
            <prop key="hibernate.hbm2ddl.auto">none</prop>
        </props>
    </property>
</bean>

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

没有一个
作为事务管理器,我使用:

org.springframework.orm.jpa.JpaTransactionManager


有什么想法吗?它与继承有关吗?

您的代码简化了很多

使用的“em”未在任何类中声明

我希望有一个

@PersistenceContext
EntityManager em;
在你的一节课上

如果它在两个类中,它将解释您的错误,因为在这种情况下,将注入两个不同的实体管理器,它们不会共享同一事务

为了避免这种情况,您应该在超类中使用抽象方法“getEm()”,并在子类中重写它,并在子类中注入em


此外,当从子类调用时,超类上的@Transactional将不起作用,因为它直接从子类而不是通过spring aop代理调用的超级方法-spring没有机会拦截。

找到了它。我传递到事务管理器的实体管理器不是AOP安全的。因此,它在执行查询时不会启动事务。我已打开并管理自己的交易,问题已解决。感谢回复

类和超级类是否都位于
com.abc.pck
包中?它们位于完全相同的包中。但是完整的包名不是com.abc.pck。它是com.abc.pck.service.db.impl。我将其作为前缀包,以便使其扫描所有子包嗯。。没有足够的信息来进一步调试您的问题。我建议您将org.hibernate.transaction logger设置为DEBUG(或在hibernate 4.2或更高版本上设置为org.hibernate.engine.transaction),从您的日志中,您将看到事务开始/完成我正在将hibernate与JPA一起使用。这里的另一个区别是,我从其他地方调用这个方法(@controller注释类),现在我确实遇到了这个问题。谢谢你的回答。我已经编辑了这个问题。现在,您可以看到有一个实体管理器,事务管理器处理实体管理器的事务。我认为,因为entityManager对象是受保护的对象,所以它是子类和父类上的单例(因为它们都共享一个entityManager对象)。所以在我更新了这个问题之后,有什么想法吗?如果根本没有事务,我希望OperationServiceImpl.update(..)中会出现异常,因为它已经需要了。您可以尝试将@Transactional移动到接口方法OperationService.update(..)中。不能从OperationServiceImpl内部调用OperationServiceImpl.update(..)。
@PersistenceContext
EntityManager em;