hibernate使用ajax向后台获取数据

这里是Struts2加hibernate写的,用的是 ObjectMapper mapper = new ObjectMapper(); 把数据传向前台,我这里写是树形菜单

$(function () { 
    $.ajax({
        type: "post",
        url: "/CRM/sy/rightAction_all.action",
        dataType: "json",
        data: {
        	right_code: "0",
            d: new Date()
        }, async: false,
        success : function(data) { 
			console.log(data); 
		} 
    });
})
package com.zking.web;
 
import java.sql.DriverAction;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.opensymphony.xwork2.ModelDriven;
import com.zking.biz.IRightBiz;
import com.zking.biz.impl.RightBizImpl; 
import com.zking.entity.Right; 
import com.zking.util.PageBean;

public class RightAction extends BaseAction  implements ModelDriven{
	private IRightBiz irb = new RightBizImpl();
	private Right right = new Right();
	private PageBean pageBean = new PageBean();
	private ObjectMapper mapper = new ObjectMapper();
	@Override
	public Right getModel() {
		return right;
	} 

	public String all() throws  Exception {  
		List list=irb.all(right);
		List> maplist = new ArrayList<>();
		for (Right right : list) {
			Map item = new HashMap<>(); 
			item.put("id", right.getRight_code());
			item.put("name", right.getRight_text());
			item.put("spread", "true"); 
			List> r = new ArrayList<>();
			Set st=right.getChildren();
			for (Right t : st) {
				Map item2 = new HashMap<>();
				item2.put("id", t.getRight_code());
				item2.put("name", t.getRight_text());
				item2.put("children", t.getChildren());
				item2.put("url", t.getRight_url()); 
				item2.put("spread", "true");  
				List> s1 = new ArrayList<>();
				Set st2=t.getChildren();
				for (Right t2 : st2) {
					Map item3 = new HashMap<>();
					item3.put("id", t2.getRight_code());
					item3.put("name", t2.getRight_text());
					item3.put("children", t2.getChildren());
					item3.put("url", t2.getRight_url()); 
					item3.put("spread", "true"); 
					s1.add(item3); 
				}
				item2.put("children", s1);
				r.add(item2);
				item.put("children", r);
			}
			maplist.add(item);
		} 
		Map map = new HashMap<>();
		map.put("rows", maplist);
		mapper.writeValue(response.getOutputStream(), map); 
		return null;
	}
 
}

上面如果直接把返回的list集合放入mapper里会出现com.fasterxml.jackson.databind.JsonMappingException: could not initialize proxy [com.zking.entity.Right#] - no Session (through reference chain: java.util.HashMap[“rows”]->java.util.ArrayList[0]->com.zking.entity.Right[“right”]->com.zking.entity.Right H i b e r n a t e P r o x y HibernateProxy HibernateProxykXA9B1wn[“right_type”])的问题,
具体怎么解决呢?唔也不知

这里是dao方法,Right是实体类

public List all(Right right) {
		Session session = SessionFactoryUtil.getSession();
		Transaction transaction = session.beginTransaction();
		String sql = "from Right where  1=1 ";
		if (StringUtils.isNotBlank(right.getRight_code())) {
			sql += " and right_code = '" + right.getRight_code() + "' ";
		}
		List list = session.createQuery(sql).list(); 
		transaction.commit();
		session.close();
		return list;
	}

这里是配置文件,用了一对多,多对一的关系,还有懒加载




	
	
		
			
			
			
		 
		
		
		
		

		
		
		
		
		
			
			
		

	

你可能感兴趣的:(hibernate使用ajax向后台获取数据)