[英]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.