gpt4 book ai didi

java - Spring ORM 4.0.5 和 Hibernate 4.3.5 - 无法保存到数据库

转载 作者:行者123 更新时间:2023-11-29 08:48:33 31 4
gpt4 key购买 nike

我正在使用 Spring ORM 4.0.5 和 Hibernate 4.3.5,但遇到了困难

当我尝试从 tomcat 调用 dao 方法时,出现以下错误

    20140527 12:25:00,031 IST DEBUG hibernate4.HibernateTemplate [Check App Scheduler_Worker-1] Could not retrieve pre-bound Hibernate session
org.hibernate.HibernateException: No Session found for current thread
at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:106)
at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1014)
at org.springframework.orm.hibernate4.HibernateTemplate.doExecute(HibernateTemplate.java:325)

我正在通过我的操作中的服务层调用我的 Dao 方法。当我使用相同的 spring xml 文件从 junit 对 dao 运行测试时,它运行得非常好。

这是我的 spring 配置文件的内容——数据源是单独配置和加载的。

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">

<!-- Hibernate session factory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="appDataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>

</props>
</property>
<property name="mappingResources">
<list>
<value>config/hibernateMappings/User.hbm.xml</value>
</list>
</property>

</bean>

<tx:advice id="txAdvice" transaction-manager="transactionManager">
<!-- the transactional semantics... -->
<tx:attributes>
<!-- all methods starting with get are read-only -->
<tx:method name="get*" read-only="true"/>
<!-- other methods use the default transaction settings (see below) -->
<tx:method name="" read-only="false"/>
</tx:attributes>
</tx:advice>

<aop:config>
<aop:pointcut id="servicesOperation" expression="execution(* com.app.services.user..*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="servicesOperation"/>
</aop:config>

非常感谢对此的任何帮助

最佳答案

M. Deinum 指出了错误

我已经为我配置了以下内容

<tx:method name="" read-only="false"/>

通过将其更改为以下内容,一切正常

<tx:method name="*" read-only="false"/>

关于java - Spring ORM 4.0.5 和 Hibernate 4.3.5 - 无法保存到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23888237/

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