簡體   English   中英

通過hibernate連接到oracle時出現Hibernate異常

[英]Hibernate Exception while connecting to oracle by 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();

我的代碼有什么問題?

我希望能快速有效地獲得答案。。謝謝!

里夫基

Spring不允許非事務性的休眠訪問。 因此,如果您添加事務訪問權限,則您的異常應該消失。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM