您的位置:首页 > 编程语言 > Java开发

spring 对于hibernate操作的封装 HibernateCallback接口的学习

2016-03-01 16:39 435 查看
我实在查看HibernateTemplate时候看到这个接口的,发现很特殊,也使用很多,然后抱着学习的态度去了解一下。

以下是对于该接口的定义,可以看出只有一个doInHibernate函数,

public interface HibernateCallback<T> {

/**

* Gets called by {@code HibernateTemplate.execute} with an active

* Hibernate {@code Session}. Does not need to care about activating

* or closing the {@code Session}, or handling transactions.

*

* <p>Allows for returning a result object created within the callback,

* i.e. a domain object or a collection of domain objects.

* A thrown custom RuntimeException is treated as an application exception:

* It gets propagated to the caller of the template.

*

* @param session active Hibernate session

* @return a result object, or {@code null} if none

* @throws HibernateException if thrown by the Hibernate API

* @see HibernateTemplate#execute

*/

T doInHibernate(Session session) throws HibernateException;

}

在HibernateTemplate主要的函数就是这个doExecute,相当于对action的代理,管理打开的session 。

/**

* Execute the action specified by the given action object within a Session.

* @param action callback object that specifies the Hibernate action

* @param enforceNativeSession whether to enforce exposure of the native

* Hibernate Session to callback code

* @return a result object returned by the action, or {@code null}

* @throws DataAccessException in case of Hibernate errors

*/

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的接口doInHibernate中实现的,通过spring管理的session来完成具体的数据库操作。使用如下。

public List<Student> getStudent(final String name){

List<Student> students = null;

students = hibernateTemplate.execute(new HibernateCallback<List<Student>>() {

@Override

public List<Student> doInHibernate(Session session) throws HibernateException {

return session.createQuery("from Student where name like '"+name+"'").list();

}

});

return students;

}

最后是关于这个回调函数的问题,编程者就不用每次创建seesion,然后去关闭session 了,通过doInHibernate中的session中的都是受spring管理的。每次执行完都会去关闭session,不用主动去调用。

基本原理跟好莱坞原则一样,Don't call me,I'll call you.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: