HibernateException: Could not obtain transaction-synchronized Session for current thread

后端 未结 4 1141
被撕碎了的回忆
被撕碎了的回忆 2021-01-04 21:16

I am getting error:

Exception in thread \"main\" org.hibernate.HibernateException: 
Could not obtain transaction-synchronized Session for current thread


        
相关标签:
4条回答
  • 2021-01-04 21:56

    I had same error as "org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread"

    I just used @Transactional(readOnly = false) in my Dao implementation resolved my issue

    0 讨论(0)
  • 2021-01-04 22:04

    The error org.hibernate.MappingException: Unknown entity: ProductPart indicates there is no entity with name ProductPart. One way to fix this issue is to pass the Class object to createCriteria method as:

    createCriteria(ProductPart.class)
    

    From API the difference in using String and Class is as follows:

    Session.createCriteria(String)

    Create a new Criteria instance, for the given entity name. 
    

    Session.createCriteria(Class)

    Create a new Criteria instance, for the given entity class, or a superclass of an entity class, with the given alias.

    If you pass a String then hibernate looks for an entity whose name is declared as ProductPart.

    0 讨论(0)
  • 2021-01-04 22:06

    With Hibernate 4.x and Spring 4.x, just Add @Transactional after @Repository it will solve this synchronizaion exception.

    0 讨论(0)
  • 2021-01-04 22:07

    You must enable the transaction support (<tx:annotation-driven> or @EnableTransactionManagement ) and declare the transactionManager and it should work through the SessionFactory.

    You must add @Transactional into your @Repository

    With @Transactional in your @Repository Spring is able to apply transactional support into your repository.

    Your Student class has no the @javax.persistence.* annotations how @Entity, I am assuming the Mapping Configuration for that class has been defined through XML.

    Ref

    0 讨论(0)
提交回复
热议问题