简体   繁体   English

org.hibernate.HibernateException:没有Hibernate会话绑定到线程

[英]org.hibernate.HibernateException:No Hibernate Session bound to thread

I want to call next statement: 我想打电话给下一条声明:

public List<User> getList(int master_id) {
        Session session = sessionFactory.getCurrentSession();
        try {
            List<User> result = (List<User>) sessionFactory.getCurrentSession()
                    .createQuery("from User WHERE master_id=:master_id")
                    .setInteger("master_id", master_id).list();
            return result;
        } finally {
            session.close();
        }
    }

But then I catch 但是后来我抓住了

No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here 没有Hibernate Session绑定到线程,并且配置不允许在此处创建非事务性会话

Someone says I have to add a @Transactional annotation to this, but as you can see there is no need to do that, because it's SELECT query. 有人说我必须为此添加一个@Transactional批注,但是如您所见,没有必要这样做,因为它是SELECT查询。 What do? 做什么?

hibernate.cfg: hibernate.cfg:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
        <mapping class="ru.tenet.smsc.domain.User" />   
        <mapping class="ru.tenet.smsc.domain.UserRoles" />
        <mapping class="ru.tenet.smsc.domain.WhiteList" />
        <mapping class="ru.tenet.smsc.domain.BlackList" />
        <mapping class="ru.tenet.smsc.domain.Distribution" />
        <mapping class="ru.tenet.smsc.domain.Range" />
        <mapping class="ru.tenet.smsc.domain.SmsEntity" />
        <mapping class="ru.tenet.smsc.domain.Archive" />
        <mapping class="ru.tenet.smsc.domain.Priority" />
    </session-factory>

</hibernate-configuration>

SessionFactory: SessionFactory的:

<bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation">
            <value>/WEB-INF/db/hibernate.cfg.xml</value>
        </property>
        <property name="configurationClass">
            <value>org.hibernate.cfg.AnnotationConfiguration</value>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.show_sql">false</prop>
                <prop key="hibernate.dialect"> org.hibernate.dialect.OracleDialect</prop>
                <prop key="hibernate.connection.useUnicode">true</prop>
                <prop key="hibernate.connection.characterEncoding">UTF-8</prop>
                <prop key="hibernate.connection.charSet">UTF-8</prop>
            </props>
        </property>
    </bean>

@Transactional is not only for insert/updates, is also for SELECTs. @Transactional不仅适用于插入/更新,还适用于SELECT。 You can see this because one of the parameters of the annotation can be readOnly=true (this means your transaction is not going to commit anything) 您可以看到此信息,因为注释的参数之一可以为readOnly = true (这意味着您的事务将不提交任何内容)

You must bind the current session to the current thread 您必须将当前会话绑定到当前线程

Session session = SessionFactoryUtils.getSession(sessionFactory, true);
TransactionSynchronizationManager.bindResource(sessionFactory, 
new SessionHolder(session));
try {
 do your work ...
}
finally {
  TransactionSynchronizationManager.unbindResource(sessionFactory);
  SessionFactoryUtils.closeSessionIfNecessary(session, sessionFactory);
}  

If you have a webapp, then you should add the Spring OpenEntityManagerInViewFilter filter 如果您有一个Web应用程序,则应添加Spring OpenEntityManagerInViewFilter过滤器

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 HTTP状态500-请求处理失败; 嵌套的异常是org.hibernate.HibernateException:没有绑定到线程的Hibernate会话 - HTTP Status 500 - Request processing failed; nested exception is org.hibernate.HibernateException: No Hibernate Session bound to thread Hibernate 4:org.hibernate.HibernateException:当前会话找不到会话 - Hibernate 4 : org.hibernate.HibernateException: No Session found for current thread org.hibernate.HibernateException:使用tx:advise找不到当前线程的会话 - org.hibernate.HibernateException: No Session found for current thread with tx:advise org.hibernate.HibernateException: 没有找到当前线程 5 的会话 - org.hibernate.HibernateException: No Session found for current thread 5 线程“ main” org.hibernate.HibernateException中的异常: - Exception in thread “main” org.hibernate.HibernateException: org.hibernate.HibernateException:找不到会话 - org.hibernate.HibernateException: No Session found HibernateException:没有 Hibernate Session 绑定到线程 - HibernateException: No Hibernate Session bound to thread 迁移到Hibernate 4 + Spring 4.2.2:org.hibernate.HibernateException:无法获取当前线程的事务同步Session - migration to hibernate 4 + spring 4.2.2: org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread Spring + Hibernate应用程序中的问题:org.hibernate.HibernateException:找不到当前线程的Session - Problems in Spring + Hibernate application: org.hibernate.HibernateException: No Session found for current thread org.hibernate.HibernateException错误 - org.hibernate.HibernateException error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM