Spring MVC中的HibernateTransactionManager错误

时间:2010-11-02 17:22:31

标签: java hibernate spring-mvc

HI,

我正在尝试将用户从Spring MVC控制器添加到数据库。我得到以下例外:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
 org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:656)
 org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

我的代码如下:

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  <property name="driverClassName" value="org.postgresql.Driver" />
  <property name="url" value="jdbc:postgresql://127.0.0.1/doolloop2" />
  <property name="username" value="doolloop2" />
  <property name="password" value="doolloop" />
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  <property name="dataSource" ref="dataSource" />
     <property name="mappingLocations">
      <list>
        <value>WEB-INF/mapping/User.hbm.xml</value>
      </list>
    </property>
    <property name="hibernateProperties">
 <props>
  <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop> 
  <prop key="hibernate.show_sql">true</prop> 
  <prop key="hibernate.hbm2ddl.auto">update</prop> 
  </props>
  </property>
</bean>
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <property name="sessionFactory" ref="sessionFactory" />
</bean>

<bean id="txService" class="com.doolloop.services.TxDataBaseServiceImpl">
        <property name="sessionFactory">
        <ref local="sessionFactory"/>
        </property>
    </bean>

然后我的服务看起来像这样:

public class TxDataBaseServiceImpl implements TxDataBaseService {

 private SessionFactory sessionFactory;

 public void setSessionFactory (SessionFactory sessionFactory)
    {
       this.sessionFactory = sessionFactory;
    }

 @Override
 @Transactional
 public void add(User user) {
  Session session = this.sessionFactory.getCurrentSession();

   Transaction tr = session.getTransaction();
     tr.begin();
     session.save(user);
     tr.commit();
 }    
}

现在在我的Servlet上我正在接通课程

 @Autowired
 private TxDataBaseServiceImpl service;



     @RequestMapping(value="/signup.dlp",method = RequestMethod.POST)
       public ModelAndView addUser(@Valid User user, BindingResult result){
        logger.info("Return View");
        registrationValidation.validate(user, result);
                /// all required validations....
      service.add(user);
       ModelAndView mv = new ModelAndView("/Accounts/signupSuccess");
       return mv;
    }

问题是我做错了什么?

1 个答案:

答案 0 :(得分:3)

这是错误:

@Autowired
private TxDataBaseServiceImpl service;

您正在尝试自动装配实现类而不是接口,因此您将获得注入的原始bean,而不是事务代理。这样做:

@Autowired
private TxDataBaseService service;

总是喜欢对接口进行编码,因为a)Joshua Bloch如此说:-)和b)Spring中的许多功能都建立在接口之上。