Springソースコード解析(八):Spring駆動Hibernameの実現


O/Rツールが現れると、多くの複雑な情報の持続化の開発が簡略化される.Springアプリケーション開発者は、Springによって提供されたO/Rスキームを通して、Hibernateなどの様々な耐久性ツールをより便利に使用することができる.ここではSpring+HibernameのSpringについて簡単な分析を行います.
SpringのHibstranceに対する配置はLocal Session FactoryBeanによって完成されています.これは工場Beanの実現で、ベースクラスのAbstractoryBernにおいて:
/**  
 *   FactoryBean         ,       sessionFactory    
 */  
public Object getObject() {   
    return this.sessionFactory;   
}  

    /**
     *   FactoryBean         ,       sessionFactory  
     */
    public Object getObject() {
        return this.sessionFactory;
    }

    afterPropertySet   : 
Java   
public void afterPropertiesSet() throws Exception {   
    //  buildSessionFactory         SessionFactory      
    SessionFactory rawSf = buildSessionFactory();   
    //     Proxy     getCurrentSession   ,        session   
    this.sessionFactory = wrapSessionFactoryIfNecessary(rawSf);   
}  

    public void afterPropertiesSet() throws Exception {
        //  buildSessionFactory         SessionFactory   
        SessionFactory rawSf = buildSessionFactory();
        //     Proxy     getCurrentSession   ,        session
        this.sessionFactory = wrapSessionFactoryIfNecessary(rawSf);
    }

     SessionFactory      ,      ,     Hibernate SessionFactory     : 
