SSH整合JSON出现的错误以及解决方案

------------------------------------------------------------------------------------------------

1、failed to lazily initialize a collection of role: com.*.*(某一个实体)

------------------------------------------------------------------------------------------------

1
Exception in thread "main" org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed

1
org.hibernate.LazyInitializationException] - could not initialize proxy - the owning Session was closed


原因:Hibernate的lazy问题。

这个是懒加载异常,就是在查询时没有加载关联表的对象,你读取这个关联对象的时候,hibernate的session已经关闭,所以无法获取对象。

你可以在配置文件里关闭懒加载 lazily=false

较简单的解决方案:【已经验证】

 "org.hibernate.LazyInitializationException: could not initialize proxy"延迟抓取出的错,hb3对many-to-one的默认处理是lazy = "proxy",把所有many-to-one,one-to-one都加上lazy="false"...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
< hibernate-mapping >
   < class table = "t_linkurl" name = "com.managerSystem.model.Linkurl" >
     < id access = "field" name = "id" >
       < generator class = "native" />
     </ id >
     < property name = "name" access = "field" />
     < property name = "url" access = "field" />
     < property name = "description" access = "field" />
     < property name = "orderid" access = "field" />
     < many-to-one column = "pid" access = "field" class = "com.managerSystem.model.Linkurl" not-found = "ignore" lazy = "false" name = "parent" />
     < set inverse = "true" access = "field" order-by = "orderid" lazy = "false" name = "children" >
       < key column = "pid" />
       < one-to-many class = "com.managerSystem.model.Linkurl" />
     </ set >
   </ class >
</ hibernate-mapping >


还有一种解决方法【未验证】

2、对于查询中如果用的是xxx.load(class,id)则改为xxx,get(class,id)。在web.xml文件中加入

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
< filter >
   < filter-name >hibernateFilter</ filter-name >
   < filter-class >org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</ filter-class >
                       
   < init-param >
             < param-name >singleSession</ param-name >
             < param-value >false</ param-value >
         </ init-param
                       
<!-- 这个--  < init-param >一定要加不然很可能会报 错:org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode (FlushMode.NEVER) - turn your Session into FlushMode.AUTO or remove 'readOnly' marker from transaction definition
>
</ filter >
                       
< filter-mapping >
   < filter-name >hibernateFilter</ filter-name >
   < url-pattern >*.mmg</ url-pattern >
</ filter-mapping >



------------------------------------------------------------------------------------------------

2、net.sf.json.JSONException: There is a cycle in the hierarchy!

------------------------------------------------------------------------------------------------

【已经验证通过】

net.sf.json.JSONException: There is a cycle in the hierarchy!

at net.sf.json.util.CycleDetectionStrategy$StrictCycleDetectionStrategy.handleRepeatedReferenceAsObject(CycleDetectionStrategy.

主外键关联,产生循环错误

需设置过滤,去掉关联

手写过滤器,去除其关联

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public String listLinkurl() throws Exception
     {
         System.out.println( "1*****************************1" +parentId);
         linkurls = linkurlManager.findLinkurls(parentId);
         List<Linkurl> lus = new ArrayList<Linkurl>();
         //去除关联
         for (Linkurl u : linkurls)
         {
             Linkurl lu = new Linkurl();
             lu.setId(u.getId());
             lu.setName(u.getName());
             lu.setUrl(u.getUrl());
             lu.setDescription(u.getDescription());
             lu.setOrderid(u.getOrderid());
             lus.add(lu);
         }
         JSONObject json = new JSONObject();
         JSONArray ja = new JSONArray();
         JsonConfig jsonConfig = new JsonConfig();
         jsonConfig.setIgnoreDefaultExcludes( false ); 
         jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT); 
         ja = JSONArray.fromObject(lus,jsonConfig);
         json.put( "items" , ja);
         ServletActionContext.getResponse().setContentType( "text/javascript;charset=UTF-8" );
         PrintWriter out = ServletActionContext.getResponse().getWriter();
         out.println(json.toString());
                       
         System.out.println(json.toString());
               
                       
         return SUCCESS;
     }


------------------------------------------------------------------------------------------------

3、Json所需要的JAR包

------------------------------------------------------------------------------------------------

commons-httpclient-3.1.jar
commons-lang-2.4.jar
commons-logging-1.1.1.jar
json-lib-2.2.3-jdk13.jar
ezmorph-1.0.6.jar
commons-collections-3.2.1.jar

1
2
出现java.lang.NoClassDefFoundError: net/sf/ezmorph/Morpher错误是因为没有导入ezmorph.jar文件或版本不对。
出现java.lang.NoClassDefFoundError: org/apache/commons/collections/map/ListOrderedMap错误是因为没有导入commons-collections.jar文件或版本不对。



------------------------------------------------------------------------------------------------

4、Java代码转换成json代码

------------------------------------------------------------------------------------------------

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
1 .       List集合转换成json代码
      
List list = new ArrayList();
      
list.add( "first" );
      
list.add( "second" );
      
JSONArray jsonArray2 = JSONArray.fromObject( list );
      
2 .       Map集合转换成json代码
      
Map map = new HashMap();
      
map.put( "name" , "json" );
      
map.put( "bool" , Boolean.TRUE);
      
map.put( "int" , new Integer( 1 ));
      
map.put( "arr" , new String[] { "a" , "b" });
      
map.put( "func" , "function(i){ return this.arr[i]; }" );
      
JSONObject json = JSONObject.fromObject(map);
      
3 .       Bean转换成json代码
      
JSONObject jsonObject = JSONObject.fromObject( new JsonBean());
      
4 .       数组转换成json代码
      
boolean [] boolArray = new boolean [] { true , false , true };
      
JSONArray jsonArray1 = JSONArray.fromObject(boolArray);
      
       
      
5 . 一般数据转换成json代码
      
JSONArray jsonArray3 = JSONArray.fromObject( "['json','is','easy']" );
      
6 .       beans转换成json代码
      
List list = new ArrayList();
      
JsonBean2 jb1 = new JsonBean2();
      
jb1.setCol( 1 );
      
jb1.setRow( 1 );
      
jb1.setValue( "xx" );
      
JsonBean2 jb2 = new JsonBean2();
      
jb2.setCol( 2 );
      
jb2.setRow( 2 );
      
jb2.setValue( "" );
      
list.add(jb1);
      
list.add(jb2);
      
JSONArray ja = JSONArray.fromObject(list);


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值