【发布时间】:2013-09-04 23:42:59
【问题描述】:
我有一个类似的方法:
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);
}
}
而super.insertOpAppRelation方法内容为:
package com.abc.pkg.service.db.impl;
public abstract class BaseService {
@PersistenceContext
protected EntityManager em;
@Transactional
protected void removeOpAppRelation(int opId,int appId){
String sql="delete table a where 1=1";
if(opId>0)
sql+=" and op_id="+opId;
if(appId>0)
sql+=" and app_id="+appId;
em.createNativeQuery(sql).executeUpdate();
}
}
并且触发了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
有什么想法吗?跟继承有关系吗?
【问题讨论】:
-
类和超类都位于
com.abc.pck包下吗? -
它们在完全相同的包下。但完整的包名不是 com.abc.pck 。它是 com.abc.pck.service.db.impl。我已将其作为前缀包以使其扫描所有子包
-
嗯.. 没有足够的信息来进一步调试您的问题。我建议您将 org.hibernate.transaction 记录器设置为 DEBUG(或 hibernate 4.2 或更高版本上的 org.hibernate.engine.transaction),从您的日志中您将看到事务开始/提交
-
实际上我正在使用带有 JPA 的休眠。这里的另一个区别是,我从其他地方调用这个方法(@controller 注释类),我现在遇到了这个问题。
标签: java hibernate jpa annotations spring-transactions