package model; import java.util.Date; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.Table; import org.hibernate.annotations.GenericGenerator; @Entity @Table(name="t_exam") public class Exam { private int id; private Student student; private Paper paper; private int singleScore; private int moreScore; private int score; private Date examDate; @Id @GeneratedValue(generator="_native") @GenericGenerator(name="_native",strategy="native") public int getId() { return id; } public void setId(int id) { this.id = id; } @ManyToOne @JoinColumn(name="studentId") public Student getStudent() { return student; } public void setStudent(Student student) { this.student = student; } @ManyToOne @JoinColumn(name="paperId") public Paper getPaper() { return paper; } public void setPaper(Paper paper) { this.paper = paper; } public int getSingleScore() { return singleScore; } public void setSingleScore(int singleScore) { this.singleScore = singleScore; } public int getMoreScore() { return moreScore; } public void setMoreScore(int moreScore) { this.moreScore = moreScore; } public int getScore() { return score; } public void setScore(int score) { this.score = score; } public Date getExamDate() { return examDate; } public void setExamDate(Date examDate) { this.examDate = examDate; } }
package model; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; import javax.persistence.Transient; @Entity @Table(name="t_student") public class Student { private String id; private String name; private String password; private String sex; private String prefession; private String cardNo; private String flag="2"; @Id @Column(name="id",unique=true,nullable=false,length=40) public String getId() { return id; } public void setId(String id) { this.id = id; } @Column(name="name",length=20) public String getName() { return name; } public void setName(String name) { this.name = name; } @Column(name="password",length=20) public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Column(name="sex",length=5) public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } @Column(name="prefession",length=40) public String getPrefession() { return prefession; } public void setPrefession(String prefession) { this.prefession = prefession; } @Column(name="cardNo",length=50) public String getCardNo() { return cardNo; } public void setCardNo(String cardNo) { this.cardNo = cardNo; } @Transient public String getFlag() { return flag; } public void setFlag(String flag) { this.flag = flag; } }
package model; import java.util.Date; import java.util.HashSet; import java.util.Set; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.OneToMany; import javax.persistence.Table; import org.hibernate.annotations.GenericGenerator; @Entity @Table(name="t_paper") public class Paper { private int id; private String paperName; private Date joinDate; private Set<Question> questions=new HashSet<Question>(); @Id @GeneratedValue(generator="_native") @GenericGenerator(name="_native",strategy="native") public int getId() { return id; } public void setId(int id) { this.id = id; } public String getPaperName() { return paperName; } public void setPaperName(String paperName) { this.paperName = paperName; } public Date getJoinDate() { return joinDate; } public void setJoinDate(Date joinDate) { this.joinDate = joinDate; } @OneToMany(mappedBy="paper",fetch=FetchType.EAGER) public Set<Question> getQuestions() { return questions; } public void setQuestions(Set<Question> questions) { this.questions = questions; } }
public List<Exam> getExams(Exam s_exam,PageBean pageBean)throws Exception { Session session = HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction(); StringBuffer hql = new StringBuffer("from Exam exam"); if(s_exam.getStudent() != null && StringUtil.isNotEmpty(s_exam.getStudent().getId())) { hql.append(" and exam.student.id = '"+s_exam.getStudent().getId()+"'"); } if(s_exam.getStudent()!=null && StringUtil.isNotEmpty(s_exam.getStudent().getName())) { hql.append(" and exam.student.name like '%"+s_exam.getStudent().getName()+"%'"); } Query query=session.createQuery(hql.toString().replaceFirst("and", "where")); if(pageBean != null) { query.setFirstResult(pageBean.getStart()); query.setMaxResults(pageBean.getPageSize()); } @SuppressWarnings("unchecked") List<Exam> examList = query.list(); session.getTransaction().commit(); return examList; }
package model; import java.util.Date; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.Table; import javax.persistence.Transient; import org.hibernate.annotations.Cascade; import org.hibernate.annotations.CascadeType; import org.hibernate.annotations.GenericGenerator; @Entity @Table(name="t_question") public class Question { private int id; private String subject; private String type; private Date joinTime; private String optionA; private String optionB; private String optionC; private String optionD; private String answer; private String userAnswer; private Paper paper; @Id @GeneratedValue(generator="_native") @GenericGenerator(name="_native",strategy="native") public int getId() { return id; } public void setId(int id) { this.id = id; } public String getSubject() { return subject; } public void setSubject(String subject) { this.subject = subject; } public String getType() { return type; } public void setType(String type) { this.type = type; } public Date getJoinTime() { return joinTime; } public void setJoinTime(Date joinTime) { this.joinTime = joinTime; } public String getOptionA() { return optionA; } public void setOptionA(String optionA) { this.optionA = optionA; } public String getOptionB() { return optionB; } public void setOptionB(String optionB) { this.optionB = optionB; } public String getOptionC() { return optionC; } public void setOptionC(String optionC) { this.optionC = optionC; } public String getOptionD() { return optionD; } public void setOptionD(String optionD) { this.optionD = optionD; } @Transient public String getUserAnswer() { return userAnswer; } public void setUserAnswer(String userAnswer) { this.userAnswer = userAnswer; } @ManyToOne @Cascade(value={CascadeType.SAVE_UPDATE}) @JoinColumn(name="paperId") public Paper getPaper() { return paper; } public void setPaper(Paper paper) { this.paper = paper; } public String getAnswer() { return answer; } public void setAnswer(String answer) { this.answer = answer; } }
控制台输出:
Hibernate: select exam0_.id as id1_0_, exam0_.examDate as examDate2_0_, exam0_.moreScore as moreScor3_0_, exam0_.paperId as paperId6_0_, exam0_.score as score4_0_, exam0_.singleScore as singleSc5_0_, exam0_.studentId as studentI7_0_ from t_exam exam0_ limit ? 首先它把exam表的信息全部查询了出来
Hibernate: select paper0_.id as id1_2_0_, paper0_.joinDate as joinDate2_2_0_, paper0_.paperName as paperNam3_2_0_, questions1_.paperId as paperId10_2_1_, questions1_.id as id1_3_1_, questions1_.id as id1_3_2_, questions1_.answer as answer2_3_2_, questions1_.joinTime as joinTime3_3_2_, questions1_.optionA as optionA4_3_2_, questions1_.optionB as optionB5_3_2_, questions1_.optionC as optionC6_3_2_, questions1_.optionD as optionD7_3_2_, questions1_.paperId as paperId10_3_2_, questions1_.subject as subject8_3_2_, questions1_.type as type9_3_2_ from t_paper paper0_ left outer join t_question questions1_ on paper0_.id=questions1_.paperId where paper0_.id=? t_paper和t_question 左关联查询
Hibernate: select student0_.id as id1_4_0_, student0_.cardNo as cardNo2_4_0_, student0_.name as name3_4_0_, student0_.password as password4_4_0_, student0_.prefession as prefessi5_4_0_, student0_.sex as sex6_4_0_ from t_student student0_ where student0_.id=? 把学生表的所有信息查询了出来