没有Hibernate Session绑定到线程,并且配置不允许创建非事务性的

时间:2013-08-07 05:01:26

标签: hibernate

您好我是Spring + hiberent的新手我正在尝试使用GenericDao但是我得到了以下异常没有Hibernate Session绑定到线程,并且配置不允许创建非事务性看到我的代码

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);

}

弹簧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" />

AnyOne帮帮我 我将No Hibernate Session绑定到线程,并且配置不允许在此创建非事务性的     

2 个答案:

答案 0 :(得分:1)

您应该添加<context:component-scan base-package="pathToScan" />

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

答案 1 :(得分:0)

关于您的配置,您完全没有dao / service bean定义,并且您没有启用向XML添加<tx:annotation-driven/>的注释驱动事务(请记住添加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。