2014-09-19 22 views
9

sto ottenendo errore:HibernateException: Impossibile ottenere sessione di transazione sincronizzata per thread corrente

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

principale

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); 
    } 

Come risolvere il problema?

UPDATE:

Se modifico metodo come questo:

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

Restituisce:

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

Ma se rimuovo @Transactional finisce con l'eccezione:

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

Lo faccio funzionare aggiungendo @Transactional, ma ora sto ottenendo org.hibernate.MappingException: Unknown entity: ProductPart anche se ho incatenato .uniqueResult() a Criteria. Come sistemarlo?

+1

tentativo di iniziare la transazione utilizzando 'session.getTransaction() begin();.' E vedere se funziona? Anche il metodo 'createCriteria' restituisce' CriteriaImpl' quindi non puoi lanciarlo direttamente su 'ProductPart' – Chaitanya

+0

@Chaitanya, Se non riesco a trasmettere in quel modo, qual è la soluzione più semplice? –

+0

RCola, non sono chiaro sul tuo commento, puoi elaborare? – Chaitanya

risposta

5

L'errore org.hibernate.MappingException: Unknown entity: ProductPart indica che non esiste un'entità con nome ProductPart. Un modo per risolvere questo problema è quello di superare il metodo Class oggetto createCriteria come:

createCriteria(ProductPart.class) 

Dalla API differenza nell'uso String e Classe è il seguente:

Session.createCriteria(String)

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

Session.createCriteria(Class)

Se si passa una stringa, l'ibernazione cerca un'entità il cui nome è dichiarato come ProductPart.

4

Con Hibernate 4.xe Spring 4.x, solo Aggiungi @Transactional dopo @Repository risolverà questa eccezione di sincronizzazione.

0

È necessario abilitare il supporto di transazione (<tx:annotation-driven> o @EnableTransactionManagement) e dichiarare il transactionManager e dovrebbe funzionare tramite SessionFactory.

È necessario aggiungere @Transactional nella vostra @Repository

Con @Transactional nella tua @Repository primavera è in grado di applicare supporto transazionale nel repository.

La classe studente non ha le annotazioni @javax.persistence.* come @Entity, sto assumendo che la Configurazione di mapping per quella classe sia stata definita tramite XML.

Ref

+0

yee, ho aggiunto l'annotazione sulla mia entità e ha funzionato, ho commentato perché non avevo intenzione di fare ecache e quindi ho ricevuto l'errore – dzgeek

Problemi correlati