Hibernate连接到oracle时的Hibernate异常

时间:2011-01-23 11:11:21

标签: java hibernate spring

我正在尝试通过hibernate连接到oracle,并且我得到了一个异常

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)

我定义了一个如下所示的应用程序上下文: 的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 个答案:

答案 0 :(得分:1)

Spring不允许非事务性的hibernate访问。因此,如果您添加了事务访问权限 - 您的异常应该消失。