Hibernate 4.0中的HibernateDaoSupport [英] HibernateDaoSupport in hibernate 4.0

查看:378
本文介绍了Hibernate 4.0中的HibernateDaoSupport的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  import org.springframework.orm.hibernate3.support。 HibernateDaoSupport的; 

$ b $ public class CustomerDaoImpl extends
HibernateDaoSupport implements CustomerDao {

public void addCustomer(Customer customer){

customer。 setCreatedDate(new Date());
getHibernateTemplate()。save(customer);

}

公共列表< Customer> findAllCustomer(){

return getHibernateTemplate()。find(from Customer);



$ / code $ / pre

我尝试这个示例: a href =http://www.mkyong.com/jsf2/jsf-2-0-spring-hibernate-integration-example/ =noreferrer> http://www.mkyong.com/jsf2/jsf -2-0-spring-hibernate-integration-example /

解决方案

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

  import java.util.List; 

import org.hibernate.SessionFactory;

public class CustomerDaoImpl实现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);

}

公共列表< Customer> findAllCustomer(){

List list = getSessionFactory()。getCurrentSession()。createQuery(from Customer)。list();
返回列表;

}
}


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/

解决方案

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;

    }
}

这篇关于Hibernate 4.0中的HibernateDaoSupport的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