在serviceImpl里使用自身的方法
@Service("tbLeaveRegisterService")
@Transactional
public class TbLeaveRegisterServiceImpl extends CommonServiceImpl implements TbLeaveRegisterServiceI {
@Autowired
private tbLeaveRegisterService tbLeaveRegisterService;//类似这种情况会如果需要在这个类里使用自身注解的方法,一定要使用this,而不要定性的去用这个类,因为在自身这里注解还没住进去,所以你如果使用了这个将报空指针。
@Autowired
private TbStudentServiceI tbStudentService;
另外说一下关于hibernate的几张表关联使用
先是
<t:dgCol title="主键" field="id" hidden="false" queryMode="single" width="120"></t:dgCol>
<t:dgCol title="班级" align="center" field="depart.departname" hidden="true" query="false" queryMode="single" width="80"></t:dgCol>
一个是主体id 一个是外键id(depart_id)而control需要对分别两个ID赋值并给予不同的modelandvidel。
/**
* 请假新增页面跳转
*
* @return
*/
@RequestMapping(params = "goAddLeave")
public ModelAndView goAddLeave(TbLeaveRegisterEntity tbLeaveRegister, HttpServletRequest req) {
String studentid = oConvertUtils.getString(req.getParameter("studentid"));//学生id
TbStudentEntity student=tbStudentService.getEntity(TbStudentEntity.class, studentid);
/*String departid = oConvertUtils.getString(req.getParameter("departid"));*/
if (StringUtil.isNotEmpty(tbLeaveRegister.getId())) {
tbLeaveRegister = tbLeaveRegisterService.getEntity(TbLeaveRegisterEntity.class, tbLeaveRegister.getId());
req.setAttribute("tbLeaveRegisterPage", tbLeaveRegister);
}
/*tbLeaveRegister.setStudent(student);*/
req.setAttribute("tbStudentPage", student);
/*String departid = oConvertUtils.getString(student.getDepart().getId());*/
/*TSDepart depart=systemService.getEntity(TSDepart.class, departid);
student.setDepart(depart);*/
return new ModelAndView("com/biber/yxyz/leave/tbLeaveRegister-addLeave");
}
/**
* 添加请假登记表
*
* @param ids
* @return
*/
@RequestMapping(params = "doAddLeave")
@ResponseBody
public AjaxJson doAddLeave(TbLeaveRegisterEntity tbLeaveRegister, HttpServletRequest request) {
AjaxJson j = new AjaxJson();
message = "请假登记表添加成功";
try{
String studentid = oConvertUtils.getString(request.getParameter("student.id"));//学生id
String departid = oConvertUtils.getString(request.getParameter("depart.id"));//部门id
TbStudentEntity student=tbStudentService.getEntity(TbStudentEntity.class, studentid);
TSDepart depart=systemService.getEntity(TSDepart.class, departid);
student.setMatter("1");
TSUser user = ResourceUtil.getSessionUserName();
tbLeaveRegister.setCreateuser(user.getUserName());
tbLeaveRegister.setStudent(student);
tbLeaveRegister.setDepart(depart);
student.setLeave(tbLeaveRegister);
tbLeaveRegisterService.save(tbLeaveRegister);
tbStudentService.saveOrUpdate(student);
systemService.addLog(message, Globals.Log_Type_INSERT, Globals.Log_Leavel_INFO);
}catch(Exception e){
e.printStackTrace();
message = "请假登记表添加失败";
throw new BusinessException(e.getMessage());
}
j.setMsg(message);
return j;
}
整个流程差不多是这样。
实体类
@JsonIgnore //getList查询转换为列表时处理json转换异常
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "departid")
public TSDepart getDepart() {
return depart;
}
public void setDepart(TSDepart depart) {
this.depart = depart;
}
@JsonIgnore //getList查询转换为列表时处理json转换异常
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "studentid")
public TbStudentEntity getStudent() {
return student;
}
public void setStudent(TbStudentEntity student) {
this.student = student;
}
过程中主要name不要错就不会有问题了。