Spring hibernatecallback学习

在spring hibernate整合中我们通过在dao中对HibernateDaoSupport继承,使用hibernateTemplate来进行crud操作,但是HibernateTemplate中是怎么实现的crud操作呢,通过点开HibernateTemplate类我们可以发现一个神奇的东西就是HibernateCallback

this.getHibernateTemplate().execute(new HibernateCallback<Object>() {

            @Override
            public Object doInHibernate(Session session) throws HibernateException {
                // TODO Auto-generated method stub
                return null;
            }
        });

为什么会这样使用,我们可以在Myeclipse中点开HibernateDaoSupport类然后再点开HibernateTemplate类可以看到其中的save方法:

@Override
    public Serializable save(final Object entity) throws DataAccessException {
        return executeWithNativeSession(new HibernateCallback<Serializable>() {
            @Override
            public Serializable doInHibernate(Session session) throws HibernateException {
                checkWriteOperationAllowed(session);
                return session.save(entity);
            }
        });
    }

可以发现是通过调用executeWithNativeSession方法,然后我们再看executeWithNativeSession是如何声明的

public <T> T executeWithNativeSession(HibernateCallback<T> action) {
        return doExecute(action, true);
    }

可以看到其中一个参数是HibernateCallback action,然后方法体中是return doExecute(action, true);
可见最后调用的方法是在doExecute中

protected <T> T doExecute(HibernateCallback<T> action, boolean enforceNativeSession) throws DataAccessException {
        Assert.notNull(action, "Callback object must not be null");

        Session session = null;
        boolean isNew = false;
        try {
            session = getSessionFactory().getCurrentSession();
        }
        catch (HibernateException ex) {
            logger.debug("Could not retrieve pre-bound Hibernate session", ex);
        }
        if (session == null) {
            session = getSessionFactory().openSession();
            session.setFlushMode(FlushMode.MANUAL);
            isNew = true;
        }

        try {
            enableFilters(session);
            Session sessionToExpose =
                    (enforceNativeSession || isExposeNativeSession() ? session : createSessionProxy(session));
            return action.doInHibernate(sessionToExpose);
        }
        catch (HibernateException ex) {
            throw SessionFactoryUtils.convertHibernateAccessException(ex);
        }
        catch (RuntimeException ex) {
            // Callback code threw application exception...
            throw ex;
        }
        finally {
            if (isNew) {
                SessionFactoryUtils.closeSession(session);
            }
            else {
                disableFilters(session);
            }
        }
    }

最后通过通过这一条语句执行hibernateCallback中封装的方法

return action.doInHibernate(sessionToExpose);

可以发现最终都是通过doExecute方法进行的crud,那么现在可以分析下我们使用hibernateTemplate.save(Object o)方法时候的过程,
1.我们传入一个类o,然后在HibernateTemplate中调用save方法中
2.在hibernateCallback回调接口中将其封装成session.save(o)的动作
3.最终将这个动作传到doExecute方法中,而真正的crud操作正式在doExecute方法中。在doExecute首先通过SessionFactory获取session,向action中传入session最终执行session.save(0)的操作.

为什么要这样进行操作,因为在上述分析中我们发现真正获取session的过程是在doExecute方法中,而在HibernateTemplate中的save方法中我们根本不可能获取session,所以我们通过回调接口,在save方法中进行接口的声明,然后在doExecute中进行接口的调用。

通过了上面的分析,我明白到了,在HibernateTemplate中的各种get load save update方法只是通过hibernateCallback接口进行动作的封装,最终都是在doExecute方法中,获取session然后执行动作。所以我们也可以直接在hibernateCallback中执行我们想要的操作。

发布了51 篇原创文章 · 获赞 7 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/qq_28042463/article/details/51338275
今日推荐