我正在使用JSpring和JPA / Hibernate处理项目。我尝试调用本机查询来更新数据库。这是错误跟踪:
Caused by: javax.persistence.TransactionRequiredException: Executing an update/delete query
at org.hibernate.ejb.AbstractQueryImpl.executeUpdate(AbstractQueryImpl.java:96)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.orm.jpa.SharedEntityManagerCreator$DeferredQueryInvocationHandler.invoke(SharedEntityManagerCreator.java:311)
at com.sun.proxy.$Proxy31.executeUpdate(Unknown Source)
at com.ncrm.dao.daoImpl.UpdateExt(daoImpl.java:124)
at com.ncrm.metier.MetierImpl.UpdateExt(MetierImpl.java:663)
at com.ncrm.loginBean.authentification(loginBean.java:57)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.el.parser.AstValue.invoke(AstValue.java:262)
at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:278)
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<context:annotation-config />
<context:component-scan base-package="com.ncrm.dao" />
<bean name="daoo" class="com.ncrm.dao.daoImpl"></bean>
<bean name="metier" class="com.ncrm.metier.MetierImpl">
<property name="dao" ref="daoo"></property>
</bean>
<!-- <bean name="paramEtat" class="com.ncrm.editionEtats.dao.paramEtatsImp"></bean> -->
<!-- ############################################################################## -->
<!-- le gestionnaire de transactions -->
<!-- ############################################################################## -->
<tx:annotation-driven transaction-manager="jpaTransactionManager" />
<bean id="jpaTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="localContainerEntityManagerFactoryBean" />
</bean>
<tx:annotation-driven transaction-manager="jpaTransactionManager" />
<bean id="jpaTransactionManagerBPa" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="localContainerEntityManagerFactoryBeanBpa" />
</bean>
<!-- ############################################################################## -->
<!-- couche de persistance JPA -->
<!-- ############################################################################## -->
<bean id="localContainerEntityManagerFactoryBean"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceXmlLocation" value="classpath:config/persistence.xml" />
<property name="persistenceUnitName" value="ncrm_extraction" />
<property name="dataSource" ref="basicDataSource" />
<property name="jpaVendorAdapter">
<bean id="hibernateJpaVendorAdapter"
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect" />
<property name="generateDdl" value="false" />
<property name="showSql" value="false"></property>
</bean>
</property>
<property name="loadTimeWeaver">
<bean id="instrumentationLoadTimeWeaver"
class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
</property>
</bean>
<bean id="localContainerEntityManagerFactoryBeanBpa"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceXmlLocation" value="classpath:config/persistence.xml" />
<property name="persistenceUnitName" value="BpaProduciton" />
<property name="dataSource" ref="basicDataSourceBpa" />
<property name="jpaVendorAdapter">
<bean id="hibernateJpaVendorAdapterBpa"
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect" />
<property name="generateDdl" value="false" />
<property name="showSql" value="false"></property>
</bean>
</property>
<property name="loadTimeWeaver">
<bean id="instrumentationLoadTimeWeaverBpa"
class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
</property>
</bean>
<!-- ############################################################################## -->
<!-- DataSource -->
<!-- ############################################################################## -->
<bean id="basicDataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/r_ncrmextraction" />
<property name="username" value="root" />
<property name="password" value="fast" />
</bean>
<bean id="basicDataSourceBpa" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/" />
<property name="username" value="root" />
<property name="password" value="fast" />
</bean>
<!-- traduction des exceptions -->
<bean id="persistenceExceptionTranslationPostProcessor"
class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
<!-- annotations de persistance -->
<bean id="persistenceAnnotationBeanPostProcessor"
class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
</beans>
我的DAO课程
public class daoImpl implements Idao,Serializable {
String BDD_REF="r_multi_4";
private static final Logger logger = Logger.getLogger(daoImpl.class);
@PersistenceContext(unitName="ncrm_extraction")
EntityManager em;
@PersistenceContext(unitName="BpaProduciton")
EntityManager emBpa;
@Transactional
public int UpdateExt(String pk_obj_id_Rem,String Req) {
logger.debug("@@@ DAO : Modifier le flag EXT table remise : "+pk_obj_id_Rem );
Query q = emBpa.createNativeQuery(Req);
// q.setParameter("Date", dteTr);
// q.setParameter("pk_obj_id_Rem", pk_obj_id_Rem);
int flag=q.executeUpdate();
return flag;
}
我的坚持:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="ncrm_extraction" transaction-type="RESOURCE_LOCAL">
<!-- <class>com.ncrm.dao.entities.user</class> -->
</persistence-unit>
<persistence-unit name="BpaProduciton" transaction-type="RESOURCE_LOCAL">
<!-- <class>com.ncrm.dao.entities.user</class> -->
</persistence-unit>
</persistence>
答案 0 :(得分:0)
假设你修改你的DaoImpl如下。
@Modifying(clearAutomatically = true)
@Transactional
public int UpdateExt(String pk_obj_id_Rem,String Req) {
logger.debug("@@@ DAO : Modifier le flag EXT table remise : "+pk_obj_id_Rem );
Query q = emBpa.createNativeQuery(Req);
int flag=q.executeUpdate();
return flag;
}
希望这有帮助。
有关详细信息,请参阅此post。