1.Hibernate 的session关闭问题
由于成员变量是懒加载方式配置,所以当通过JSON转换时,json lib 调用对象的getXX方法是,hibernate会临时再去加载关联对象,但是此时的session早已经关闭,所以会抛出异常:could not initialize proxy - the owning Session was closed
解决方法:将session保留在某个请求线程内,知道request请求结束返回。spring提供了这样一个filter
<!-- Hibernate Open Session In View filter -->
<filter>
<filter-name>hibernateOpenSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<!-- 放在struts filter之前 -->
<filter-mapping>
<filter-name>hibernateOpenSessionInViewFilter</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
2.动态代理类属性过滤问题
由于延迟加载获得的对象,并不是我们自己写的类型,而是由cglib字节码生成器生成的动态代理类型,在这个子类中又添加了新的属性,所以当通过json lib转换时,会抛出异常:java.sql.SQLException: Positioned Update not supported
解决方法:将cglib中添加的属性过滤掉,
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.setExcludes(new String[]{"handler","hibernateLazyInitializer"});
JSON json = JSONSerializer.toJSON(bean, jsonConfig);
3.关联对象的过滤问题
由于hibernate提供实体将的关联关系的配置,所以对象之间增加了耦合关系,如果通过json转换生成字符串,势必导致关联对象之间的调用,造成信息的泄露,所以必须对关联对象字段或关键信息进行过滤,通过jsonConfig.setExcludes(new String[]{”"}); 可以解决这个问题。但是不同类型的对象都要过滤指定自己的字段,势必很麻烦。如果你的hibernate的映射类时通过JPA配置的,我们可以通过判断注解类型,过滤成员变量。自己写了一个过滤方法:
import java.lang.reflect.Field;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import net.sf.json.JSON;
import net.sf.json.JSONSerializer;
import net.sf.json.JsonConfig;
/**
* @author Joshua
*
*/
public class CheJsonUtil {
public static JSON getJSON(Object bean) {
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.setExcludes(getExcludeFields(bean));
return JSONSerializer.toJSON(bean, jsonConfig);
}
/**
* 排除bean之间的关系字段
* @param bean
* @return
*/
public static String[] getExcludeFields(Object bean) {
Set<String> list = new HashSet<String>();
list.add("handler");
list.add("hibernateLazyInitializer");
for (Class<?> superClass = bean.getClass(); superClass != Object.class; superClass = superClass
.getSuperclass()) {
Field[] fields = superClass.getDeclaredFields();
for (Field field : fields) {
if (field.getAnnotation(OneToOne.class) != null
|| field.getAnnotation(OneToMany.class) != null
|| field.getAnnotation(ManyToOne.class) != null
|| field.getAnnotation(ManyToMany.class) != null) {
list.add(field.getName());
}
}
}
return list.toArray(new String[list.size()]);
}
}