HQL的左连接_左连接与右连接的区别

HQL的左连接_左连接与右连接的区别最近做一个查询实现把一个表的记录全部显示出来并且显示关联的另外一个表的记录,这当然谁都知道要用到外连接查询,然而过程并不愉快。在Hibernate的映射文件中配置好关联关系之后,查询的时候可以直接使用比如selectnewmap(student.studentIDasstudentID,student.studentAccountasstudentAccount,student.stu

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

最近做一个查询实现把一个表的记录全部显示出来并且显示关联的另外一个表的记录,这当然谁都知道要用到外连接查询,然而过程并不愉快。

在Hibernate的映射文件中配置好关联关系之后,查询的时候可以直接使用比如

select new map(student.studentID as studentID, student.studentAccount as studentAccount,student.studentName as studentName,student.skill.skill as skill) from Student student where student.studentID!=0 and ........

,但是默认使用的内连接,就是说外键必须匹配的记录才能查出来,实现不了要求。
当我决定用左连接查询之后,做了很多尝试,但是因为对HQL不够熟悉,都没有达到要求。比如这样的

select new map(student.studentID as studentID,student.studentAccount as studentAccount,student.studentName as studentName,student.canInterview as canInterview,student.mail as mail,skill.skill as skill,skill.description as description) from Student student left join student.skill skill with student.studentID!=0 and ........
错误,报错:with-clause expressions did not reference from-clause element to which the with-clause
原因因给是是在with语句里面没有用到skill

改成是这样?

select new map(student.studentID as studentID,student.studentAccount as studentAccount,student.studentName as studentName,student.canInterview as canInterview,student.mail as mail,skill.skill as skill,skill.description as description) from Student student left join student.skill skill where student.skill.skillID = skill.skillID and ....... 虽然有了skill,但是,报错:with-clause referenced two different from-clause elements

幸好没有崩溃,想起来看一下书。其实怪就怪在没想起来用到join…where,where对字段的限制并没有那么严格,但是因为在Student关联的是Skill实体,又不能直接用where而放弃join,所以,正确的语句:

select new map(student.studentID as studentID,student.studentAccount as studentAccount,student.studentName as studentName,student.canInterview as canInterview,student.mail as mail,skill.skill as skill,skill.description as description) from Student student left join student.skill skill where student.studentID!=0 and ......

注意,上面的student.studentID!=0其实没有起到条件筛选作用,而是为了后面接上and条件不会出错。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/193407.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 遗传算法做多目标优化_python 遗传算法

    遗传算法做多目标优化_python 遗传算法多目标遗传优化算法nsga2,python源码实现。源码实现,不适用任何第三方工具包。由于是基本源码,所有熟悉其他语言的也能看懂意思。

    2022年8月23日
    14
  • 详细介绍mysql索引类型:FULLTEXT、NORMAL、SPATIAL、UNIQUE

    详细介绍mysql索引类型:FULLTEXT、NORMAL、SPATIAL、UNIQUEmysql索引类型:FULLTEXT、NORMAL、SPATIAL、UNIQUE的详细介绍Normal普通索引Unique唯一索引FullText全文索引SPATIAL空间索引btree索引和hash索引的区别在实际操作过程中,应该选取表中哪些字段作为索引?Normal普通索引表示普通索引,大多数情况下都可以使用Unique唯一索引表示唯一的,不允许重复的索引,如果该字段信息…

    2022年6月12日
    34
  • ora 01031 权限不足_sql权限不足

    ora 01031 权限不足_sql权限不足原因分析:因为当前用户没有对其他用户的表的修改权限,所有报权限不足的错误。解决办法:把自己所有的权限都给用户B。grantallprivilegestoB;

    2022年4月19日
    219
  • HDU3577 线段树(区间更新)

    HDU3577 线段树(区间更新)

    2021年9月4日
    64
  • linux下解压缩rar格式的文件压缩包

    linux下解压缩rar格式的文件压缩包

    2021年8月19日
    73
  • Excel VBA编程

    Excel VBA编程文章目录如何创建VBAVBA语法规则声明变量给变量赋值让变量存储的数据参与运算关于声明变量的其他知识变量的作用域特殊的变量——数组声明多维数组声明动态数组其他创建数组的方法数组函数利用UBound求数组的最大索引号利用LBound函数求最小索引号求多维数组的最大和最小索引号用join函数将一维数组合并成字符串将数组内容写入工作表中数组的存取特殊数据的专用容器——常量对象,集合及对象的属性和方法VB…

    2022年4月28日
    82

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号