struts2 json 插件 (与hibernate 关联对象)碰到的问题和处理
2016-06-16 13:57
543 查看
1.先上代码
BaseJsonAction.java
2.浏览器中输入: http://localhost:8080/caroa/user/findAll.action
发现会提示异常:could not initialize proxy - no Session
然后打开debug模式
进去看了一下 出现了:com.sun.jdi.InvocationException occurred invoking method.这个异常
这边应该是lazy加载所产生的问题,在 hibernate.cfg.xml 中加入
再debug 运行一下。 看见里边之前有异常的都有正常值了
3.F8 跳过 debug 发现又出异常了 :org.apache.struts2.json.JSONException: java.lang.reflect.InvocationTargetException
控制台一看 所有有主外键关联的类 都去查了,好恐怖的输出。。。 (以下是 n分之一)
这边我是这么处理的:(这里不需要关联表的属性,只要主表属性)找到User类,把关联表里边getX()的 关系里边加上@JSON(serialize=false)
继续运行:
看控制台,恩,只有1条语句输出。ok的。
继续看页面
页面也输出了我要的json:
贴图了:虽然很丑
搞定,收工~
BaseJsonAction.java
package com.jiang.action; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.struts2.convention.annotation.Action; import org.apache.struts2.convention.annotation.Namespace; import org.apache.struts2.convention.annotation.ParentPackage; import org.apache.struts2.convention.annotation.Result; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Controller; import com.jiang.entities.User; import com.jiang.service.IUserService; import com.opensymphony.xwork2.ActionSupport; @SuppressWarnings("serial") @Controller @Scope("session") @ParentPackage("json-default") @Namespace("/user") public class BaseJsonAction extends ActionSupport{ private Map<String,Object> dataMap; public Map<String, Object> getDataMap() { return dataMap; } public void setDataMap(Map<String, Object> dataMap) { this.dataMap = dataMap; } @Autowired private IUserService<User> userService; @Action(value = "findAll",results = { @Result(name="success",type = "json")}) public String findAll() throws Exception { dataMap = new HashMap<String,Object>(); List<User> userList = userService.findAll(User.class); dataMap.put("success", true); dataMap.put("userList", userList); return SUCCESS; } }
2.浏览器中输入: http://localhost:8080/caroa/user/findAll.action
发现会提示异常:could not initialize proxy - no Session
然后打开debug模式
进去看了一下 出现了:com.sun.jdi.InvocationException occurred invoking method.这个异常
这边应该是lazy加载所产生的问题,在 hibernate.cfg.xml 中加入
<!-- 用于解决懒加载时找不到session的问题 --> <!-- 关闭Load方法的延迟加载 --> <property name="hibernate.enable_lazy_load_no_trans">true</property>
再debug 运行一下。 看见里边之前有异常的都有正常值了
3.F8 跳过 debug 发现又出异常了 :org.apache.struts2.json.JSONException: java.lang.reflect.InvocationTargetException
控制台一看 所有有主外键关联的类 都去查了,好恐怖的输出。。。 (以下是 n分之一)
Hibernate: select cars0_.userid as userid13_2_0_, cars0_.id as id1_2_0_, cars0_.id as id1_2_1_, cars0_.back as back2_2_1_, cars0_.carName as carName3_2_1_, cars0_.dashboard as dashboar4_2_1_, cars0_.drivingLicense as drivingL5_2_1_, cars0_.front as front6_2_1_, cars0_.gpsMachine as gpsMachi7_2_1_, cars0_.incar as incar8_2_1_, cars0_.key as key9_2_1_, cars0_.left as left10_2_1_, cars0_.right as right11_2_1_, cars0_.type as type12_2_1_, cars0_.userid as userid13_2_1_ from caroa.car cars0_ where cars0_.userid=? Hibernate: select idcard0_.id as id1_5_0_, idcard0_.back as back2_5_0_, idcard0_.front as front3_5_0_, idcard0_.number as number4_5_0_ from caroa.idcard idcard0_ where idcard0_.id=? Hibernate: select users0_.idcardid as idcardid8_8_0_, users0_.id as id1_8_0_, users0_.id as id1_8_1_, users0_.address as address2_8_1_, users0_.age as age3_8_1_, users0_.idcardid as idcardid8_8_1_, users0_.mark as mark4_8_1_, users0_.name as name5_8_1_, users0_.password as password6_8_1_, users0_.picture as picture7_8_1_, users0_.typeid as typeid9_8_1_ from caroa.user users0_ where users0_.idcardid=? Hibernate: select cars0_.userid as userid13_2_0_, cars0_.id as id1_2_0_, cars0_.id as id1_2_1_, cars0_.back as back2_2_1_, cars0_.carName as carName3_2_1_, cars0_.dashboard as dashboar4_2_1_, cars0_.drivingLicense as drivingL5_2_1_, cars0_.front as front6_2_1_, cars0_.gpsMachine as gpsMachi7_2_1_, cars0_.incar as incar8_2_1_, cars0_.key as key9_2_1_, cars0_.left as left10_2_1_, cars0_.right as right11_2_1_, cars0_.type as type12_2_1_, cars0_.userid as userid13_2_1_ from caroa.car cars0_ where cars0_.userid=? Hibernate: select idcard0_.id as id1_5_0_, idcard0_.back as back2_5_0_, idcard0_.front as front3_5_0_, idcard0_.number as number4_5_0_ from caroa.idcard idcard0_ where idcard0_.id=? Hibernate: select users0_.idcardid as idcardid8_8_0_, users0_.id as id1_8_0_, users0_.id as id1_8_1_, users0_.address as address2_8_1_, users0_.age as age3_8_1_, users0_.idcardid as idcardid8_8_1_, users0_.mark as mark4_8_1_, users0_.name as name5_8_1_, users0_.password as password6_8_1_, users0_.picture as picture7_8_1_, users0_.typeid as typeid9_8_1_ from caroa.user users0_ where users0_.idcardid=? Hibernate: select cars0_.userid as userid13_2_0_, cars0_.id as id1_2_0_, cars0_.id as id1_2_1_, cars0_.back as back2_2_1_, cars0_.carName as carName3_2_1_, cars0_.dashboard as dashboar4_2_1_, cars0_.drivingLicense as drivingL5_2_1_, cars0_.front as front6_2_1_, cars0_.gpsMachine as gpsMachi7_2_1_, cars0_.incar as incar8_2_1_, cars0_.key as key9_2_1_, cars0_.left as left10_2_1_, cars0_.right as right11_2_1_, cars0_.type as type12_2_1_, cars0_.userid as userid13_2_1_ from caroa.car cars0_ where cars0_.userid=? Hibernate: select idcard0_.id as id1_5_0_, idcard0_.back as back2_5_0_, idcard0_.front as front3_5_0_, idcard0_.number as number4_5_0_ from caroa.idcard idcard0_ where idcard0_.id=? Hibernate: select users0_.idcardid as idcardid8_8_0_, users0_.id as id1_8_0_, users0_.id as id1_8_1_, users0_.address as address2_8_1_, users0_.age as age3_8_1_, users0_.idcardid as idcardid8_8_1_, users0_.mark as mark4_8_1_, users0_.name as name5_8_1_, users0_.password as password6_8_1_, users0_.picture as picture7_8_1_, users0_.typeid as typeid9_8_1_ from caroa.user users0_ where users0_.idcardid=? Hibernate: select cars0_.userid as userid13_2_0_, cars0_.id as id1_2_0_, cars0_.id as id1_2_1_, cars0_.back as back2_2_1_, cars0_.carName as carName3_2_1_, cars0_.dashboard as dashboar4_2_1_, cars0_.drivingLicense as drivingL5_2_1_, cars0_.front as front6_2_1_, cars0_.gpsMachine as gpsMachi7_2_1_, cars0_.incar as incar8_2_1_, cars0_.key as key9_2_1_, cars0_.left as left10_2_1_, cars0_.right as right11_2_1_, cars0_.type as type12_2_1_, cars0_.userid as userid13_2_1_ from caroa.car cars0_ where cars0_.userid=?
这边我是这么处理的:(这里不需要关联表的属性,只要主表属性)找到User类,把关联表里边getX()的 关系里边加上@JSON(serialize=false)
继续运行:
看控制台,恩,只有1条语句输出。ok的。
继续看页面
页面也输出了我要的json:
贴图了:虽然很丑
搞定,收工~
相关文章推荐
- 深入java虚拟机(九)--常见问题总结
- Java基础--基本数据类型转换
- springMVC中ajax的使用
- java 生成XML 文件
- Spring 依赖配置
- 【转】java URLConnection从网上下载图片或音乐
- java中,map集合排序实例
- ssh环境搭建(2)-config篇(spring4.2.6+struts2.5+hibernate5.1 )
- spring学习之@ModelAttribute运用详解
- java 操作pdf模板(向指定域添加文本内容和图片)
- JAVA WebService开发入门引入
- 动态代理JDK cglib
- jdbc源码详解(一):示例+Driver注册流程
- Java并发编程:Callable、Future和FutureTask
- java 相邻的数据处理
- 设置eclipse的console打印所有日志
- Java关键字this、super使用总结
- 学习java与.net二者互通对比你从中获取的知识更多
- needsRefresh(Lorg/springframework/beans/factory/annotation/InjectionMetadata
- Java8:Stream概念