HibernateDaoSupport在hibernate 4.0中

我是jsf 2.0 spring 3.1和hibernate 4.1集成的新手。 我如何更改以下代码,因为hibernate 4.0不包含HibernateDaoSupport。

import org.springframework.orm.hibernate3.support.HibernateDaoSupport; public class CustomerDaoImpl extends HibernateDaoSupport implements CustomerDao{ public void addCustomer(Customer customer){ customer.setCreatedDate(new Date()); getHibernateTemplate().save(customer); } public List findAllCustomer(){ return getHibernateTemplate().find("from Customer"); } } 

我正在尝试这个样本: http : //www.mkyong.com/jsf2/jsf-2-0-spring-hibernate-integration-example/

我找到了解决方案。 我应该使用会话工厂。

 import java.util.List; import org.hibernate.SessionFactory; public class CustomerDaoImpl implements CustomerDao{ private SessionFactory sessionFactory; public SessionFactory getSessionFactory() { return sessionFactory;} public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } public void addCustomer(Customer customer){ getSessionFactory().getCurrentSession().save(customer); } public List findAllCustomer(){ List list = getSessionFactory().getCurrentSession().createQuery("from Customer").list(); return list; } } 

另一种通过注释获得hibernate会话的方法,如下所示

 @Autowired @Qualifier("sessionFactory") private SessionFactory sessionFactory; public Session getSession() { return sessionFactory.getCurrentSession(); } 

Spring ApplicationContext.xml中的SessionFactory bean

                

正如Samira所说,用“SessionFactory”代替“HibernateDaoSupport”是任何新的Spring / Hibernate代码的“正确方法”:

https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/orm/hibernate4/support/HibernateDaoSupport.html

注意:Hibernate访问代码也可以用简单的Hibernate风格编码。 因此,对于新启动的项目,请考虑采用基于SessionFactory.getCurrentSession()的标准Hibernate样式的编码数据访问对象。 这个HibernateTemplate主要作为基于Hibernate 3的数据访问代码的迁移帮助程序存在,从Hibernate 4.x中的错误修复中受益。

但是……我也在一个Mkyong.com教程中遇到了同样的问题:

http://www.mkyong.com/spring/maven-spring-hibernate-mysql-example/

我正在使用Spring 4.2.4.RELEASE和Hibernate 4.3.8.Final。

对我来说(使教程启动/运行)的权宜之计是使用Spring-orm对HibernateDaoSupport的内置支持。 具体来说,我只是将导入从“hibernate3”更改为“hibernate4”:

StockDaoImpl.java =>

 package com.mkyong.stock.dao.impl; ... // import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import org.springframework.orm.hibernate4.support.HibernateDaoSupport; ... 

万一有人遇到同样的问题:)

您可以通过扩展HibernateDAOSupport类并重写其afterPropertiesSet()方法来使用Hibernate DAO Support。

在HibernateDAO支持中调用此方法,此时由于sessionFactory为null,因此抛出此错误。 在您的自定义类中,您可以显式设置此属性,然后调用父类的相同方法(即HibernateDAOSupport的addProperties()方法)

 package com.techcielo.spring4.hibernate.template; import org.hibernate.SessionFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.orm.hibernate3.HibernateTemplate; import org.springframework.stereotype.Component; @Component("hibernateTemplate") public class Hibernate4CustomTemplate extends HibernateTemplate{ @Autowired(required=true) private SessionFactory sessionFactory; public void setSessionFactory(SessionFactory sessionFactory) { System.out.println("Setting SessionFactory"); this.sessionFactory = sessionFactory; super.setSessionFactory(sessionFactory); } @Override public void afterPropertiesSet() { System.out.println("Checking if properties set..."+this.sessionFactory); setSessionFactory(sessionFactory); super.afterPropertiesSet(); } } 

以下可用于样品 !