2011-01-23 61 views
0

我試圖通過Hibernate來連接到Oracle,我得到一個異常Hibernate的異常,同時通過休眠

org.hibernate.HibernateException: 
No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here 
at org.springframework.orm.hibernate3.SpringSessionContext.currentSession(SpringSessionContext.java:63)  
at org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:574) 
at com.mycompany.infra.daoimpl.GenericDAOImpl.findAll(GenericDAOImpl.java:133) 
at com.mycompany.testHibernate.TeamTest.main(TeamTest.java:29) 

我定義的,看起來像這樣的應用程序上下文連接到Oracle: applicationContext.xml中

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" 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"> 

    <!-- There is no use for this bean at current moment. --> <bean id="OnMediaSessionFactory" parent="sessionFactory">  <property name="schemaUpdate" value="${hibernate.schema_update}" />   <property name="hibernateProperties">   <props> 
       <!-- prop key="hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop 
--> 
       <prop key="show_sql">${hibernate.show_sql}</prop> 
       <prop key="connection.provider_class">${hibernate.connection.provider_class}</prop> 
       <prop key="hibernate.dialect">${hibernate.dialect}</prop> 
       <prop key="hibernate.max_fetch_depth">${hibernate.max_fetch_size}</prop> 
       <prop key="hibernate.default_batch_fetch_size">${hibernate.default_batch_fetch_size}</prop> 
       <prop key="hibernate.jdbc.fetch_size">${hibernate.jdbc.fetch_size}</prop> 
       <prop key="hibernate.jdbc.batch_size">${hibernate.jdbc.batch_size}</prop> 
       <prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop> 
       <prop key="hibernate.cache.provider_class">${hibernate.cache.provider_class}</prop> 
       <prop key="hibernate.cache.use_second_level_cache">${hibernate.cache.use_second_level_cache}</prop>    </props>  </property>  </bean> 


    <import resource="classpath:/applicationContext-hibernate.xml" /> 


    <bean id="messageHandlerSpring" class="com.mycompany.messagehandler.MessageHandlerSpringImpl"> </bean> 
      <bean id="omTeamDAO" class="com.mycompany.infra.daoimpl.MutableDAOImpl">  <constructor-arg value="com.mycompany.model.OmTeam" />  








我的測試連接看起來是這樣的:

ApplicationContext ctx = new ClassPathXmlApplicationContext(
       "applicationContext.xml");  SessionFactory sessions = (SessionFactory) ctx 
       .getBean("OnMediaSessionFactory"); 

     GenericDAO<OmTeam> dao = new MutableDAOImpl<OmTeam>(OmTeam.class,sessions, 10); 
       try {   Collection<OmTeam> teams = dao.findAll();   for (OmTeam team : teams) { 
       System.out.println(team.getOmTeamName());   }  } catch (Exception e) {    System.out.println("Exception while loading teams");   e.printStackTrace(); 
    } 

在另一個項目中的以下行工作:

集合團隊= dao.findAll();

我的代碼有什麼問題?

我很想得到一個非常快速有效的答案.. 謝謝!

Rivki。

+1

如果您花時間格式化粘貼的代碼,則您的問題更有可能得到解答。目前,由於幾行很長的行和不好的縮進,讀起來非常困難。 – Codo 2011-01-23 11:21:17

回答

1

Spring不允許非事務性的hibernate訪問。所以如果你添加事務訪問 - 你的異常應該消失。