Java   
protected SessionFactory buildSessionFactory() throws Exception {   
    SessionFactory sf = null;   
  
    // Create Configuration instance.   
    Configuration config = newConfiguration();   
  
    //       ,     ,LobHander Holder ,  Holder   ThreadLocal  ,                
    if (this.dataSource != null) {   
        // Make given DataSource available for SessionFactory configuration.   
        configTimeDataSourceHolder.set(this.dataSource);   
    }   
  
    if (this.jtaTransactionManager != null) {   
        // Make Spring-provided JTA TransactionManager available.   
        configTimeTransactionManagerHolder.set(this.jtaTransactionManager);   
    }   
  
    if (this.lobHandler != null) {   
        // Make given LobHandler available for SessionFactory configuration.   
        // Do early because because mapping resource might refer to custom types.   
        configTimeLobHandlerHolder.set(this.lobHandler);   
    }   
  
    //     Hibernate        ,     Configuration           
    try {   
        // Set connection release mode "on_close" as default.   
        // This was the case for Hibernate 3.0; Hibernate 3.1 changed   
        // it to "auto" (i.e. "after_statement" or "after_transaction").   
        // However, for Spring's resource management (in particular for   
        // HibernateTransactionManager), "on_close" is the better default.   
        config.setProperty(Environment.RELEASE_CONNECTIONS, ConnectionReleaseMode.ON_CLOSE.toString());   
  
        if (!isExposeTransactionAwareSessionFactory()) {   
            // Not exposing a SessionFactory proxy with transaction-aware   
            // getCurrentSession() method -> set Hibernate 3.1 CurrentSessionContext   
            // implementation instead, providing the Spring-managed Session that way.   
            // Can be overridden by a custom value for corresponding Hibernate property.   
            config.setProperty(Environment.CURRENT_SESSION_CONTEXT_CLASS,   
                    "org.springframework.orm.hibernate3.SpringSessionContext");   
        }   
  
        if (this.entityInterceptor != null) {   
            // Set given entity interceptor at SessionFactory level.   
            config.setInterceptor(this.entityInterceptor);   
        }   
  
        if (this.namingStrategy != null) {   
            // Pass given naming strategy to Hibernate Configuration.   
            config.setNamingStrategy(this.namingStrategy);   
        }   
  
        if (this.typeDefinitions != null) {   
            // Register specified Hibernate type definitions.   
            Mappings mappings = config.createMappings();   
            for (int i = 0; i < this.typeDefinitions.length; i++) {   
                TypeDefinitionBean typeDef = this.typeDefinitions[i];   
                mappings.addTypeDef(typeDef.getTypeName(), typeDef.getTypeClass(), typeDef.getParameters());   
            }   
        }   
  
        if (this.filterDefinitions != null) {   
            // Register specified Hibernate FilterDefinitions.   
            for (int i = 0; i < this.filterDefinitions.length; i++) {   
                config.addFilterDefinition(this.filterDefinitions[i]);   
            }   
        }   
  
        if (this.configLocations != null) {   
            for (int i = 0; i < this.configLocations.length; i++) {   
                // Load Hibernate configuration from given location.   
                config.configure(this.configLocations[i].getURL());   
            }   
        }   
  
        if (this.hibernateProperties != null) {   
            // Add given Hibernate properties to Configuration.   
            config.addProperties(this.hibernateProperties);   
        }   
  
        if (this.dataSource != null) {   
            boolean actuallyTransactionAware =   
                    (this.useTransactionAwareDataSource || this.dataSource instanceof TransactionAwareDataSourceProxy);   
            // Set Spring-provided DataSource as Hibernate ConnectionProvider.   
            config.setProperty(Environment.CONNECTION_PROVIDER,   
                    actuallyTransactionAware ?   
                    TransactionAwareDataSourceConnectionProvider.class.getName() :   
                    LocalDataSourceConnectionProvider.class.getName());   
        }   
  
        if (this.jtaTransactionManager != null) {   
            // Set Spring-provided JTA TransactionManager as Hibernate property.   
            config.setProperty(   
                    Environment.TRANSACTION_MANAGER_STRATEGY, LocalTransactionManagerLookup.class.getName());   
        }   
  
        if (this.mappingLocations != null) {   
            // Register given Hibernate mapping definitions, contained in resource files.   
            for (int i = 0; i < this.mappingLocations.length; i++) {   
                config.addInputStream(this.mappingLocations[i].getInputStream());   
            }   
        }   
  
        if (this.cacheableMappingLocations != null) {   
            // Register given cacheable Hibernate mapping definitions, read from the file system.   
            for (int i = 0; i < this.cacheableMappingLocations.length; i++) {   
                config.addCacheableFile(this.cacheableMappingLocations[i].getFile());   
            }   
        }   
  
        if (this.mappingJarLocations != null) {   
            // Register given Hibernate mapping definitions, contained in jar files.   
            for (int i = 0; i < this.mappingJarLocations.length; i++) {   
                Resource resource = this.mappingJarLocations[i];   
                config.addJar(resource.getFile());   
            }   
        }   
  
        if (this.mappingDirectoryLocations != null) {   
            // Register all Hibernate mapping definitions in the given directories.   
            for (int i = 0; i < this.mappingDirectoryLocations.length; i++) {   
                File file = this.mappingDirectoryLocations[i].getFile();   
                if (!file.isDirectory()) {   
                    throw new IllegalArgumentException(   
                            "Mapping directory location [" + this.mappingDirectoryLocations[i] +   
                            "] does not denote a directory");   
                }   
                config.addDirectory(file);   
            }   
        }   
  
        if (this.entityCacheStrategies != null) {   
            // Register cache strategies for mapped entities.   
            for (Enumeration classNames = this.entityCacheStrategies.propertyNames(); classNames.hasMoreElements();) {   
                String className = (String) classNames.nextElement();   
                String[] strategyAndRegion =   
                        StringUtils.commaDelimitedListToStringArray(this.entityCacheStrategies.getProperty(className));   
                if (strategyAndRegion.length > 1) {   
                    config.setCacheConcurrencyStrategy(className, strategyAndRegion[0], strategyAndRegion[1]);   
                }   
                else if (strategyAndRegion.length > 0) {   
                    config.setCacheConcurrencyStrategy(className, strategyAndRegion[0]);   
                }   
            }   
        }   
  
        if (this.collectionCacheStrategies != null) {   
            // Register cache strategies for mapped collections.   
            for (Enumeration collRoles = this.collectionCacheStrategies.propertyNames(); collRoles.hasMoreElements();) {   
                String collRole = (String) collRoles.nextElement();   
                String[] strategyAndRegion =   
                        StringUtils.commaDelimitedListToStringArray(this.collectionCacheStrategies.getProperty(collRole));   
                if (strategyAndRegion.length > 1) {   
                    config.setCollectionCacheConcurrencyStrategy(collRole, strategyAndRegion[0], strategyAndRegion[1]);   
                }   
                else if (strategyAndRegion.length > 0) {   
                    config.setCollectionCacheConcurrencyStrategy(collRole, strategyAndRegion[0]);   
                }   
            }   
        }   
  
        if (this.eventListeners != null) {   
            // Register specified Hibernate event listeners.   
            for (Iterator it = this.eventListeners.entrySet().iterator(); it.hasNext();) {   
                Map.Entry entry = (Map.Entry) it.next();   
                Assert.isTrue(entry.getKey() instanceof String, "Event listener key needs to be of type String");   
                String listenerType = (String) entry.getKey();   
                Object listenerObject = entry.getValue();   
                if (listenerObject instanceof Collection) {   
                    Collection listeners = (Collection) listenerObject;   
                    EventListeners listenerRegistry = config.getEventListeners();   
                    Object[] listenerArray =   
                            (Object[]) Array.newInstance(listenerRegistry.getListenerClassFor(listenerType), listeners.size());   
                    listenerArray = listeners.toArray(listenerArray);   
                    config.setListeners(listenerType, listenerArray);   
                }   
                else {   
                    config.setListener(listenerType, listenerObject);   
                }   
            }   
        }   
  
        // Perform custom post-processing in subclasses.   
        postProcessConfiguration(config);   
  
        //      Configuration    SessionFactory      
        logger.info("Building new Hibernate SessionFactory");   
        this.configuration = config;   
        sf = newSessionFactory(config);   
    }   
    //                
    finally {   
        if (this.dataSource != null) {   
            // Reset DataSource holder.   
            configTimeDataSourceHolder.set(null);   
        }   
  
        if (this.jtaTransactionManager != null) {   
            // Reset TransactionManager holder.   
            configTimeTransactionManagerHolder.set(null);   
        }   
  
        if (this.lobHandler != null) {   
            // Reset LobHandler holder.   
            configTimeLobHandlerHolder.set(null);   
        }   
    }   
  
    // Execute schema update if requested.   
    if (this.schemaUpdate) {   
        updateDatabaseSchema();   
    }   
  
    return sf;   
}  

    protected SessionFactory buildSessionFactory() throws Exception {
        SessionFactory sf = null;

        // Create Configuration instance.
        Configuration config = newConfiguration();

        //       ,     ,LobHander Holder ,  Holder   ThreadLocal  ,             
        if (this.dataSource != null) {
            // Make given DataSource available for SessionFactory configuration.
            configTimeDataSourceHolder.set(this.dataSource);
        }

        if (this.jtaTransactionManager != null) {
            // Make Spring-provided JTA TransactionManager available.
            configTimeTransactionManagerHolder.set(this.jtaTransactionManager);
        }

        if (this.lobHandler != null) {
            // Make given LobHandler available for SessionFactory configuration.
            // Do early because because mapping resource might refer to custom types.
            configTimeLobHandlerHolder.set(this.lobHandler);
        }

        //     Hibernate        ,     Configuration        
        try {
            // Set connection release mode "on_close" as default.
            // This was the case for Hibernate 3.0; Hibernate 3.1 changed
            // it to "auto" (i.e. "after_statement" or "after_transaction").
            // However, for Spring's resource management (in particular for
            // HibernateTransactionManager), "on_close" is the better default.
            config.setProperty(Environment.RELEASE_CONNECTIONS, ConnectionReleaseMode.ON_CLOSE.toString());

            if (!isExposeTransactionAwareSessionFactory()) {
                // Not exposing a SessionFactory proxy with transaction-aware
                // getCurrentSession() method -> set Hibernate 3.1 CurrentSessionContext
                // implementation instead, providing the Spring-managed Session that way.
                // Can be overridden by a custom value for corresponding Hibernate property.
                config.setProperty(Environment.CURRENT_SESSION_CONTEXT_CLASS,
                        "org.springframework.orm.hibernate3.SpringSessionContext");
            }

            if (this.entityInterceptor != null) {
                // Set given entity interceptor at SessionFactory level.
                config.setInterceptor(this.entityInterceptor);
            }

            if (this.namingStrategy != null) {
                // Pass given naming strategy to Hibernate Configuration.
                config.setNamingStrategy(this.namingStrategy);
            }

            if (this.typeDefinitions != null) {
                // Register specified Hibernate type definitions.
                Mappings mappings = config.createMappings();
                for (int i = 0; i < this.typeDefinitions.length; i++) {
                    TypeDefinitionBean typeDef = this.typeDefinitions[i];
                    mappings.addTypeDef(typeDef.getTypeName(), typeDef.getTypeClass(), typeDef.getParameters());
                }
            }

            if (this.filterDefinitions != null) {
                // Register specified Hibernate FilterDefinitions.
                for (int i = 0; i < this.filterDefinitions.length; i++) {
                    config.addFilterDefinition(this.filterDefinitions[i]);
                }
            }

            if (this.configLocations != null) {
                for (int i = 0; i < this.configLocations.length; i++) {
                    // Load Hibernate configuration from given location.
                    config.configure(this.configLocations[i].getURL());
                }
            }

            if (this.hibernateProperties != null) {
                // Add given Hibernate properties to Configuration.
                config.addProperties(this.hibernateProperties);
            }

            if (this.dataSource != null) {
                boolean actuallyTransactionAware =
                        (this.useTransactionAwareDataSource || this.dataSource instanceof TransactionAwareDataSourceProxy);
                // Set Spring-provided DataSource as Hibernate ConnectionProvider.
                config.setProperty(Environment.CONNECTION_PROVIDER,
                        actuallyTransactionAware ?
                        TransactionAwareDataSourceConnectionProvider.class.getName() :
                        LocalDataSourceConnectionProvider.class.getName());
            }

            if (this.jtaTransactionManager != null) {
                // Set Spring-provided JTA TransactionManager as Hibernate property.
                config.setProperty(
                        Environment.TRANSACTION_MANAGER_STRATEGY, LocalTransactionManagerLookup.class.getName());
            }

            if (this.mappingLocations != null) {
                // Register given Hibernate mapping definitions, contained in resource files.
                for (int i = 0; i < this.mappingLocations.length; i++) {
                    config.addInputStream(this.mappingLocations[i].getInputStream());
                }
            }

            if (this.cacheableMappingLocations != null) {
                // Register given cacheable Hibernate mapping definitions, read from the file system.
                for (int i = 0; i < this.cacheableMappingLocations.length; i++) {
                    config.addCacheableFile(this.cacheableMappingLocations[i].getFile());
                }
            }

            if (this.mappingJarLocations != null) {
                // Register given Hibernate mapping definitions, contained in jar files.
                for (int i = 0; i < this.mappingJarLocations.length; i++) {
                    Resource resource = this.mappingJarLocations[i];
                    config.addJar(resource.getFile());
                }
            }

            if (this.mappingDirectoryLocations != null) {
                // Register all Hibernate mapping definitions in the given directories.
                for (int i = 0; i < this.mappingDirectoryLocations.length; i++) {
                    File file = this.mappingDirectoryLocations[i].getFile();
                    if (!file.isDirectory()) {
                        throw new IllegalArgumentException(
                                "Mapping directory location [" + this.mappingDirectoryLocations[i] +
                                "] does not denote a directory");
                    }
                    config.addDirectory(file);
                }
            }

            if (this.entityCacheStrategies != null) {
                // Register cache strategies for mapped entities.
                for (Enumeration classNames = this.entityCacheStrategies.propertyNames(); classNames.hasMoreElements();) {
                    String className = (String) classNames.nextElement();
                    String[] strategyAndRegion =
                            StringUtils.commaDelimitedListToStringArray(this.entityCacheStrategies.getProperty(className));
                    if (strategyAndRegion.length > 1) {
                        config.setCacheConcurrencyStrategy(className, strategyAndRegion[0], strategyAndRegion[1]);
                    }
                    else if (strategyAndRegion.length > 0) {
                        config.setCacheConcurrencyStrategy(className, strategyAndRegion[0]);
                    }
                }
            }

            if (this.collectionCacheStrategies != null) {
                // Register cache strategies for mapped collections.
                for (Enumeration collRoles = this.collectionCacheStrategies.propertyNames(); collRoles.hasMoreElements();) {
                    String collRole = (String) collRoles.nextElement();
                    String[] strategyAndRegion =
                            StringUtils.commaDelimitedListToStringArray(this.collectionCacheStrategies.getProperty(collRole));
                    if (strategyAndRegion.length > 1) {
                        config.setCollectionCacheConcurrencyStrategy(collRole, strategyAndRegion[0], strategyAndRegion[1]);
                    }
                    else if (strategyAndRegion.length > 0) {
                        config.setCollectionCacheConcurrencyStrategy(collRole, strategyAndRegion[0]);
                    }
                }
            }

            if (this.eventListeners != null) {
                // Register specified Hibernate event listeners.
                for (Iterator it = this.eventListeners.entrySet().iterator(); it.hasNext();) {
                    Map.Entry entry = (Map.Entry) it.next();
                    Assert.isTrue(entry.getKey() instanceof String, "Event listener key needs to be of type String");
                    String listenerType = (String) entry.getKey();
                    Object listenerObject = entry.getValue();
                    if (listenerObject instanceof Collection) {
                        Collection listeners = (Collection) listenerObject;
                        EventListeners listenerRegistry = config.getEventListeners();
                        Object[] listenerArray =
                                (Object[]) Array.newInstance(listenerRegistry.getListenerClassFor(listenerType), listeners.size());
                        listenerArray = listeners.toArray(listenerArray);
                        config.setListeners(listenerType, listenerArray);
                    }
                    else {
                        config.setListener(listenerType, listenerObject);
                    }
                }
            }

            // Perform custom post-processing in subclasses.
            postProcessConfiguration(config);

            //      Configuration    SessionFactory   
            logger.info("Building new Hibernate SessionFactory");
            this.configuration = config;
            sf = newSessionFactory(config);
        }
        //             
        finally {
            if (this.dataSource != null) {
                // Reset DataSource holder.
                configTimeDataSourceHolder.set(null);
            }

            if (this.jtaTransactionManager != null) {
                // Reset TransactionManager holder.
                configTimeTransactionManagerHolder.set(null);
            }

            if (this.lobHandler != null) {
                // Reset LobHandler holder.
                configTimeLobHandlerHolder.set(null);
            }
        }

        // Execute schema update if requested.
        if (this.schemaUpdate) {
            updateDatabaseSchema();
        }

        return sf;
    }

     org.hibernate.cfg.Configuration      SessionFactory: 
