gpt4 book ai didi

java - hibernate连接oracle时出现Hibernate异常

转载 作者:行者123 更新时间:2023-12-01 15:57:43 29 4
gpt4 key购买 nike

我尝试通过 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();
}

以下行在另一个项目中工作:

Collection teams = dao.findAll();

我的代码有什么问题吗?

我很想快速有效地得到答案。谢谢!

里夫基。

最佳答案

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

关于java - hibernate连接oracle时出现Hibernate异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4773499/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com