Detailed description of HibernateDaoSupport (commonly used Dao during addition, deletion, modification, and query)

Source: Internet
Author: User

1. Spring provides the tool class HibernateDaoSupport for Hibernate DAO. This class mainly provides two methods: public final HibernateTemplate getHibernateTemplate (); public final void setSessionFactory (SessionFactory sessionFactory); The setSessionFactory method receives dependency injection from Spring applicationContext, the SessionFactory instance configured in Spring is received. The getHibernateTemplate method is used to generate a Session using the SessionFactory, and then generate the HibernateTemplate to access the database. The typical DAO code inheriting HibernateDaoSupport is as follows: public class UserDAOImpl extends HibernateDaoSupport implements UserDAO {public void save (Users transientInstance) {log. debug ("saving Users instance"); try {getHibernateTemplate (). save (transientInstance); log. debug ("save successful");} catch (RuntimeException re) {log. error ("save failed", re); throw re ;}}..................} In fact, the DAO implementation still relies on the template access method of HibernateTemplate. However, the work of HibernateDaoSupport injecting dependencies into SessionFactory has been completed, and the work of obtaining HibernateTemplate has also been completed. Note: This method must be configured with SessionFactory in the Spring configuration file. In the DAO implementation inherited by HibrnateDaoSupport, Hibernate Session management is managed by Spring instead of opening Hibernate code. Based on actual operations, Spring uses the "open session once every transaction" policy to automatically improve the performance of database access. 2. Advantages and Disadvantages of HibernateDaoSupport: Do not use Hiberenate or Spring for Hibernate when writing Dao classes: currently, when writing DAO, we generally directly inherit the hibernate encapsulation class HibernateDaoSupport provided by spring, and then use the class such as saveOrUpdate (), saveOrUpdateCopy (), find (), and so on. In addition, when using the excute () method to implement some more complex hibernate functions, hibernate classes such as Query, Session, and Type are also used. In this way, the direct use of spring and hibernate classes has the problem that your code will have to depend on a version of spring and hibernate. For example, if hibernate3 is released, the changes are quite large. In fact, the most terrible thing is the packet structure. The packet structure of hibernate2 is net. sf. hibernate. * However, hibernate3 is org. hibernate. *. Similarly, to support hibernate3, the package name is changed to org. springframework. orm. hibernate3 .*. If you are developing a new project, it doesn't matter. If you are upgrading a project, it will be a problem. If you want to upgrade your project from hibernate2 to hibernate3, You have to modify all references to hibernate and spring-hibernate in DAO. If your code contains methods and classes that are incompatible with hibernate2 and hibernate3, such as saveOrUpdateCopy () (no more in hibernate3), you will have to rewrite it. So you may say that I won't upgrade it like this. If your software has been upgraded from hibernate to 4 to 5 for many years, do you still use hibernate2? If you develop a platform in this way, can you require that all software projects that use your platform only use hibernate2? Furthermore, if I develop a product, there will be tens of thousands of customers in the future. After 1 or 2 years, I need to upgrade. At this time, my upgrade package has dozens of MB, and almost all DAO has been changed. Such an upgrade is tantamount to reinstalling. Cause Analysis: DAO in our project depends on hibernate and spring, because our use of DAO is inheritance, which is a strong association and dependency. solution 1: we only need to make some adjustments to solve this problem, that is, we do not use direct inheritance, but use interfaces for separation. You can use the Fa pipeline de mode to first create a base class named BasicDao. We can see from the name that it is the base class for all DAO operations, such as save (), delete (), load (), query (), and other methods, in addition to some basic methods, such as paging query, getCount, and parsing query conditions to form HQL statements, are also implemented here, however, do not use any methods or classes related to hibernate or spring. At the same time, BasicDao calls a DaoSupport interface. The DaoSupport interface is the basic method and the most primitive element required for persistence. Then, I provide different implementations for the DaoSupport interface, such as the implementation of hibernate2 DaoSupportHibernateImp and hibernate3 DaoSupportHibernate3Imp. The entire structure is shown in. BasicDao can be used using methods provided by hibernate or spring, but not directly, but by calling the DaoSupport implementation class. However, BasicDao is the implementation class used. We use spring IoC and the configuration file to determine which implementation to use. At the same time, BasicDao should not use classes such as SpringContext to implement IoC. Instead, it should establish the setDaoSupport () and getDaoSupport () methods and create references in the spring configuration file.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.