Java   
protected SessionFactory newSessionFactory(Configuration config) throws HibernateException {   
    return config.buildSessionFactory();   
}  

    protected SessionFactory newSessionFactory(Configuration config) throws HibernateException {
        return config.buildSessionFactory();
    }

        LocalSessionFactory                 SessionFactory   ;        SessionFactory  ,    session           -      Proxy   getCurrentSession       ; 
Java   
//        SessionFactory     Proxy,    Spring    getCurrentSession      
    protected SessionFactory getTransactionAwareSessionFactoryProxy(SessionFactory target) {   
        Class sfInterface = SessionFactory.class;   
        if (target instanceof SessionFactoryImplementor) {   
            sfInterface = SessionFactoryImplementor.class;   
        }   
        return (SessionFactory) Proxy.newProxyInstance(sfInterface.getClassLoader(),   
                new Class[] {sfInterface}, new TransactionAwareInvocationHandler(target));   
    }  

//        SessionFactory     Proxy,    Spring    getCurrentSession   
    protected SessionFactory getTransactionAwareSessionFactoryProxy(SessionFactory target) {
        Class sfInterface = SessionFactory.class;
        if (target instanceof SessionFactoryImplementor) {
            sfInterface = SessionFactoryImplementor.class;
        }
        return (SessionFactory) Proxy.newProxyInstance(sfInterface.getClassLoader(),
                new Class[] {sfInterface}, new TransactionAwareInvocationHandler(target));
    }
