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.
以下是对于该接口的定义,可以看出只有一个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.
相关文章推荐
- java 线程操作
- java中的final变量
- 笔试题之java基础
- java实现字符串指定的部分进行反转
- Java中的Collection和Map(四)--Set体系
- Java 依赖注入标准 JSR-330 简介
- 关于java中泛型问题
- java动态代理与AOP
- java, 字符串String,方法
- java下的IO操作
- Java 9终于要包含Jigsaw项目了
- 性能调优之Java系统级性能监控及优化
- Java中获取完整的url
- [置顶] eclipse 设置默认编码为Utf-8 详细教程。
- 经典搜索(深搜+剪枝+Java细节注意)
- java解决方案,技术型的
- SPRING IN ACTION 第4版笔记-第二章-004-Bean是否单例
- Java锁机制
- myeclipse
- myeclipse