开发者

Hibernate Exception while connecting to oracle by hibernate

开发者 https://www.devze.com 2023-02-06 19:20 出处:网络
I\'m trying to connect to oracle by hibernate, and I get an exception org.hibernate.HibernateException:

I'm trying to connect to oracle by hibernate, and I get an exception

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)

I defined an application context that looks like this: 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" />      

My test connection looks like this:

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(); 
    }

The following line work in another project:

Collection teams = dao.findAll();

what wrong in my code?

I would love to get an answer very quickly and 开发者_如何转开发efficiently.. Thanks!

Rivki.


Spring doesn't allow for non transactional hibernate access. So if you add transactional access - your exception should go away.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号