スクリーンショットの実現は以下の通りです.
 
private static class TransactionAwareInvocationHandler implements InvocationHandler {   
  
    private final SessionFactory target;   
  
    public TransactionAwareInvocationHandler(SessionFactory target) {   
        this.target = target;   
    }   
  
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {   
        // Invocation on SessionFactory/SessionFactoryImplementor interface coming in...   
        //    getCurrentSession      ,            session       
        if (method.getName().equals("getCurrentSession")) {   
            // Handle getCurrentSession method: return transactional Session, if any.   
            try {   
                return SessionFactoryUtils.doGetSession((SessionFactory) proxy, false);   
            }   
            catch (IllegalStateException ex) {   
                throw new HibernateException(ex.getMessage());   
            }   
        }   
        else if (method.getName().equals("equals")) {   
            // Only consider equal when proxies are identical.   
            return (proxy == args[0] ? Boolean.TRUE : Boolean.FALSE);   
        }   
        else if (method.getName().equals("hashCode")) {   
            // Use hashCode of SessionFactory proxy.   
            return new Integer(hashCode());   
        }   
  
        //         SessionFactory        
        try {   
            return method.invoke(this.target, args);   
        }   
        catch (InvocationTargetException ex) {   
            throw ex.getTargetException();   
        }   
    }   
}  

    private static class TransactionAwareInvocationHandler implements InvocationHandler {

        private final SessionFactory target;

        public TransactionAwareInvocationHandler(SessionFactory target) {
            this.target = target;
        }

        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            // Invocation on SessionFactory/SessionFactoryImplementor interface coming in...
            //    getCurrentSession      ,            session    
            if (method.getName().equals("getCurrentSession")) {
                // Handle getCurrentSession method: return transactional Session, if any.
                try {
                    return SessionFactoryUtils.doGetSession((SessionFactory) proxy, false);
                }
                catch (IllegalStateException ex) {
                    throw new HibernateException(ex.getMessage());
                }
            }
            else if (method.getName().equals("equals")) {
                // Only consider equal when proxies are identical.
                return (proxy == args[0] ? Boolean.TRUE : Boolean.FALSE);
            }
            else if (method.getName().equals("hashCode")) {
                // Use hashCode of SessionFactory proxy.
                return new Integer(hashCode());
            }

            //         SessionFactory     
            try {
                return method.invoke(this.target, args);
            }
            catch (InvocationTargetException ex) {
                throw ex.getTargetException();
            }
        }
    }
