package action; import Bean.*; import org.hibernate.Hibernate; import org.hibernate.Session; import tools.HibernateTools; import java.util.List; public class Class extends HibernateTools { private Session session; private int classid,numsum; private String classname; private int key; private List<ClassEntity> class_list; public String addclass(){ if (classid==0||classname==null) { addActionError("输入的参数有误,请重新输入"); return INPUT; } //更新班级人数 numsum=sumnumber(classid); session=init(); ClassEntity clazzEntity=new ClassEntity(); if (session.get(ClassEntity.class, classid)!=null){ addActionError("该班级已经存在"); return ERROR; } clazzEntity.setClassId(classid); clazzEntity.setName(classname); clazzEntity.setSumNum(numsum); session.save(clazzEntity); destory(); addActionError("操作成功"); return SUCCESS; } private int sumnumber(int classid) { if (session==null){ session=init(); } int num=session.createQuery("select count(*) from StudentEntity stu where stu.classId = "+classid).getFirstResult(); return num; } public static void main(String arg[]){//main--来自高金磊 //测试 init(); // Class c=new Class(); // c.setClassid(102); // c.setClassname("测试"); // //c.addclass(); // c.setKey(123); // c.showClass(); destory(); } public String showClass(){ if (session==null||!session.isOpen()){ session=init(); } class_list=session.createQuery("from ClassEntity").getResultList(); for (int n=0;n<class_list.size();n++) { List<Long> num=session.createQuery("select count(*) from StudentEntity where classId="+class_list.get(n).getClassId()).getResultList(); class_list.get(n).setSumNum(num.get(0).intValue()); session.saveOrUpdate(class_list.get(n)); } destory(); return SUCCESS; } public String deleteClass(){ if (session==null||!session.isOpen()){ session=init(); } if (key==0){ return INPUT; } ClassEntity clazzEntity=session.get(ClassEntity.class,key); session.delete(clazzEntity); destory(); return SUCCESS; } public int getClassid() { return classid; } public void setClassid(int classid) { this.classid = classid; } public int getNumsum() { return numsum; } public void setNumsum(int numsum) { this.numsum = numsum; } public String getClassname() { return classname; } public void setClassname(String classname) { this.classname = classname; } public int getKey() { return key; } public void setKey(int key) { this.key = key; } public List<ClassEntity> getClass_list() { return class_list; } public void setClass_list(List<ClassEntity> class_list) { this.class_list = class_list; } }