I am migrating my code from hibernate 3 (that is using hibernate template) to JPA 2. My project is using Spring as well.
Current project is using hibernatetempate as
hibernateTemplate.executeWithNativeSession(new HibernateCallback<Object>() {
@Override
public Object doInHibernate(Session session) throws SQLException {
Query query = session.getNamedQuery("updateToProcessed");
query.setParameter("Id", id);
return query.executeUpdate();
}
});
updateToProcessed is a simple update hql query. Please help to let me know how to convert it to JPA (to use entityManager)
I tried using
Query query = entityManager.createNamedQuery("updateToProcessed");
query.setParameter("Id", id);
query.executeUpdate();
Complete method is
@Override
public void updateAllBatchDetails(final String id) {
Query query = entityManager.createNamedQuery("updateToProcessed");
query.setParameter("Id", id);
query.executeUpdate();
}
But I am getting error as:
Caused by: javax.persistence.TransactionRequiredException: Executing an update/delete query
at org.hibernate.jpa.spi.AbstractQueryImpl.executeUpdate(AbstractQueryImpl.java:71)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.springframework.orm.jpa.SharedEntityManagerCreator$DeferredQueryInvocationHandler.invoke(SharedEntityManagerCreator.java:333)
at com.sun.proxy.$Proxy143.executeUpdate(Unknown Source)
I have configured transactionManager in applicationContext.xml like
I was expecting this answer and I have already configured that in applicationContext.xml but still I am getting that error
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"
p:entityManagerFactory-ref="entityManagerFactory"/>
But somehow @Transactional is working that I don't want to use.
This is the applicationContext.xml
<?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:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<context:component-scan base-package="com.batch"/>
<context:annotation-config/>
<context:spring-configured/>
<context:property-placeholder location="classpath:batch.properties"/>
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"
p:entityManagerFactory-ref="entityManagerFactory"/>
<aop:aspectj-autoproxy/>
<import resource="classpath:core/applicationContext.xml"/>
<import resource="classpath:spring/applicationContext-resources.xml"/>
<import resource="classpath:spring/applicationContext-batch.xml"/>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
p:persistenceUnitName="default"
p:jpaVendorAdapter-ref="jpaVendorAdapter"
p:dataSource-ref="dataSource" />
<bean id="batchProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean"
p:location="classpath:batch.properties"/>
</beans>