---
title: "hibernate 3.2中的session接口的不同"
author: "dreamstone"
date: "2007-07-29T00:55:31Z"
tags: []
slug: "hibernate-3-2-session"
draft: false
lastmod: "2007-07-29T00:55:31Z"
id: 106495
url: "https://dreamstone.iteye.com/blog/106495"
---

hibernate 3中的session接口的不同  
hibernate3.2版本中session出现了2个  
新session接口：org.hibernate.Session  
老session接口：org.hibernate.classic.Session  
顾名思义，classic包下的session就是以前常用的session，新的这个相比老的有很大变化。下边详细列出  
1，去掉了所有的find方法  
在新的session接口中没有find方法，而在老的session接口中把find全部注释成deprecated了。  
2,去掉所有的saveOrUpdateCopy，使用merge代替，这是classic.Session注释中的一段原话.  
/\**  
  * Copy the state of the given object onto the persistent object with the same  
  * identifier. If there is no persistent instance currently associated with  
  * the session, it will be loaded. Return the persistent instance. If the  
  * given instance is unsaved or does not exist in the database, save it and  
  * return it as a newly persistent instance. Otherwise, the given instance  
  * does not become associated with the session.  
  *  
  * @deprecated use [{@link](mailto:{@link) org.hibernate.Session#merge(String, Object)}  
  *  
  * @param object a transient instance with state to be copied  
  * @return an updated persistent instance  
  \*/  
注意这句：@deprecated use [{@link](mailto:{@link) org.hibernate.Session#merge(String, Object)}  
3,去掉了iterate方法  
给出的注释是使用createQuery，自己获得iterate  
4，去掉了filter方法  
@deprecated use [{@link](mailto:{@link) #createFilter(Object, String)}.{@link Query#list}  
给出的注释说用createFilter代替，实际就是自己从createFilter获得query然后自己查询  
5，增加了一些方法  
具体自己看api吧，主要是提供了一些新的功能。  
总结：  
从上边的改变不难看出hibernate对于接口的设定观念改变了。

以前的策略是：  
尽量给出全的接口，这样减少用户的代码量，所以filter直接返回collection，iterate直接返回  
iterate。但这样的结果是过度的提供接口，造成了学习上的负担和选择上的负担。如何记住这些函数，如何在众多函数  
中选择是个麻烦事情。  
凡是做java的都知道，用一个java的东西最辛苦的是选择，在开源的世界里边选择一个适合自己的工程，再在这个选择的工程里边选择实现方法  
因为可能提供很多种实现方法，而且有些还是deprecated的。

现在的策略：  
尽量简化接口，或减少函数，或者简化函数名，例如把saveOrUpdateCopy变成merge。  
这样的好处是记忆学习负担少。多写几句代码不是特别麻烦。其实我个人来讲更喜欢现在的感觉。  
以前的策略其实很大程度上是满足程序员的个人需求，更有成就感。但确不适合使用者的需求。

ok，无论如何现在的情况是更好了。