多对多关联查询sql语句
1.student,score,coure的實體關聯定義如下:
------------------------------------------------------------------------------------------------
Student:
@Entity
@Table(name="tb_student")
public class Student implements Serializable {
????? .....
private Set<Course> courses=new HashSet<Course>();
private Set<Score> scores=new HashSet<Score>();
.....
@ManyToMany
@JoinTable(name="tb_student_course",
??? joinColumns=@JoinColumn(name="student_id",referencedColumnName="id"),
??? inverseJoinColumns=@JoinColumn(name="course_id",referencedColumnName="id")
??? )
public Set<Course> getCourses() {
return courses;
}
public void setCourses(Set<Course> courses) {
this.courses = courses;
}
@OneToMany(mappedBy="student")
public Set<Score> getScores() {
return scores;
}
public void setScores(Set<Score> scores) {
this.scores = scores;
}
}
-----------------------------------------------------------------------------------------------
Score:
@Entity @Table(name="tb_score") public class Score implements Serializable {@ManyToOne@JoinColumn(name="course_id")public Course getCourse() {return course;}public void setCourse(Course course) {this.course = course;}@ManyToOne@JoinColumn(name="student_id")public Student getStudent() {return student;}public void setStudent(Student student) {this.student = student;}}-------------------------------------------------------------------------------------------------Course實體無關聯注解。
--------------------------------------------------------------------------------------------------------------
student跟course是多對多單向,student可訪問course
student跟score是一對多雙向
score跟course是多對一單向,score可訪問course
---------------------------------------------------------------------------------------------------------------
查找學生1所有課程的所有成績:
public List<Score> findScoreByStudentId(Integer id) {List<Score> ls=em.createQuery("select score from Student s join s.scores score where s.id=:id") .setParameter("id", id) .getResultList();return ls;}?
客戶端:
ScoreDAO scoredao=(ScoreDAO)cxt.lookup("ScoreDAOBean/remote");List<Score> scores1=scoredao.findScoreByStudentId(new Integer(1));System.out.println("==查詢學生1的所有科目成績");for(Score s:scores1 ){System.out.println(s.getCourse().getName()+"--"+s.getScore());}
結果輸出:
?
sql輸出:
22:21:07,765 INFO [STDOUT] Hibernate: selectscores1_.id as id19_,scores1_.course_id as course4_19_,scores1_.student_id as student3_19_,scores1_.score as score19_ fromtb_student student0_ inner jointb_score scores1_ on student0_.id=scores1_.student_id wherestudent0_.id=? 22:21:07,765 INFO [STDOUT] Hibernate: selectcourse0_.id as id18_0_,course0_.name as name18_0_,course0_.description as descript3_18_0_,course0_.optional as optional18_0_,course0_.teacher as teacher18_0_ fromtb_course course0_ wherecourse0_.id=? 22:21:07,765 INFO [STDOUT] Hibernate: selectstudent0_.id as id20_1_,student0_.name as name20_1_,student0_.description as descript3_20_1_,student0_.class_id as class9_20_1_,student0_.temporary as temporary20_1_,student0_.age as age20_1_,student0_.sex as sex20_1_,student0_.birthday as birthday20_1_,student0_.createDate as createDate20_1_,classeo1_.id as id17_0_,classeo1_.classname as classname17_0_ fromtb_student student0_ left outer jointb_class classeo1_ on student0_.class_id=classeo1_.id wherestudent0_.id=? 22:21:07,781 INFO [STDOUT] Hibernate: selectcourse0_.id as id18_0_,course0_.name as name18_0_,course0_.description as descript3_18_0_,course0_.optional as optional18_0_,course0_.teacher as teacher18_0_ fromtb_course course0_ wherecourse0_.id=??
默認及時加載???(不是對集合默認延遲加載嗎?),JPQL將查詢改為如下也可以:
public List<Score> findScoreByStudentId(Integer id) {List<Score> ls=em.createQuery("select s.scores from Student s where s.id=:id") .setParameter("id", id) .getResultList();return ls;}?結果輸出:==查詢學生1的所有科目成績 course1--99.0 course2--98.0?
?
輸出的sql語句如下:
22:36:55,546 INFO [STDOUT] Hibernate: selectscores1_.id as id19_,scores1_.course_id as course4_19_,scores1_.student_id as student3_19_,scores1_.score as score19_ fromtb_student student0_ inner jointb_score scores1_ on student0_.id=scores1_.student_id wherestudent0_.id=? 22:36:55,546 INFO [STDOUT] Hibernate: selectcourse0_.id as id18_0_,course0_.name as name18_0_,course0_.description as descript3_18_0_,course0_.optional as optional18_0_,course0_.teacher as teacher18_0_ fromtb_course course0_ wherecourse0_.id=? 22:36:55,546 INFO [STDOUT] Hibernate: selectstudent0_.id as id20_1_,student0_.name as name20_1_,student0_.description as descript3_20_1_,student0_.class_id as class9_20_1_,student0_.temporary as temporary20_1_,student0_.age as age20_1_,student0_.sex as sex20_1_,student0_.birthday as birthday20_1_,student0_.createDate as createDate20_1_,classeo1_.id as id17_0_,classeo1_.classname as classname17_0_ fromtb_student student0_ left outer jointb_class classeo1_ on student0_.class_id=classeo1_.id wherestudent0_.id=? 22:36:55,562 INFO [STDOUT] Hibernate: selectcourse0_.id as id18_0_,course0_.name as name18_0_,course0_.description as descript3_18_0_,course0_.optional as optional18_0_,course0_.teacher as teacher18_0_ fromtb_course course0_ wherecourse0_.id=?轉載于:https://www.cnblogs.com/cxccbv/archive/2009/01/24/1380796.html
總結
以上是生活随笔為你收集整理的多对多关联查询sql语句的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 抽象工厂模式(C#)
- 下一篇: 写一个ajax程序就是如此简单