2011-01-23 91 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访问。所以如果你添加事务访问 - 你的异常应该消失。