TransactionRequiredException Executing an update/delete query

后端 未结 20 2071
没有蜡笔的小新
没有蜡笔的小新 2020-12-02 15:29

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         


        
相关标签:
20条回答
  • 2020-12-02 15:47

    I have also faced same issue when I work with Hibernate and Spring Jpa Data Repository. I forgot to place @Transactional on spring data repository method.

    Its working for me after annotating with @Transactional.

    0 讨论(0)
  • 2020-12-02 15:49

    as form the configuration xml it is clear that you are going to use spring based transaction. Please make sure that the import which you are using for @Transactional is "org.springframework.transaction.annotation.Transactional"

    in your case it might be "javax.transaction.Transactional"

    cheers

    Chetan Verma

    0 讨论(0)
  • 2020-12-02 15:50

    After integrating Spring 4 with Hibernate 5 in my project and experiencing this problem, I found that I could prevent the error from appearing by changing the way of getting the session from sessionFactory.openSession() to sessionFactory.getCurrentSession(). the proper way to work out this bug may be keep using the same session for the binding transaction.opensession will always create a new session which doesnt like the former one holding a transaction configed.using getCurrentSession and adding additional property <property name="current_session_context_class">org.springframework.orm.hibernate5.SpringSessionContext</property> works fine for me.

    0 讨论(0)
  • 2020-12-02 15:51

    Faced the same problem, I simply forgot to activate the transaction management with the @EnableTransactionManagement annotation.

    Ref:

    https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/transaction/annotation/EnableTransactionManagement.html

    0 讨论(0)
  • 2020-12-02 15:52

    I was facing the same error inspite of adding @Transactional annotation at class level and importing org.springframework.transaction.annotation.Transactional.

    Then I realized that since I was using hibernate query, I had imported javax.persistence.Query instead of import org.hibernate.query.Query. Therefore adding import org.hibernate.query.Query solved the issue for me.

    I hope this idea helps someone.

    0 讨论(0)
  • 2020-12-02 15:53

    In my case, I had the wrong @Transactional imported.

    The correct one is:

    import org.springframework.transaction.annotation.Transactional;
    

    and not

    import javax.transaction.Transactional;
    
    0 讨论(0)
提交回复
热议问题