Java HibernateException:无法获取当前线程的事务同步会话

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25933532/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 01:32:00  来源:igfitidea点击:

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

javaspringhibernate

提问by J.Olufsen

I am getting error:

我收到错误:

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

main

主要的

ppService.deleteProductPart(cPartId, productId);

@Service("productPartService")

@Service("productPartService")

@Override
public void deleteProductPart(int cPartId, int productId) {
    productPartDao.deleteProductPart(cPartId, productId);
}

@Repository("productPartDAO")

@Repository("productPartDAO")

@Override
    public void deleteProductPart(ProductPart productPart) {
        sessionFactory.getCurrentSession().delete(productPart);
    }


@Override
    public void deleteProductPart(int cPartId, int productId) {
        ProductPart productPart  = (ProductPart) sessionFactory.getCurrentSession()
                .createCriteria("ProductPart")
                .add(Restrictions.eq("part", cPartId))
                .add(Restrictions.eq("product", productId)).uniqueResult();
        deleteProductPart(productPart);
    }

How to fix it?

如何解决?

UPDATE:

更新:

If I modify method like this:

如果我像这样修改方法:

@Override
@Transactional
public void deleteProductPart(int cPartId, int productId) {          
    System.out.println(sessionFactory.getCurrentSession());
}

It returns:

它返回:

SessionImpl(PersistenceContext[entityKeys=[],collectionKeys=[]];ActionQueue[insertions=[] updates=[] deletions=[] collectionCreations=[] collectionRemovals=[] collectionUpdates=[] collectionQueuedOps=[] unresolvedInsertDependencies=UnresolvedEntityInsertActions[]])

But if I remove @Transactionalit ends up with exception:

但是,如果我将@Transactional其删除,则会出现异常:

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

I get it working by adding @Transactional, but now I am getting org.hibernate.MappingException: Unknown entity: ProductPartalthough I chained .uniqueResult()to Criteria. How to fix it?

我通过添加来让它工作@Transactional,但现在我得到了org.hibernate.MappingException: Unknown entity: ProductPart虽然我链接.uniqueResult()Criteria. 如何解决?

采纳答案by Chaitanya

The error org.hibernate.MappingException: Unknown entity: ProductPartindicates there is no entity with name ProductPart. One way to fix this issue is to pass the Classobject to createCriteriamethod as:

该错误org.hibernate.MappingException: Unknown entity: ProductPart表明没有名为 name 的实体ProductPart。解决此问题的一种方法是将Class对象传递给createCriteria方法:

createCriteria(ProductPart.class)

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

从 API 来看,使用 String 和 Class 的区别如下:

Session.createCriteria(String)

Session.createCriteria(字符串)

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

Session.createCriteria(Class)

Session.createCriteria(类)

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

使用给定的别名为给定的实体类或实体类的超类创建一个新的 Criteria 实例。

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

如果您传递一个字符串,则休眠会查找名称声明为ProductPart.

回答by Ayman Al-Absi

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

对于 Hibernate 4.x 和 Spring 4.x,只需在 @Repository 之后添加 @Transactional 即可解决此同步异常。

回答by Anand Rockzz

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

您必须启用事务支持(<tx:annotation-driven>@EnableTransactionManagement)并声明事务管理器,它应该通过 SessionFactory 工作。

You must add @Transactionalinto your @Repository

您必须添加@Transactional到您的@Repository

With @Transactionalin your @RepositorySpring is able to apply transactional support into your repository.

随着@Transactional在你的@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.

您的 Student 类没有@javax.persistence.*注释@Entity,我假设该类的映射配置已通过 XML 定义。

Ref

参考

回答by pooja patil

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

我有与“org.hibernate.HibernateException:无法获得当前线程的事务同步会话”相同的错误

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

我刚刚在Dao实现中使用了@Transactional(readOnly = false)解决了我的问题