当前位置: 代码迷 >> J2EE >> sssh+ajax中@JSON的有关问题
  详细解决方案

sssh+ajax中@JSON的有关问题

热度:43   发布时间:2016-04-19 22:49:19.0
sssh+ajax中@JSON的问题?
我现在在做一个用extjs做前端的项目,全部请求都是用ajax访问 ,返回JSON数据。
本来这之前做过一些dom觉得没问题,可是越往后有些问题越搞不定。
前提:首先我知道在get方法上加上@JSON(serialize=false)的对象不会被转换成JSON对象。
现在列举我不会的问题,希望大家能够帮忙解决,因我本人确实是菜鸟,找的这家公司就我一个搞java的
问题一:
有二个类分别是Supplier(供应商),Type(类型)
public class Supplier {
private String supplier_id;
private String supplier_name;
private String supplier_username;
private String supplier_pwd;
private String supplier_person;
private String supplier_phone;
private String supplier_email;
private String supplier_desc;
private String supplier_address;
private Boolean supplier_isInner;
private Set<Type> supplierTypes; //供应商拥有的类型
private Set<Role> roles;//供应商拥有的角色
}


public class Type {
private String id;
private String text;
private Boolean leaf;
private Boolean checked;
private Set<Type> children;//类型拥有的子类型
private String pid;
private Set<Supplier> suppliers;//类型拥有的供应商
}

比如现在我要查询出一个供应商,比较简单的是把供应商下面两个属性的get方法加上@JSON(serialize=false)

@JSON(serialize=false)
public Set<Type> getSupplierTypes() {
return supplierTypes;
}
@JSON(serialize=false)
public Set<Role> getRoles() {
return roles;
}

查出结果是只有基本属性的对象:
{"model":{"supplier_address":"测试","supplier_desc":"测试","supplier_email":"abc@qq.com","supplier_id":"4028818344d488880144d4912ffd0001","supplier_isInner":null,"supplier_name":"测试","supplier_person":"测试","supplier_phone":"测试","supplier_pwd":"123456","supplier_username":"testtest"},"msg":false,"success":false}
现在我又想查询出供应商的supplierTypes
第一步去掉下面方法的@JSON

public Set<Type> getSupplierTypes() {
return supplierTypes;
}

然后我在查询的Dao中这么写

public Supplier searchSupplierById(String id) {
Supplier supplier = (Supplier) hibernateTemplate.get(Supplier.class, id);
supplier.getSupplierTypes();
return supplier;
}

结果:
1.failed to lazily initialize a collection of role: cn.com.su.domain.Supplier.supplierTypes, no session or session was closed
2.org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: cn.com.su.domain.Supplier.supplierTypes, no session or session was closed
3.org.apache.struts2.json.JSONException: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: cn.com.su.domain.Supplier.supplierTypes, no session or session was closed

org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: cn.com.su.domain.Supplier.supplierTypes, no session or session was closed

我想这下怎么办呢,好像是ajax请求时不会开启事务。难道要这样改掉映射文件,加上lazy="true"

<set name="supplierTypes" table="supplier_type" cascade="all" lazy="true">
<key column="supplier_id"></key>
<many-to-many class="cn.com.su.domain.Type" column="id"></many-to-many>
</set>

我不想这么干,觉得这样就写死了。
我又想到了通过HQL语句,解决问题

public Supplier searchSupplierNoLazyById(String id) {
List<Supplier> suppliers =  hibernateTemplate.find("from Supplier s left outer join fetch  s.supplierTypes st where s.supplier_id = ?",id);
if(suppliers.size()>0){
return suppliers.get(0);
}
return null;
}
[/java]
这时又报了一个懒加载的异常:
failed to lazily initialize a collection of role: cn.com.su.domain.Type.children, no session or session was closed
异常是因为上面的HQL语句虽然查出了Type,但把type转换成JSON对象时,type下的两个对象属性并未查询出来
private Set<Type> children;//类型拥有的子类型
private Set<Supplier> suppliers;//类型拥有的供应商
我又在这两个属性上加上了@JSON
[code=java]
@JSON(serialize=false)
public Set<Type> getChildren() {
return children;
}
@JSON(serialize=false)
public Set<Supplier> getSuppliers() {
return suppliers;
}

到这时我想要的结果终于成功出来了:
{"model":{"supplierTypes":[{"checked":false,"id":"5.1.1","leaf":true,"pid":"5.1","text":" 烹饪锅具"}],"supplier_address":"测试","supplier_desc":"测试","supplier_email":"abc@qq.com","supplier_id":"4028818344d488880144d4912ffd0001","supplier_isInner":null,"supplier_name":"测试","supplier_person":"测试","supplier_phone":"测试","supplier_pwd":"123456","supplier_username":"testtest"},"msg":false,"success":false}
但是为了查询出这个结果我把Type下的
private Set<Type> children;//类型拥有的子类型
private Set<Supplier> suppliers;//类型拥有的供应商
加上@JSON了 。
当我在其它请求里想要把数据children和suppliers也查出来时,就没有办法了。
我的问题描述完了,求解决半法:
最后描述一下我的问题,就是每次请求的结果都是有区别的,但是@JSON已经写死了,该怎么办。
本人QQ:597477731求指导 
  相关解决方案