多对多关联查询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语句相关推荐
- 用户表-角色表-权限表多表关联查询sql语句
在企业系统中经常会使用到给用户分配权限的情况,往往在用户信息表和权限表之间还维护了一张角色表,即通过给用户添加角色,角色添加权限的这样一种方式来给用户间接的添加权限. 如图示例 那么,查询用户权限的多 ...
- Bootstrap4+MySQL前后端综合实训-Day08-AM【多表查询sql语句、关联数据的假删除、自动增长主键的获取、栏目管理“数据编辑”按钮的实现】
[Bootstrap4前端框架+MySQL数据库]前后端综合实训[10天课程 博客汇总表 详细笔记][附:实训所有代码] 目 录 多表查询sql语句 关联数据的假删除(status状态码/数据可恢 ...
- SpringBoot实现1对1、1对多、多对多关联查询——基于SpringBoot和Vue的后台管理系统项目系列博客(十八)
系列文章目录 系统功能演示--基于SpringBoot和Vue的后台管理系统项目系列博客(一) Vue2安装并集成ElementUI--基于SpringBoot和Vue的后台管理系统项目系列博客(二) ...
- mybatis实现多对多关联查询(超详细版)
mybatis实现多对多关联查询XML实现 在开发过程中,持久层架mybatis为我们封装了SQL操作,只需要提供相应的SQL语句即可查询出结果,若结合逆向工程插件便可免去写一些简单SQL的繁琐工 ...
- MySQL的基础架构以及一条查询sql语句的执行流程
详细介绍了Mysql的基础架构以及一条查询sql的执行流程. 如果不想作为一个只能简单的写写sql工程师,而是想要深入的学习MySQL,那么我们有必要首先从宏观的角度来了解MySQL的整体架构,只有把 ...
- java多对多关联数据操作,hibernate实施多对多关联查询时,关联表数据被删除
hibernate执行多对多关联查询时,关联表数据被删除 本帖最后由 binbb521 于 2012-12-04 11:48:29 编辑 S2SH框架开发的网站,执行两个多对多关系的表查询时,关联两个 ...
- 原生sql 查询返回一个实体_python连接SQLServer执行给定的查询SQL语句,并返回结果数据...
今天写这篇文章的初衷就是在我昨天发表的文章<Python连接SQLServer数据库执行增删改查操作>后,一些读者反馈说跟Python完全没有关系,这里我想说的是:内容的确是与Python ...
- MySql-一条查询SQL语句的执行
MySql-一条查询SQL语句的执行 你好! 感谢您花费几分钟的时间阅读本篇博客,本人写的是方便学习与记录,要是有错误的地方请您指出,感谢 1 缓存 这个的缓存不是指Redis,或者Mybat ...
- oracle 数据库循环查询语句怎么写,sql循环查询(sql语句循环查询详解)
sql循环查询(sql语句循环查询详解) 2020-07-24 11:15:51 共10个回答 select*fromawheret_idin('3','4');select*fromawheret_ ...
最新文章
- ARC指南 strong和weak指针
- 用CSS3制作很特别的波浪形菜单
- 人类基因测序被重新定义,时间减半创世界纪录
- cc、gcc、g++ 的区别和联系
- 巧用linux版powershell,管理linux下的docker
- plus 什么是mybais_谈谈自己用mybatis-plus中的一些经验。
- C语言指针和数组概述
- python的字符编码叙述_Python: 熟悉又陌生的字符编码
- 可燃气体浓度多少合格_科普:气体容积单位LEL%、VOL%、TLV(ppm)之间如何换算?...
- 从源码入手,一文带你读懂Spring AOP面向切面编程
- 一些比较隐秘的OJ的网址
- Linux下如何查找nginx配置文件的位置
- 梦幻西游qq表情包YR
- 华为路由器配置静态路由默认路由
- iPhone删掉的照片能恢复吗?iPhone最近删除的照片怎么恢复?
- Element Black 打造 NFT 新形式
- Aspose.Word企业案例:Acumen Fuse 使用 Aspose 组件将项目分析数据导出到 Microsoft Word 和 Excel
- lin通信ldf文件解析_LIN通讯机制
- JIT - 即时编译
- COM的INTERFACE使用实践
热门文章
- STM32开发 -- 复位类型判断
- pat天梯赛L2-025. 分而治之
- 第五人格每天服务器维护多长时间,第五人格:每天玩的时间并不长,大概一天1-5局...
- dw按钮图片滚动js_轮播图--swiper插件/原生js/jQuery
- Java未被捕获的异常
- ELF文件装载链接过程及hook原理
- C语言结构体里的成员数组和指针
- JZOJ 3853. 【NOIP2014八校联考第2场第2试9.28】帮助Bsny(help)
- android 动态移动xy,android – 如何使用AChartEngine动态线图和X轴自动平移(滚动)?...
- linux mysql安装失败 lib冲突问题_Linux 安装 Mysql 冲突 问题