String yhxxhql = "select yuid from Yonghuxinxi a where 1=1 ";
Session session = kaoqinDao.getSessionFactory().getCurrentSession();
Query query = session.createQuery(yhxxhql);
List listyh = query.list();
//查考勤班次表,取kqbcid
String bancihql = "select kqbcid from Kaoqinbanci k where 1=1";
Query query2 = session.createQuery(bancihql);
List listbc = query2.list();
//判断操作标识符
String opflag=kaoqinModel.getOpflag();
String kqid = kaoqinModel.getKqid();
Kaoqin kaoqin = null;
if (opflag.equals("add")) {
kaoqin = new Kaoqin();
} else if (opflag.equals("edit")) {
kaoqin = this.kaoqinDao.findById(kqid);
if (kaoqin == null ) {
throw new ClassNotFoundException("系统查无此用户");
}
kaoqinModel.setAdddate(kaoqin.getAdddate());// 编辑状态下,保留原来的adddate
}else{
throw new Exception("无此操作");
}
// 初始化日期
if (opflag.equals("add")) {
kaoqin.setAdddate(new Date());
kaoqin.setEditdate(new Date());
}else if (opflag.equals("edit")) {
// 编辑状态下,yonghu里面的adddate容易被model替换成null,注意
kaoqin.setEditdate(new Date());
}else{
throw new Exception("无此操作");
}
// 保存数据
if (opflag.equals("add")) {
if(listyh != null){
for(int i = 0;i
kaoqinModel.setKqbcid(listyh.get(i).toString());
for(int j = 0;j
ComObj.commonOperation("insert", this.kaoqinDao, kaoqin);
}
}
}
}
}else if (opflag.equals("edit")) {
// 先把中间表数据删掉,再重新插入
// Session session = kaoqinDao.getSessionFactory()
// .getCurrentSession();
// 去掉readonly限制,不要用AUTO属性,AUTO属性会插入主表后,才删除中间表信息,导致缺失中间表数据
session.setFlushMode(FlushMode.COMMIT);
Transaction trans = session.beginTransaction();
try {
session.saveOrUpdate(kaoqin);// 保存用户
session.flush();
trans.commit();
} catch (Exception e) {
trans.rollback();
e.printStackTrace();
throw new Exception("编辑失败" + e);
}
}else{
throw new Exception("无此操作");
}