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?