get CurrenntSessionの実現を見てみます.Session FactoryUtilsで:
private static Session doGetSession(   
            SessionFactory sessionFactory, Interceptor entityInterceptor,   
            SQLExceptionTranslator jdbcExceptionTranslator, boolean allowCreate)   
            throws HibernateException, IllegalStateException {   
  
        Assert.notNull(sessionFactory, "No SessionFactory specified");   
  
        //  TransactionSynchronizationManager Resource   ThreadLocal  ,sessionFactory     , ThreadLocal          
        //      Hiberante      ThreadLocal session       
        SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager.getResource(sessionFactory);   
        if (sessionHolder != null && !sessionHolder.isEmpty()) {   
            // pre-bound Hibernate Session   
            Session session = null;   
            if (TransactionSynchronizationManager.isSynchronizationActive() &&   
                    sessionHolder.doesNotHoldNonDefaultSession()) {   
                // Spring transaction management is active ->   
                // register pre-bound Session with it for transactional flushing.   
                session = sessionHolder.getValidatedSession();   
                if (session != null && !sessionHolder.isSynchronizedWithTransaction()) {   
                    logger.debug("Registering Spring transaction synchronization for existing Hibernate Session");   
                    TransactionSynchronizationManager.registerSynchronization(   
                            new SpringSessionSynchronization(sessionHolder, sessionFactory, jdbcExceptionTranslator, false));   
                    sessionHolder.setSynchronizedWithTransaction(true);   
                    // Switch to FlushMode.AUTO, as we have to assume a thread-bound Session   
                    // with FlushMode.NEVER, which needs to allow flushing within the transaction.   
                    FlushMode flushMode = session.getFlushMode();   
                    if (flushMode.lessThan(FlushMode.COMMIT) &&   
                            !TransactionSynchronizationManager.isCurrentTransactionReadOnly()) {   
                        session.setFlushMode(FlushMode.AUTO);   
                        sessionHolder.setPreviousFlushMode(flushMode);   
                    }   
                }   
            }   
            else {   
                // No Spring transaction management active -> try JTA transaction synchronization.   
                session = getJtaSynchronizedSession(sessionHolder, sessionFactory, jdbcExceptionTranslator);   
            }   
            if (session != null) {   
                return session;   
            }   
        }   
        //        Session   
        logger.debug("Opening Hibernate Session");   
        Session session = (entityInterceptor != null ?   
                sessionFactory.openSession(entityInterceptor) : sessionFactory.openSession());   
  
        // Use same Session for further Hibernate actions within the transaction.   
        // Thread object will get removed by synchronization at transaction completion.   
        //      Session  SessionHolder,    ThreadLocal          ,  ThreadLocal   TransactionSynchronizationManager     ,    sessionFactory      
        //               session   ,   FlushMode   Never,   session            
        if (TransactionSynchronizationManager.isSynchronizationActive()) {   
            // We're within a Spring-managed transaction, possibly from JtaTransactionManager.   
            logger.debug("Registering Spring transaction synchronization for new Hibernate Session");   
            SessionHolder holderToUse = sessionHolder;   
            if (holderToUse == null) {   
                holderToUse = new SessionHolder(session);   
            }   
            else {   
                holderToUse.addSession(session);   
            }   
            if (TransactionSynchronizationManager.isCurrentTransactionReadOnly()) {   
                session.setFlushMode(FlushMode.NEVER);   
            }   
            TransactionSynchronizationManager.registerSynchronization(   
                    new SpringSessionSynchronization(holderToUse, sessionFactory, jdbcExceptionTranslator, true));   
            holderToUse.setSynchronizedWithTransaction(true);   
            if (holderToUse != sessionHolder) {   
                TransactionSynchronizationManager.bindResource(sessionFactory, holderToUse);   
            }   
        }   
        else {   
            // No Spring transaction management active -> try JTA transaction synchronization.   
            registerJtaSynchronization(session, sessionFactory, jdbcExceptionTranslator, sessionHolder);   
        }   
  
        // Check whether we are allowed to return the Session.   
        if (!allowCreate && !isSessionTransactional(session, sessionFactory)) {   
            closeSession(session);   
            throw new IllegalStateException("No Hibernate Session bound to thread, " +   
                "and configuration does not allow creation of non-transactional one here");   
        }   
  
        return session;   
    }  

