Department dep = null;
int list = sl.check(dept);
if( list == 0){
Department departid = addDept(dept);
System.out.println(departid.getdeptid());
emp.setdeptid(departid.getdeptid());
}
else{
dep = sl.getDept(list);
emp.setdeptid(dep.getdeptid());
}
sl.add(emp);
如果存在或不存在具有特定部门名称的记录,如果存在获取该记录的id,则其他人将创建新记录并获取该记录的ID
public int check(String dept){
SQLQuery q = session.createSQLQuery("select d.deptid as deptid from Department d where d.dept=:deptname");
q.setParameter("deptname", dept);
Object obj = q.uniqueResult();
if(obj==null){
return 0;
}
else{
int departid = (Integer)obj;
return departid ;
}
}
addDept函数用于保存记录sl是包含addDeptLogic函数的对象,其中session.save(department)存在
private Department addDept(String dept) {
Department dep = new Department();
dep.setdept(dept);
sl.addDeptLogic(dep);
return dep;
}
当部门已经存在时它工作正常当必须创建新部门并且时间dep变为空时它不起作用