Hi I am using hibernate JPA with spring and mongodb and i am running my application on Glassfish-4.0.
My service class is :
@Component
public class T
I am not sure if this will help your situation (that is if it stills exists), however, after scouring the web for a similar issue.
I was creating a native query from a persistence EntityManager to perform an update.
Query query = entityManager.createNativeQuery(queryString);
I was receiving the following error:
caused by: javax.persistence.TransactionRequiredException: Executing an update/delete query
Many solutions suggest adding @Transactional to your method. Just doing this did not change the error.
Some solutions suggest asking the EntityManager for a EntityTransaction
so that you can call begin and commit yourself.
This throws another error:
caused by: java.lang.IllegalStateException: Not allowed to create transaction on shared EntityManager - use Spring transactions or EJB CMT instead
I then tried a method which most sites say is for use application managed entity managers and not container managed (which I believe Spring is) and that was joinTransaction()
.
Having @Transactional
decorating the method and then calling joinTransaction()
on EntityManager object just prior to calling query.executeUpdate()
and my native query update worked.
I hope this helps someone else experiencing this issue.
Using @Modifying and @Transaction fixed me
@Modifying @Query(value="DELETE FROM lock WHERE user_id = ?1" ,nativeQuery=true) void deleteByUserId(Long userId);
@Service @Transactional
If the previous answers fail, make sure you use @Service stereotype for the class where you call the update method on your repository. I originally used @Component instead and it was not working, the simple change to @Service made it work.
You need not to worry about begin and end transaction. You have already apply @Transactional annotation, which internally open transaction when your method starts and ends when your method ends. So only required this is to persist your object in database.
@Transactional(readOnly = false, isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
public String persist(Details details){
details.getUsername();
details.getPassword();
Query query = em.createNativeQuery("db.details.find(username= "+details.getUsername()+"& password= "+details.getPassword());
em.persist(details);
System.out.println("Sucessful!");
return "persist";
}
EDIT : The problem seems to be with your configuration file. If you are using JPA then your configuration file should have below configuration
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"
p:entityManagerFactory-ref="entityManagerFactory" />
<bean id="jpaAdapter"
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"
p:database="ORACLE" p:showSql="true" />
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
p:persistenceUnitName="YourProjectPU"
p:persistenceXmlLocation="classpath*:persistence.xml"
p:dataSource-ref="dataSource" p:jpaVendorAdapter-ref="jpaAdapter">
<property name="loadTimeWeaver">
<bean
class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
</property>
<property name="persistenceProvider" ref="interceptorPersistenceProvider" />
</bean>
Using @PersistenceContext with @Modifying as below fixes error while using createNativeQuery
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.transaction.annotation.Transactional;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
@PersistenceContext
private EntityManager entityManager;
@Override
@Transactional
@Modifying
public <S extends T> S save(S entity) {
Query q = entityManager.createNativeQuery(...);
q.setParameter...
q.executeUpdate();
return entity;
}
I received this exception when trying to run a bulk UPDATE query in a non-JTA (i.e. resource-local) entity manager in Java SE. I had simply forgotten to wrap my JPQL code in
em.getTransaction().begin();
and
em.getTransaction().commit();