private static Session doGetSession(
            SessionFactory sessionFactory, Interceptor entityInterceptor,
            SQLExceptionTranslator jdbcExceptionTranslator, boolean allowCreate)
            throws HibernateException, IllegalStateException {

        Assert.notNull(sessionFactory, "No SessionFactory specified");

        //  TransactionSynchronizationManager Resource   ThreadLocal  ,sessionFactory     , ThreadLocal       
        //      Hiberante      ThreadLocal session    
        SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager.getResource(sessionFactory);
        if (sessionHolder != null && !sessionHolder.isEmpty()) {
            // pre-bound Hibernate Session
            Session session = null;
            if (TransactionSynchronizationManager.isSynchronizationActive() &&
                    sessionHolder.doesNotHoldNonDefaultSession()) {
                // Spring transaction management is active ->
                // register pre-bound Session with it for transactional flushing.
                session = sessionHolder.getValidatedSession();
                if (session != null && !sessionHolder.isSynchronizedWithTransaction()) {
                    logger.debug("Registering Spring transaction synchronization for existing Hibernate Session");
                    TransactionSynchronizationManager.registerSynchronization(
                            new SpringSessionSynchronization(sessionHolder, sessionFactory, jdbcExceptionTranslator, false));
                    sessionHolder.setSynchronizedWithTransaction(true);
                    // Switch to FlushMode.AUTO, as we have to assume a thread-bound Session
                    // with FlushMode.NEVER, which needs to allow flushing within the transaction.
                    FlushMode flushMode = session.getFlushMode();
                    if (flushMode.lessThan(FlushMode.COMMIT) &&
                            !TransactionSynchronizationManager.isCurrentTransactionReadOnly()) {
                        session.setFlushMode(FlushMode.AUTO);
                        sessionHolder.setPreviousFlushMode(flushMode);
                    }
                }
            }
            else {
                // No Spring transaction management active -> try JTA transaction synchronization.
                session = getJtaSynchronizedSession(sessionHolder, sessionFactory, jdbcExceptionTranslator);
            }
            if (session != null) {
                return session;
            }
        }
        //        Session
        logger.debug("Opening Hibernate Session");
        Session session = (entityInterceptor != null ?
                sessionFactory.openSession(entityInterceptor) : sessionFactory.openSession());

        // Use same Session for further Hibernate actions within the transaction.
        // Thread object will get removed by synchronization at transaction completion.
        //      Session  SessionHolder,    ThreadLocal          ,  ThreadLocal   TransactionSynchronizationManager     ,    sessionFactory   
        //               session   ,   FlushMode   Never,   session         
        if (TransactionSynchronizationManager.isSynchronizationActive()) {
            // We're within a Spring-managed transaction, possibly from JtaTransactionManager.
            logger.debug("Registering Spring transaction synchronization for new Hibernate Session");
            SessionHolder holderToUse = sessionHolder;
            if (holderToUse == null) {
                holderToUse = new SessionHolder(session);
            }
            else {
                holderToUse.addSession(session);
            }
            if (TransactionSynchronizationManager.isCurrentTransactionReadOnly()) {
                session.setFlushMode(FlushMode.NEVER);
            }
            TransactionSynchronizationManager.registerSynchronization(
                    new SpringSessionSynchronization(holderToUse, sessionFactory, jdbcExceptionTranslator, true));
            holderToUse.setSynchronizedWithTransaction(true);
            if (holderToUse != sessionHolder) {
                TransactionSynchronizationManager.bindResource(sessionFactory, holderToUse);
            }
        }
        else {
            // No Spring transaction management active -> try JTA transaction synchronization.
            registerJtaSynchronization(session, sessionFactory, jdbcExceptionTranslator, sessionHolder);
        }

        // Check whether we are allowed to return the Session.
        if (!allowCreate && !isSessionTransactional(session, sessionFactory)) {
            closeSession(session);
            throw new IllegalStateException("No Hibernate Session bound to thread, " +
                "and configuration does not allow creation of non-transactional one here");
        }

        return session;
    }
