javax.persistence.TransactionRequiredException错误:jpa2+;hibernate-本机查询
当我试图更新sphinx实时索引时,出现了上述错误。由于sphinx使用的数据库非常接近mysql,所以我尝试使用实体管理器来更新索引。但是sphinxql并不是一个完整的mysql数据库,所以如果我使用entitymanager.merge方法,sphinxql无法理解生成的sql。因此,我决定使用createNativeQuery插入新索引或从实时索引中删除索引。但是,当我调用createNativeQuery的.executeUpdate方法时,标题中出现了异常 这是我的刀:javax.persistence.TransactionRequiredException错误:jpa2+;hibernate-本机查询,java,mysql,spring,hibernate,Java,Mysql,Spring,Hibernate,当我试图更新sphinx实时索引时,出现了上述错误。由于sphinx使用的数据库非常接近mysql,所以我尝试使用实体管理器来更新索引。但是sphinxql并不是一个完整的mysql数据库,所以如果我使用entitymanager.merge方法,sphinxql无法理解生成的sql。因此,我决定使用createNativeQuery插入新索引或从实时索引中删除索引。但是,当我调用createNativeQuery的.executeUpdate方法时,标题中出现了异常 这是我的刀: public
public interface RTIndexGeoDao {
public boolean deleteIndex(long id);
public boolean insertIndex(long id, long agentActivityId, long agentId, double latitude, double longitude);
}
这是我对Dao的实现:
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.springframework.stereotype.Repository;
@Repository(value = "rtIndexGeoDao")
public class JPARTIndexDao implements RTIndexGeoDao {
private EntityManager em;
@PersistenceContext(unitName = "rtIndexPU")
public void setRtIndexEntityManager(EntityManager em)
{
this.em = em;
}
public boolean deleteIndex(long id) {
//This is to update our geodistance index
try
{
this.em.createNativeQuery("delete from rt_geo where agent_agent_id = ?1").setParameter(1, id).executeUpdate();
return true;
}
catch (Exception ex)
{
return false;
}
}
public boolean insertIndex(long id, long agentActivityId, long agentId,
double latitude, double longitude) {
try
{
this.em.createNativeQuery("insert into rt_geo(id, agent_activity_id, agent_agent_id, latitude, longitude) values(?1, ?2, ?3, ?4, ?5)")
.setParameter(1, id)
.setParameter(2, agentActivityId)
.setParameter(3, agentId)
.setParameter(4, latitude)
.setParameter(5, longitude).executeUpdate();
return true;
}
catch (Exception ex)
{
return false;
}
}
}
这是我的RTIndexManager:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springtest.mavenspringapp.repository.RTIndexGeoDao;
@Component
@Transactional
public class SphinxRTIndexManagerImpl implements SphinxRTIndexManager {
/**
*
*/
private static final long serialVersionUID = 1L;
@Autowired
private RTIndexGeoDao rtIndexGeoDao;
public boolean deleteIndex(long id) {
return rtIndexGeoDao.deleteIndex(id);
}
public boolean insertIndex(long id, long agentActivityId, long agentId,
double latitude, double longitude) {
return rtIndexGeoDao.insertIndex(id, agentActivityId, agentId, latitude, longitude);
}
}
这是我的persistence.xml
<persistence-unit name="rtIndexPU" transaction-type="RESOURCE_LOCAL">
</persistence-unit>
更新:我正在发布我的applicationContext.xml
<!-- enabling annotation driven configuration /-->
<context:annotation-config/>
<bean id="rtIndexEntityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
p:dataSource-ref="rtIndexDataSource"
p:jpaVendorAdapter-ref="jpaAdapter">
<property name="loadTimeWeaver">
<bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
</property>
<property name="persistenceUnitName" value="rtIndexPU"></property>
</bean>
<bean id="rtIndexTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"
p:entityManagerFactory-ref="rtIndexEntityManagerFactory"/>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"
p:entityManagerFactory-ref="entityManagerFactory"/>
我有另一个持久性单元,每个经理、dao等都在工作。但当我在persistenceContext为rtIndexPU的entitymanager上使用createNativeQuery.executeUpdate时,会出现以下异常:
javax.persistence.TransactionRequiredException:执行更新/删除查询
我已经搜索了互联网上的所有问题,但无法解决这个问题。提前感谢。由于您的代码中有两个事务管理器,问题很可能是您的
SphinxRTIndexManagerImpl
没有使用正确的事务管理器(或任何事务管理器)
您应该将代码更改为:
@Component
@Transactional("rtIndexTransactionManager")
public class SphinxRTIndexManagerImpl implements SphinxRTIndexManager {
}
此外,您还必须通过执行以下操作来配置第二个事务管理器(RTIndextTransactionManager)以使用注释:
<tx:annotation-driven transaction-manager="rtIndexTransactionManager"/>
由于您的代码中有两个事务管理器,因此问题很可能是您的
SphinxRTIndexManagerImpl
没有使用正确的事务管理器
您应该将代码更改为:
@Component
@Transactional("rtIndexTransactionManager")
public class SphinxRTIndexManagerImpl implements SphinxRTIndexManager {
}
此外,您还必须通过执行以下操作来配置第二个事务管理器(RTIndextTransactionManager)以使用注释:
<tx:annotation-driven transaction-manager="rtIndexTransactionManager"/>
请同时发布您的Spring配置我编辑了我的问题。您的配置中是否有任何地方?是的,我有,但我忘了在此处发布。请同时发布您的Spring配置我编辑了我的问题。您的配置中是否有任何地方?是的,我有,但我忘了在此处发布。事实上,它是!谢谢你的帮助和帮助。我希望这能帮助其他有同样问题的人。我找到的大多数答案都表明在交易之前使用beginTransaction。换句话说,建议手动处理transaction@JorgeCespedes很高兴你能帮忙!事实上,是的!谢谢你的帮助和帮助。我希望这能帮助其他有同样问题的人。我找到的大多数答案都表明在交易之前使用beginTransaction。换句话说,建议手动处理transaction@JorgeCespedes很高兴你能帮忙!