spring框架中getSession与getHibernateTemplate的区别
?
一直都很疑惑在spring框架中getSession与getHibernateTemplate的区别。相同的地方不用说大家都是调用数据库返回结果的。但为什么要这样有二个不同的兄弟呢。认真学习了一下(其实还是很粗略的)
从使用上看
getHibernateTemplate必须继承或实例HibernateDaoSupport当然包括sessionFactory
Java代码
@SuppressWarnings("unchecked")??
??? public T get(String id) {??
??????? T ret = (T) getHibernateTemplate().get(type, id);??
??????? if (ret == null) {??
??????????? throw new ObjectRetrievalFailureException(type,??
??????????????????? "can't get object with id " + id);??
??????? }??
??????? return ret;??
??? }??
public IResponseDataNPage findPageByCriteria(??
??????????? final DetachedCriteria detachedCriteria, final Order order,??
??????????? final int pageSize, final int startIndex)? {??
??????? return (IResponseDataNPage) getHibernateTemplate().execute(??
??????????????? new HibernateCallback() {??
??????????????????? public Object doInHibernate(Session session)??
??????????????????????????? throws HibernateException {??
??????????????????????? Criteria criteria = detachedCriteria??
??????????????????????????????? .getExecutableCriteria(session);??
??????????????????????? int totalCount = ((Integer) criteria.setProjection(??
??????????????????????????????? Projections.rowCount()).uniqueResult())??
??????????????????????????????? .intValue();??
??????????????????????? criteria.setProjection(null);??
??????????????????????? if (order != null)??
??????????????????????????? criteria.addOrder(order);??
??????????????????????? if(pageSize>0 && startIndex>=0)??
??????????????????????? {??
??????????????????????????? criteria.setFirstResult(startIndex).setMaxResults(??
??????????????????????????????????? pageSize);??
??????????????????????? }??
??????????????????????????
??????????????????????? List items = getHibernateTemplate().findByCriteria(??
??????????????????????????????? detachedCriteria);??
??????????????????????? // IResponseDataNPage ps = new IResponseDataNPage(items,??
??????????????????????? // totalCount, pageSize, startIndex);??
??????????????????????? IResponseDataNPage dataNPage = buildResponseDataNPage(??
??????????????????????????????? null, items, totalCount);??
??????????????????????? return dataNPage;??
??????????????????? }??
??????????????? }, true);??
??? }??
??????
?
}?
@SuppressWarnings("unchecked")
public T get(String id) {
T ret = (T) getHibernateTemplate().get(type, id);
if (ret == null) {
throw new ObjectRetrievalFailureException(type,
"can't get object with id " + id);
}
return ret;
}
public IResponseDataNPage findPageByCriteria(
final DetachedCriteria detachedCriteria, final Order order,
final int pageSize, final int startIndex)? {
return (IResponseDataNPage) getHibernateTemplate().execute(
new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException {
Criteria criteria = detachedCriteria
.getExecutableCriteria(session);
int totalCount = ((Integer) criteria.setProjection(
Projections.rowCount()).uniqueResult())
.intValue();
criteria.setProjection(null);
if (order != null)
criteria.addOrder(order);
if(pageSize>0 && startIndex>=0)
{
criteria.setFirstResult(startIndex).setMaxResults(
pageSize);
}
List items = getHibernateTemplate().findByCriteria(
detachedCriteria);
// IResponseDataNPage ps = new IResponseDataNPage(items,
// totalCount, pageSize, startIndex);
IResponseDataNPage dataNPage = buildResponseDataNPage(
null, items, totalCount);
return dataNPage;
}
}, true);
}
}
getSession只有sessionFactory
Java代码
public T get(final PK id) {??
??????? return (T) getSession().load(entityClass, id);??
??? }??
??? public Page<T> find(Page<T> page, String hql, Object... values) {??
??????? Assert.notNull(page);??
??????? Query q = createQuery(hql, values);??
??????? if (page.isFirstSetted()) {??
??????????? q.setFirstResult(page.getFirst());??
??????? }??
??????? if (page.isPageSizeSetted()) {??
??????????? q.setMaxResults(page.getPageSize());??
??????? }??
??????? page.setResult(q.list());??
??????? return page;??
??? }?
public T get(final PK id) {
return (T) getSession().load(entityClass, id);
}
public Page<T> find(Page<T> page, String hql, Object... values) {
Assert.notNull(page);
Query q = createQuery(hql, values);
if (page.isFirstSetted()) {
q.setFirstResult(page.getFirst());
}
if (page.isPageSizeSetted()) {
q.setMaxResults(page.getPageSize());
}
page.setResult(q.list());
return page;
} 从上面可以看出getSession能实现与getHibernateTemplate完全一样的代码。
但getSession要自己实现一堆的功能。如createQuery等等。
但深入比较发现一下不同处:
getSession() 获得的是原始的sessionFactory,每次你必须自己维护session如结束后你必须关闭session。如果是hibernate中进行数据库操作,你获得是原始的hibernate style和excepttion。
而hibernate template是spring包装过的,它会帮你管理session,并且它会将hibernate exceptions转换成其他的分类后的错误。这点getSession是肯定不行了。例如你用orcle和mysql返回的错误在getSession中就是不一样的,而在hibernate template中就是一样的。
但我在实际使用中发现,对于基本的操作Hibernate template处理的的确比getSession要好,但到了复杂查询的时候如分页时需要调用getHibernateTemplate().execute(HibernateCallBack).要产生很多innerClass,调试非常不便。而getSession就相当简单多了。