ここではSpringでHiberanteのSession Factory及びSessionを使用するための準備作業であり、これに加えて、ユーザーはHibernate Templateを使用することによって、HbernateのO/R機能を使用することができ、以前に見たのと同じexecuteのフィードバックである.
public Object execute(HibernateCallback action, boolean exposeNativeSession) throws DataAccessException {   
    Assert.notNull(action, "Callback object must not be null");   
    //        Hibernate Session   
    Session session = getSession();   
    boolean existingTransaction = SessionFactoryUtils.isSessionTransactional(session, getSessionFactory());   
    if (existingTransaction) {   
        logger.debug("Found thread-bound Session for HibernateTemplate");   
    }   
  
    FlushMode previousFlushMode = null;   
    try {   
        previousFlushMode = applyFlushMode(session, existingTransaction);   
        enableFilters(session);   
        Session sessionToExpose = (exposeNativeSession ? session : createSessionProxy(session));   
        //           
        Object result = action.doInHibernate(sessionToExpose);   
        flushIfNecessary(session, existingTransaction);   
        return result;   
    }   
    catch (HibernateException ex) {   
        throw convertHibernateAccessException(ex);   
    }   
    catch (SQLException ex) {   
        throw convertJdbcAccessException(ex);   
    }   
    catch (RuntimeException ex) {   
        // Callback code threw application exception...   
        throw ex;   
    }   
    finally {   
        //                ,   
        if (existingTransaction) {   
            logger.debug("Not closing pre-bound Hibernate Session after HibernateTemplate");   
            disableFilters(session);   
            if (previousFlushMode != null) {   
                session.setFlushMode(previousFlushMode);   
            }   
        } //   Session     
        else {   
            // Never use deferred close for an explicitly new Session.   
            if (isAlwaysUseNewSession()) {   
                SessionFactoryUtils.closeSession(session);   
            }   
            else {   
                SessionFactoryUtils.closeSessionOrRegisterDeferredClose(session, getSessionFactory());   
            }   
        }   
    }   
}  

    public Object execute(HibernateCallback action, boolean exposeNativeSession) throws DataAccessException {
        Assert.notNull(action, "Callback object must not be null");
        //        Hibernate Session
        Session session = getSession();
        boolean existingTransaction = SessionFactoryUtils.isSessionTransactional(session, getSessionFactory());
        if (existingTransaction) {
            logger.debug("Found thread-bound Session for HibernateTemplate");
        }

        FlushMode previousFlushMode = null;
        try {
            previousFlushMode = applyFlushMode(session, existingTransaction);
            enableFilters(session);
            Session sessionToExpose = (exposeNativeSession ? session : createSessionProxy(session));
            //        
            Object result = action.doInHibernate(sessionToExpose);
            flushIfNecessary(session, existingTransaction);
            return result;
        }
        catch (HibernateException ex) {
            throw convertHibernateAccessException(ex);
        }
        catch (SQLException ex) {
            throw convertJdbcAccessException(ex);
        }
        catch (RuntimeException ex) {
            // Callback code threw application exception...
            throw ex;
        }
        finally {
            //                ,
            if (existingTransaction) {
                logger.debug("Not closing pre-bound Hibernate Session after HibernateTemplate");
                disableFilters(session);
                if (previousFlushMode != null) {
                    session.setFlushMode(previousFlushMode);
                }
            } //   Session  
            else {
                // Never use deferred close for an explicitly new Session.
                if (isAlwaysUseNewSession()) {
                    SessionFactoryUtils.closeSession(session);
                }
                else {
                    SessionFactoryUtils.closeSessionOrRegisterDeferredClose(session, getSessionFactory());
                }
            }
        }
    }
どのように対応したSessionを得るかを見てみます.まだSession FactoryUtilsの方法doGet Sessionを使っています.
protected Session getSession() {   
    if (isAlwaysUseNewSession()) {   
        return SessionFactoryUtils.getNewSession(getSessionFactory(), getEntityInterceptor());   
    }   
    else if (!isAllowCreate()) {   
        return SessionFactoryUtils.getSession(getSessionFactory(), false);   
    }   
    else {   
        return SessionFactoryUtils.getSession(   
                getSessionFactory(), getEntityInterceptor(), getJdbcExceptionTranslator());   
    }   
}  

    protected Session getSession() {
        if (isAlwaysUseNewSession()) {
            return SessionFactoryUtils.getNewSession(getSessionFactory(), getEntityInterceptor());
        }
        else if (!isAllowCreate()) {
            return SessionFactoryUtils.getSession(getSessionFactory(), false);
        }
        else {
            return SessionFactoryUtils.getSession(
                    getSessionFactory(), getEntityInterceptor(), getJdbcExceptionTranslator());
        }
    }
このように私達は他のTemplateと同じようにHibernateの基本機能を使うことができます.使う時Springはすでに私達のSessionに対する獲得と閉鎖になりました.