9

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?

8
  • 1
    try beginning the transaction using session.getTransaction().begin(); and see if that helps? Also the createCriteria method returns the CriteriaImpl so you cannot cast it directly to ProductPart
    – Learner
    Commented Sep 19, 2014 at 12:37
  • @Chaitanya, If I cannot cast like that then what is the easiest solution?
    – J.Olufsen
    Commented Sep 19, 2014 at 13:04
  • RCola, I am not clear on your comment, can you elaborate?
    – Learner
    Commented Sep 19, 2014 at 13:07
  • Also in your updated question you are just printing something to console so what you are trying to acive, it is not clear
    – Learner
    Commented Sep 19, 2014 at 13:09
  • 1
    Finally for the error Unknown entity: ProductPart , you have to change the createCriteria code as createCriteria(ProductPart.class)
    – Learner
    Commented Sep 19, 2014 at 13:10

3 Answers 3

7

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.

6

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

1

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

1
  • yees, i added the annotation on my entity and it worked, i commented the <tx:annotation..> because i was not going to do ecache and so i got the error
    – dzgeek
    Commented Apr 23, 2015 at 15:39

Not the answer you're looking for? Browse other questions tagged or ask your own question.