日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

多对多关联查询sql语句

發布時間:2025/3/14 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 多对多关联查询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());}


結果輸出:

==查詢學生1的所有科目成績 course1--99.0 course2--98.0

?

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语句的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。