Spring/Hibernate 截断/删除表中的所有行 - 事务问题
我有一个 Camel 项目,在创建控制 bean 后,我们想要清理数据库日志表。因此,每次运行应用程序时,我们都会截断一个名为代理订单的表。这是在实体对象中设置为命名查询。
@NamedNativeQuery(name="cleanOrderTable", query="TRUNCATE agent_orders",resultClass= AgentOrderEntity.class)
调用此查询的代码如下所示:
@Component("mgr")
public class Controller{
@PersistenceContext(unitName="camel")
private EntityManager em;
.......
@Transactional
public void clearHistoricalOrders() throws Exception{
Query query = em.createNamedQuery("cleanOrderTable");
query.executeUpdate();
}
}
调用清除历史记录方法,我们收到错误 javax.persistence.TransactionRequiredException: Executing an update/delete query
我已经尝试了所有方法,UserTransaction
,em.getTransaction().begin
- 没有任何作用。知道如何运行这个查询吗?
我们的应用程序 context.xml 中有以下 tran 管理器设置:
<tx:annotation-driven transaction-manager="txManager" />
<bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager"
p:dataSource-ref="dataSource">
<property name="entityManagerFactory" ref="emFactory" />
</bean>
I have a Camel project and after we create a controll bean we want to clean up a DB log table. SO each time we run the application we TRUNCATE a table called agent orders. This is setup in an Enity object as a named query.
@NamedNativeQuery(name="cleanOrderTable", query="TRUNCATE agent_orders",resultClass= AgentOrderEntity.class)
The code that calls this query looks like:
@Component("mgr")
public class Controller{
@PersistenceContext(unitName="camel")
private EntityManager em;
.......
@Transactional
public void clearHistoricalOrders() throws Exception{
Query query = em.createNamedQuery("cleanOrderTable");
query.executeUpdate();
}
}
Call the clear history method we get an error javax.persistence.TransactionRequiredException: Executing an update/delete query
I have tried everything, UserTransaction
, em.getTransaction().begin
- nothing works. Any idea how I can run this query?
We have the following tran manager setup in our app context.xml:
<tx:annotation-driven transaction-manager="txManager" />
<bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager"
p:dataSource-ref="dataSource">
<property name="entityManagerFactory" ref="emFactory" />
</bean>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
希望这有帮助!
Hope this helps!
我会尝试使用
TransactionTemplate
执行查询,只是为了检查 @Transactional 注释是否确实有效。另外,
resultClass= AgentOrderEntity.class
是怎么回事?为什么截断表的查询需要返回一些东西?I'd try executing the query with a
TransactionTemplate
just to check that the @Transactional annotation really isn't having an effect.Also, what's up with
resultClass= AgentOrderEntity.class
? Why does a query that truncates a table need to return something?