我在使用Native-query执行插入查询时遇到事务异常,如何解决?

时间:2016-09-26 05:22:47

标签: jpa spring-integration

我的代码是

<int-jpa:outbound-channel-adapter
		channel="nativeQlChannel"
		native-query="insert into responsestatus(partner_id,response) values (:pid,:response)"
		entity-manager="entityManager" >
		<int-jpa:parameter name="pid" expression="payload['pid']" />
		<int-jpa:parameter name="response" expression="payload['response']" >
	</int-jpa:outbound-channel-adapter>

例外

Caused by: javax.persistence.TransactionRequiredException: Executing an update/delete query
    at org.hibernate.jpa.spi.AbstractQueryImpl.executeUpdate(AbstractQueryImpl.java:54)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.springframework.orm.jpa.SharedEntityManagerCreator$DeferredQueryInvocationHandler.invoke(SharedEntityManagerCreator.java:372)
    at com.sun.proxy.$Proxy29.executeUpdate(Unknown Source)

1 个答案:

答案 0 :(得分:0)

如果你的线程尚未在事务中运行,则需要使用<int-jpa:transactional />启动它。

the reference manual