问题描述
我得到错误:
线程main中的异常org.hibernate.HibernateException:
无法获取当前线程的事务同步会话
main
ppService.deleteProductPart(cPartId,productId);
@Service(productPartService)
@Override
public void deleteProductPart(int cPartId,int productId){
productPartDao.deleteProductPart(cPartId,productId);
}
@Repository(productPartDAO) (ProductPart productPart){
sessionFactory.getCurrentSession()。delete(productPart)(p)
@Override
public void deleteProductPart ;
$ b @Override
public void deleteProductPart(int cPartId,int productId){
ProductPart productPart =(ProductPart)sessionFactory.getCurrentSession()$ b).createCriteria(ProductPart)
.add(Restrictions.eq(part,cPartId))
.add(Restrictions.eq(product,productId))。uniqueResult() ;
deleteProductPart(productPart);
}
如何解决它?
更新:
如果我修改这样的方法:
@Override
@Transactional
public void deleteProductPart(int cPartId,int productId){
System.out.println(sessionFactory.getCurrentSession());
}
返回:
SessionImpl(PersistenceContext [entityKeys = [],collectionKeys = []]; ActionQueue [insertions = [] updates = [] deletions = [] collectionCreations = [] collectionRemovals = [] collectionUpdates = [] collectionQueuedOps = [] unresolvedInsertDependencies = UnresolvedEntityInsertActions []])
但是,如果我删除<$ c $
org.hibernate.HibernateException:无法获取当前线程的事务同步会话
我通过添加 @Transactional
,但现在我得到 org.hibernate.MappingException:未知实体:ProductPart
尽管我链接了 .uniqueResult()
到 Criteria
。如何解决这个问题?
ProductPart
的实体。解决此问题的一种方法是将 Class
对象传递给 createCriteria
方法: createCriteria(ProductPart.class)
从API中使用String和Class的区别如下:
为给定的实体名称创建一个新的Criteria实例。
如果传递一个字符串,那么hibernate会查找名称为 ProductPart
的实体。
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")
@Override
public void deleteProductPart(int cPartId, int productId) {
productPartDao.deleteProductPart(cPartId, productId);
}
@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 @Transactional
it ends up with exception:
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: ProductPart
although I chained .uniqueResult()
to Criteria
. How to fix it?
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.
If you pass a String then hibernate looks for an entity whose name is declared as ProductPart
.
这篇关于HibernateException:无法获取当前线程的事务同步会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!