The difference between HibernateTemplate usage and session

HibernateTemplate usage

HibernateTemplate provides a lot of common methods to complete basic operations, such as the usual operations such as adding, deleting, modifying, and querying. Spring 2.0 adds support for named SQL queries, as well as support for paging. In most cases, using Hibernate's normal usage, you can complete most CRUD operations on DAO objects. The following is a brief introduction to the common methods of HibernateTemplate:

◇ void delete(Object entity): delete the specified persistent instance

◇ deleteAll (Collection entities): delete all persistent class instances in the collection

◇ find(String queryString): Returns a collection of instances according to the HQL query string

◇ findByNamedQuery(String queryName): Returns a collection of instances according to the named query

◇ get(Class entityClass, Serializable id): load an instance of a specific persistent class according to the primary key

◇ save(Object entity): save a new instance

◇ saveOrUpdate(Object entity): Choose to save or update according to the instance state

◇ update(Object entity): Update the state of the instance, requiring the entity to be a persistent state

◇ setMaxResults(int maxResults): Set the size of the paging

 

Here is the source code of a complete DAO class :

public class PersonDAOHibernate implements PersonDAO

{

    //Use log4j to complete the log function during debugging

     private static Log log = LogFactory.getLog(NewsDAOHibernate.class);

    //Save the SessionFactory as a private member variable.

    private SessionFactory sessionFactory;

    //Save HibernateTemplate as a private variable

   private HibernateTemplate hibernateTemplate = null;

    //Set the value to inject the required setter method of SessionFactory

    public void setSessionFactory(SessionFactory sessionFactory)

    {

        this.sessionFactory = sessionFactory;

    }

    //Initialize the HibernateTemplate required by this DAO

    public HIbernateTemplate getHibernateTemplate()

    {

        //First, check if the original hibernateTemplate instance still exists

         if ( hibernateTemplate == null)

        {

             //If it does not exist, create a new HibernateTemplate instance

               hibernateTemplate = new HibernateTemplate(sessionFactory);

         }

         return hibernateTemplate;

     }

    //return all instances of people

    public List getPersons()

    {       

        //Return all instances of Person through the find method of HibernateTemplate

         return getHibernateTemplate().find("from Person");

    }

    /**

     * Return a specific instance based on the primary key

      * @return the Person instance corresponding to a specific primary key

      * @param primary key value

      */

     public News getNews(int personid)

     {

         return (Person)getHibernateTemplate().get(Person.class, new Integer(personid));

     }

    /**

     * @ person 需要保存的Person实例

      */

    public void savePerson(Person person)

    {                

        getHibernateTemplate().saveOrUpdate(person);

    }

    /**

     * @ param personid 需要删除Person实例的主键

      */

    public void removePerson(int personid)

    {

          //先加载特定实例

           Object p = getHibernateTemplate().load(Person.class, new Integer(personid));

          //删除特定实例

           getHibernateTemplate().delete(p);

    }

}

 

HibernateTemplate与session的区别

使用方法没有多大的区别

我们使用HibernateTemplate,有一个很重要的原因就在于我们不想直接控制事务,不想直接去获取,打开Session,开始一个事务,处理异常,提交一个事务,最后关闭一个SessionHibernateTemplate 是Hibernate操作进行封装,我们只要简单的条用HibernateTemplate 对象,传入hql和参数,就获得查询接口,至于事务的开启,关闭,都交给HibernateTemplate  对象来处理我们自己只专注于业务,不想去作这些重复而繁琐的操作。我们把这些责任全部委托给了 HibernateTemplate,然后使用声明式的配置来实现这样的功能。如果我们通过类似getSession()这样的方法获得了Session,那就意味着我们放弃了上面所说的一切好处。

在使用Spring的时候 DAO类继承了 HibernateDaoSupport 类又因为HibernateDaoSupport 类里面有个属性 hibernateTemplate;所以就可以进行设置注,也就是Spring的一大优点面向切面式编程,进行设置注入,在Tomcat启动的时候由 Tomcat 加载 ApplicationContext.xml,配置文件给 hibernateTemplate赋值,这样的话就实现了,在使用某个对象之前不用给他实例化

转自:http://blog.163.com/zsq303288862@126/blog/static/9374596120111123576334/

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326458573&siteId=291194637