获取“HibernateException:没有 Hibernate 会话绑定到线程”,&配置不允许创建..”在执行 Spring Hibernate 时

发布于 2024-12-11 17:07:36 字数 3181 浏览 0 评论 0原文

我正在尝试编写一个带有事务管理支持的简单 spring-hibernate 命令行应用程序,但我不断得到 “org.hibernate.HibernateException:没有 Hibernate Session 绑定到线程,并且配置不允许在此处创建非事务性会话”

我的 Spring 配置文件(applicationContext.xml)

    <!-- Context Provider -->
<bean class="com.rps.util.ContextUtil" />
<!-- The transactional service objects -->
<bean id="service" class="com.rps.service.DefaultService" p:parentDao-ref="parentDao" p:childDao-ref="childDao" />

<!--the transactional advice-->
<tx:advice id="txAdvice" transaction-manager="txManager">
    <tx:attributes>
        <tx:method name="get*" read-only="true" />
        <tx:method name="*" />
    </tx:attributes>
</tx:advice>

<!--this transactional advice runs for any service method execution-->
<aop:config>
    <aop:advisor pointcut="execution(* com.rps.service..*.*(..))" advice-ref="txAdvice" />
</aop:config>

<!-- The PlatformTransactionManager -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" p:sessionFactory-ref="sessionFactory" />

<!-- The DataSource -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" p:driverClassName="com.mysql.jdbc.Driver" p:url="jdbc:mysql://localhost:3306/OsivTest" p:username="XXX" p:password="YYY" />

<!-- Session Factory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" p:dataSource-ref="dataSource">
    <property name="mappingResources">
        <list>
            <value>com/rps/domain/Parent.hbm.xml</value>
            <value>com/rps/domain/Child.hbm.xml</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
        </props>
    </property>
</bean>

<!-- Data Access Objects -->
<bean id="parentDao" class="com.rps.data.hbm.HibernateParentDao" p:sessionFactory-ref="sessionFactory" />
<bean id="childDao" class="com.rps.data.hbm.HibernateChildDao" p:sessionFactory-ref="sessionFactory" />

DAO 代码(Session Factory 对象集)通过上面上下文文件中配置的 IoC 容器)

public List<T> findAll() {
    return sessionFactory.getCurrentSession().createCriteria(getPersistentClass()).list();
}

DefaultService 类:(从 IoC 容器注入的parentDao)

public class DefaultService implements Service {

@Override
public List<Parent> getAllParent() {
    return parentDao.findAll();
}
    //Other methods
}

主要方法代码:

public static void main(String[] args) {

    ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    Service s = (Service) context.getBean("service");
    s.getAllParent();
}

我正在使用 hibernate 版本 3.1.3 和 spring 3。 我们如何为每个线程配置一个会话?我认为问题在于没有会话与我的执行线程关联。我假设事务管理器将打开一个会话(如果尚未找到),但它不会这样做。

I am trying to write a simple spring-hibernate command line application with transaction management support, but I am constantly getting
"org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here"

my spring config file (applicationContext.xml)

    <!-- Context Provider -->
<bean class="com.rps.util.ContextUtil" />
<!-- The transactional service objects -->
<bean id="service" class="com.rps.service.DefaultService" p:parentDao-ref="parentDao" p:childDao-ref="childDao" />

<!--the transactional advice-->
<tx:advice id="txAdvice" transaction-manager="txManager">
    <tx:attributes>
        <tx:method name="get*" read-only="true" />
        <tx:method name="*" />
    </tx:attributes>
</tx:advice>

<!--this transactional advice runs for any service method execution-->
<aop:config>
    <aop:advisor pointcut="execution(* com.rps.service..*.*(..))" advice-ref="txAdvice" />
</aop:config>

<!-- The PlatformTransactionManager -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" p:sessionFactory-ref="sessionFactory" />

<!-- The DataSource -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" p:driverClassName="com.mysql.jdbc.Driver" p:url="jdbc:mysql://localhost:3306/OsivTest" p:username="XXX" p:password="YYY" />

<!-- Session Factory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" p:dataSource-ref="dataSource">
    <property name="mappingResources">
        <list>
            <value>com/rps/domain/Parent.hbm.xml</value>
            <value>com/rps/domain/Child.hbm.xml</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
        </props>
    </property>
</bean>

<!-- Data Access Objects -->
<bean id="parentDao" class="com.rps.data.hbm.HibernateParentDao" p:sessionFactory-ref="sessionFactory" />
<bean id="childDao" class="com.rps.data.hbm.HibernateChildDao" p:sessionFactory-ref="sessionFactory" />

DAO Code (Session Factory object set by IoC container as configured in context file above)

public List<T> findAll() {
    return sessionFactory.getCurrentSession().createCriteria(getPersistentClass()).list();
}

DefaultService Class: (parentDao injected from IoC container)

public class DefaultService implements Service {

@Override
public List<Parent> getAllParent() {
    return parentDao.findAll();
}
    //Other methods
}

Main method code:

public static void main(String[] args) {

    ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    Service s = (Service) context.getBean("service");
    s.getAllParent();
}

I am using hibernate version 3.1.3 and spring 3.
How do we configure one session per thread? I believe the issues is that no session is being associated with my execution thread. I was assuming that transaction manager will open a session if not already found, but it does not do so.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

_蜘蛛 2024-12-18 17:07:36

引用1:

我使用的是 hibernate 版本 3.1.3 和 spring 3

引用2:

注意
从 Spring 3.0 开始,Spring 需要 Hibernate 3.2 或更高版本。

(来源)

顺便说一句,当前稳定的 Hibernate 版本是 3.6.7

Quote 1:

I am using hibernate version 3.1.3 and spring 3

Quote 2:

Note
As of Spring 3.0, Spring requires Hibernate 3.2 or later.

(Source)

BTW, the current stable Hibernate version is 3.6.7

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文