【问题标题】:No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional没有 Hibernate Session 绑定到线程,并且配置不允许创建非事务性
【发布时间】:2013-08-07 05:01:25
【问题描述】:

嗨,我是 spring + hiberent 的新手,我正在尝试编写 GenericDao,但出现以下异常 No Hibernate Session bound to thread,并且配置不允许创建非事务性,请参阅我的代码

IGenericDao

public interface IGenericDao<T> {
    public void delete(T obj);
    public void saveOrUpdate(T obj);
}

GenericDaoImpl

  @Transactional
  public class GenericDaoImpl<T> implements IGenericDao<T> {
    @Autowired
    private SessionFactory sessionFactory;
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }
    protected SessionFactory getSessionFactory() {
        if (sessionFactory == null)
            throw new IllegalStateException("SessionFactory has not been set on DAO before usage");
        return sessionFactory;
    }
    @Override
    public void delete(T obj) {
        getSessionFactory().getCurrentSession().delete(obj);
    }
    @Override
    public void saveOrUpdate(T obj) {
        getSessionFactory().getCurrentSession().saveOrUpdate(obj);
    }
}

IGenericService

public interface IGenericService<T extends Object> {
    public void delete(T obj);
    public void saveOrUpdate(T obj);

}

IGenericServiceManagerImpl

public  class IGenericServiceManagerImpl<T extends Object> implements  IGenericService<T>  {

@Autowired
IGenericDao<T> genericDao;
@Override

public void delete(T obj) {
    // TODO Auto-generated method stub
    genericDao.delete(obj);
}

@Override

public void saveOrUpdate(T obj) {
    // TODO Auto-generated method stub
    genericDao.saveOrUpdate(obj);

}

spring-servlet.xml

   xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.1.xsd

       ">

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource">
        <ref bean="dataSource"/>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
        </props>
    </property>


    <property name="annotatedClasses">
        <list>

            <value>com.codes.gdi.model.EMP</value>


        </list>
    </property>
</bean>

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.google.appengine.api.rdbms.AppEngineDriver" />


    <property name="url" value="jdbc:google:rdbms://spring:myinstance/gdirectorystaging" />
    <property name="username" value="" />
    <property name="password" value="" />
</bean>

<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />

任何人帮助我 我没有绑定到线程的休眠会话,并且配置不允许在这里创建非事务性会话

【问题讨论】:

    标签: hibernate


    【解决方案1】:

    你应该添加&lt;context:component-scan base-package="pathToScan" /&gt;

    http://static.springsource.org/spring/docs/3.0.0.M3/reference/html/ch04s12.html (4.12.2)

    【讨论】:

      【解决方案2】:

      关于您的配置,您完全缺少 dao/service bean 定义,并且您没有启用将 &lt;tx:annotation-driven/&gt; 添加到 XML 的注释驱动事务(记得添加 xmlns:tx="http://www.springframework.org/schema/tx"http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd)。

      <beans xmlns="http://www.springframework.org/schema/beans"
          xmlns:tx="http://www.springframework.org/schema/tx"
          xmlns:context="http://www.springframework.org/schema/context"
          xsi:schemaLocation="
              http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
              http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
              http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
      

      如果 DAO 代码仍然无法与 Hibernate3 一起使用,请以这种方式使用 HibernateTemplate

      public class GenericDaoImpl extends HibernateTemplate implements IGenericDao{
        @Override
        public void delete(T obj) {
          getSession().delete(obj);
        }
      }
      

      如果您使用的是 Hibernate4,请保持 DAO 代码不变,但根据新类 (org.springframework.orm.hibernate4.HibernateTransactionManager) 更改 bean 类并使用 org.springframework.orm.hibernate4.LocalSessionFactoryBean 构建 sessionFactory。

      【讨论】:

      • 您好,感谢您尝试添加的帮助,但我得到引用的文件包含错误 (springframework.org/schema/tx)。有关更多信息,请右键单击问题视图中的消息并选择“显示详细信息...”
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多