Spring Template (3)-ORM design and implementation
Generally, the most used ORM is Hibernate. In addition to Hibernate classes such as Session and SessionFactory, you also need to handle issues such as transaction processing, opening sessions, and closing sessions, to some extent, it makes it more difficult to use Hibernate. The Hibernate encapsulation provided by Spring, such as HibernateDaoSupport and HIbernateTemplate, simplifies these general processes.
Spring's ORM package provides support for many ORM products. The Template class provided by Spring is usually used. These template classes encapsulate the main data operation methods, such as query and update. In the Templdate encapsulation, sessions in Hibernate are processed, connection processing and transaction processing. Hibernate's persistent data operations are encapsulated into Spring's unified Transaction Processing Framework, which is implemented through Spring's AOP.
Class diagram:
DaoSupport is a core class that supports HIbernate operations through HIbernateTemplate.
Spring's ORM module is not re-developed. It encapsulates the use of Hibernate through the IOC container and the AOP module. To use HIbernate, You need to configure HIbernate. These configurations are completed through SessionFactory. In the HIbernate module of Spring, LocalSessionFactoryBean is provided to encapsulate SessionFactory configurations. This LocalSessionFactory encapsulation is used, the SessionFactory configuration information can be defined by Bean and injected into the SessionFactory singleton object instantiated in the IOC container. The LocalSessionFactoryBean design laid the foundation for HIbernate's use.
Taking hibernateTemplate as an Example
Similar to the use of JdbcTemplate, Spring uses the same mode and uses the execute callback. As follows:
Code
Public
T execute (HibernateCallback
Action) throws DataAccessException {return doExecute (action, false, false);} protected
T doExecute (HIbernateCallback
Action, boolean enforceNewSession, boolean enforceNativeSession) throws DataAccessException {Assert. notNull (action, "Callback object must not be null"); // obtain the HIbernate Session and determine whether to force a new Session. // if necessary, you can directly open a new session through SessionFactory. Otherwise, you need to use SessionSession session = (enforceNewSession? SessionFactoryUtils. getNewSession (getSessionFactory (), getEntityInterceptor (): getSession (); // determines whether Transaction already exists. If yes, the sessionboolean existingTransaction = (! EnforceNewSession &&(! IsAllowCreate () | SessionFactoryUtils. isSessionTransactional (session, getsessionFactory (); if (existingTransaction) {logger. debug ("Found thread-bound Session for HIbernateTemplate");} FlushMode previusflushmode = null; try {previousFlushMode = applyFlushMOde (session, existingTransaction); enableFilters (session ); session sessionToExpose = (enforceNativeSession | isExposeNativeSession ()? Session: createSessionProxy (session); // This is the call to the callback function in HIbernateCallback. The Session as a parameter can be used by the callback function T result = action. doInHibernate (sessionToExpose); flushIfNecessary (session, existingTransaction); return result;} catch (HibernateException ex) {throw convertHibernateAccessException (ex);} catch (SQLException ex) {throw convertJdbcAccessException (ex);} catch (RuntimeException ex) {throw ex; // If Transaction exists, the current callback is complete After the session, do not close this session} finally {if (existingTransaction) {logger. debug ("Not closing pre-bound Hibernate Session after HibernateTemplate"); disableFilters (session); if (previusflushmode! = Null) {session. setFlushMode (previusflushmode) ;}}// if Transaction does not exist, disable the current Sessionelse {if (isAlwaysUseNewSession () {SessionFactoryUtils. closeSession (session);} else {SessionFactoryUtils. closeSessionOrRegisterDeferredClose (session, getSessionFactory ());}}}}
Summary
Spring encapsulates transaction processing and sessions through HIbernateTemplate without directly performing operations on sessions.
Spring does not provide specific ORM implementation, but only provides an integrated environment and platform for the application to use the orm product. In addition, the Hibernate APIs encapsulated by Spring facilitate users.