问题
i am new in integration of jsf 2.0 spring 3.1 and hibernate 4.1. how can i change following code, because hibernate 4.0 does not include 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<Customer> findAllCustomer(){
return getHibernateTemplate().find("from Customer");
}
}
i am trying this sample: http://www.mkyong.com/jsf2/jsf-2-0-spring-hibernate-integration-example/
回答1:
i found the solution. i should use session factory instead.
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<Customer> findAllCustomer(){
List list = getSessionFactory().getCurrentSession().createQuery("from Customer").list();
return list;
}
}
回答2:
Another way to get hibernate session via annotation like following
@Autowired
@Qualifier("sessionFactory")
private SessionFactory sessionFactory;
public Session getSession() {
return sessionFactory.getCurrentSession();
}
SessionFactory bean in spring applicationContext.xml
<!-- sessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="packagesToScan">
<list>
<value></value>
</list>
</property>
<property name="hibernateProperties">
<props></props>
</property>
<property name="dataSource">
<ref bean="dataSource" />
</property>
</bean>
回答3:
As Samira said above, substituting "SessionFactory" for "HibernateDaoSupport" is the "correct approach" for any new Spring/Hibernate code:
https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/orm/hibernate4/support/HibernateDaoSupport.html
NOTE: Hibernate access code can also be coded in plain Hibernate style. Hence, for newly started projects, consider adopting the standard Hibernate style of coding data access objects instead, based on SessionFactory.getCurrentSession(). This HibernateTemplate primarily exists as a migration helper for Hibernate 3 based data access code, to benefit from bug fixes in Hibernate 4.x.
HOWEVER ... I also ran against the same problem in one of the Mkyong.com tutorials:
http://www.mkyong.com/spring/maven-spring-hibernate-mysql-example/
I'm using Spring 4.2.4.RELEASE and Hibernate 4.3.8.Final.
The expedient solution for me (to get the tutorial up/running) is to use Spring-orm's built-in support for HibernateDaoSupport. Specifically, I just changed the line with the import from "hibernate3" to "hibernate4":
StockDaoImpl.java =>
package com.mkyong.stock.dao.impl;
...
// import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.orm.hibernate4.support.HibernateDaoSupport;
...
In case anybody runs into the same problem :)
回答4:
You can use Hibernate DAO Support by extending HibernateDAOSupport class and overriding its afterPropertiesSet() method.
This method is called in HibernateDAO support and at that time since sessionFactory is null it is throwing this error. In your custom class you can set this property explicitly and then call the same method of Parent Class (i.e. HibernateDAOSupport's addProperties() method)
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();
}
}
Following can be used for sample!
来源:https://stackoverflow.com/questions/11188633/hibernatedaosupport-in-hibernate-4-0