我们在编程时发生的灵异事件非常多,我们先来看一个例子,
public class Department extends EntityBase{ //....省略 @ManyToOne(targetEntity=Department.class,cascade={CascadeType.REFRESH},fetch=FetchType.EAGER,optional=true) @JoinColumn(name="parentDepartmentIds",columnDefinition="bigint",nullable=true) private Department parentDepartment;//上级部门 ...省略 }
这个对象是自身的实体类型(Department ),而现在要把获取到的值传过去
String departName= request.getParameter("department.names");//部门名称
这是一个接口,在浏览器url里面给参数进行测试,那么怎么将这个String类型的值转换成自身实体类型的数据,存到对象里呢?下面是方法全部代码,请大家看一下,帮帮我阿
public void AddDepartment(){ try { User user = Context.getCurrentUser(); Department dd=new Department(); dd.setCompanyCode("100000"/*user.getCompanyCode()*/);//动态部门编号 String fadepart= request.getParameter("parentDept");//父部门 System.out.println("编号:"+user.getCompanyCode()); String departName= request.getParameter("department.names");//部门名称 String departMs= request.getParameter("department.description");//部门描述 String Peple= request.getParameter("department.principal.names");//负责人 String str=new String(departName.getBytes("ISO-8859-1"),"utf-8"); String str2=new String(departMs.getBytes("ISO-8859-1"),"utf-8"); //dd.setParentDepartment(fadepart); dd.setNames(new String(str) ); dd.setDescription(str2); dd.setCreatedDate(new Date()); /*User uu=new User(); long id=1; uu.setIds(id); dd.setPrincipal(uu);*/ int r=departmentServiceImpl.AddDepartment(dd); System.out.println("r:"+r); Map mm=new HashMap(); if(r ==-1){ mm.put("status", "1"); mm.put("message", "添加失败"); JSONObject jsonObject = JSONObject.fromObject(mm); outJSONData(jsonObject); }else{ mm.put("status", "0"); mm.put("message", "添加成功"); JSONObject jsonObject = JSONObject.fromObject(mm); outJSONData(jsonObject); } JSONObject jsonObject = JSONObject.fromObject(mm); outJSONData(jsonObject); } catch (Exception e) { outStringData("-2");//系统异常 e.printStackTrace(); } }