2012-05-05 23 views
32

sto ottenendo l'eccezione di cui sopra con Spring3 e Hibernte4org.hibernate.HibernateException: nessuna sessione trovato per thread corrente

Quello che segue è il mio file xml fagioli

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation=" 
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd 
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-3.1.xsd"> 

    <context:annotation-config/> 


    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
     <property name="driverClassName" value="com.mysql.jdbc.Driver"/> 
     <property name="url" value="jdbc:mysql://localhost:3306/GHS"/> 
     <property name="username" value="root"/> 
     <property name="password" value="newpwd"/> 
    </bean> 

    <bean id="sessionFactory" 
     class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
    <property name="dataSource" ref="dataSource"/> 
    <property name="hibernateProperties"> 
     <props> 
      <prop key="dialect">org.hibernate.dialect.MySQL5Dialect</prop> 
     </props> 
    </property> 
    <property name="packagesToScan"> 
     <list> 
      <value>com.example.ghs.model.timetable</value> 
     </list> 
    </property> 
    </bean> 

    <bean id="baseDAO" 
     class="com.example.ghs.dao.BaseDAOImpl"/> 
</beans> 

La mia classe BaseDAO assomiglia a questo

public class BaseDAOImpl implements BaseDAO{ 
private SessionFactory sessionFactory; 

@Autowired 
public BaseDAOImpl(SessionFactory sessionFactory){ 
    this.sessionFactory = sessionFactory; 
} 

@Override 
public Session getCurrentSession(){ 
    return sessionFactory.getCurrentSession(); 
} 
} 

il seguente codice genera l'eccezione nel titolo

public class Main { 
public static void main(String[] args){ 
    ClassPathXmlApplicationContext context = 
      new ClassPathXmlApplicationContext("dao-beans.xml"); 
    BaseDAO bd = (BaseDAO) context.getBean("baseDAO"); 
    bd.getCurrentSession(); 
} 
} 

Qualcuno ha un'idea su come risolvere questo problema?

risposta

43

getCurrentSession() ha senso solo in un ambito di transazione.

È necessario dichiarare un gestore transazioni appropriato, delimitare i confini della transazione ed eseguire l'accesso ai dati al suo interno. Ad esempio, come segue:

<bean id = "transactionManager" class = "org.springframework.orm.hibernate4.HibernateTransactionManager"> 
    <property name = "sessionFactory" ref = "sessionFactory" /> 
</bean> 

.

PlatformTransactionManager ptm = context.getBean(PlatformTransactionManager.class); 
TransactionTemplate tx = new TransactionTemplate(ptm); 

tx.execute(new TransactionCallbackWithoutResult() { 
    public void doInTransactionWithoutResult(TransactionStatus status) { 
     // Perform data access here 
    } 
}); 

Vedi anche:

+0

Grazie per la risposta. Esaminerò le risorse che hai collegato sopra. –

+4

'' dovrebbe anche essere incluso come menzionato da [@ vishal-jagtap] (http://stackoverflow.com/questions/10459922/org-hibernate-hibernateexception-no-session-found-for -corrente-thread/# 16296691) –

+0

Grazie mille, questo mi ha davvero aiutato in avanti. –

-1

Quale pacchetto ha messo la classe BaseDAOImpl in .. Penso che richieda un nome di pacchetto simile a quello che hai usato nel contesto dell'applicazione xml e richiede anche un'annotazione pertinente.

24

mi sono imbattuto lo stesso problema, ma ho risolto, come di seguito Aggiunto @Transactional su daoImpl classe

Aggiunto responsabile trnsaction nel file di configurazione:

<tx:annotation-driven/> 

<bean id="transactionManager" 
class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 
<property name="sessionFactory" ref="sessionFactory"></property> 
</bean> 
+0

Questo ha funzionato bene per me. Ho salvato la mia giornata. : D – wsams

+0

grazie ... ha funzionato bene anche con me .. –

5

mi limiterò a aggiungere qualcosa che mi ha portato un po 'di tempo per eseguire il debug: non dimenticate che un'annotazione @Transactional funziona solo su metodi "pubblici".

Ho messo alcuni @Transactional su quelli "protetti" e ho ricevuto questo errore.

Speranza che aiuta :)

http://docs.spring.io/spring/docs/3.1.0.M2/spring-framework-reference/html/transaction.html

visibilità Metodo e @Transactional

Quando si utilizza proxy, si dovrebbe applicare l'annotazione @Transactional solo ai metodi con visibilità pubblica. Se si annotano i metodi protetti, private o package-visible con l'annotazione @Transactional, , non viene generato alcun errore, ma il metodo annotato non mostra le impostazioni transazionali configurate per . Considerare l'uso di AspectJ (vedi qui sotto) se è necessario annotare metodi non pubblici.

Problemi correlati