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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 分布式日志传输系统Databus(一)–系统介绍「建议收藏」

    分布式日志传输系统Databus(一)–系统介绍「建议收藏」Databus系统是微博DIP团队开源的分布式日志传输系统。它是一个分布式、高可用的,用于采集和移动大量日志数据的服务。它基于流式数据的简单而灵活的架构,具备健壮性和容错性,具有故障转移与恢复机制。它

    2022年7月2日
    23
  • 简述controller,service,repository注解的用法(谈谈application.properties的作用)

    Spring2.5中除了提供@Component注释外,还定义了几个拥有特殊语义的注释,它们分别是:@Repository、@Service和@Controller。在目前的Spring版本中,这3个注释和@Component是等效的,但是从注释类的命名上,很容易看出这3个注释分别和持久层、业务层和控制层(Web层)相对应。1、@Component@Comp…

    2022年4月14日
    76
  • pushback流的例子

    pushback流的例子pushback 流有 PushbackInpu 和 PushbackRead nbsp 例子 nbsp publicclassS publicstatic String args throwsIOExce PushbackInpu newPushbackI

    2025年7月29日
    4
  • origin安装嵌入python_python爬虫之git的使用(origin说明)

    origin安装嵌入python_python爬虫之git的使用(origin说明)1、首先我们回忆两个命令#gitremoteaddorigin远程仓库链接#gitpush-uoriginmaster我们一起看看这个命令,git是git的一级命令,push就是下载,-u应该使用用账户验证maser就是分支的名字(前面我们说过),那么这个origin是个什么鬼?大家看看下面的这个5毛钱图,就能发现,其实origin就是远程仓库的名称。如果不相信在看看我的配置文件#…

    2022年5月3日
    97
  • rsyslogd日志管理

    rsyslogd日志管理课程大纲1.日志管理简介2.rsyslogd日志服务3.日志轮替日志管理就是可以保存系统所有的想记录的信息首先要确定服务启动。psaux|greprsyslogd#查看服务是否启动chkconfig–list|greprsyslog#查看服务是否启动rsyslog的配置文件位置:/ect/rsyslog.conf常见日志的作用/v…

    2022年8月15日
    10
  • apache服务器搭建教程_apache本地服务器

    apache服务器搭建教程_apache本地服务器一、下载安装配置服务器1.下载1.百度搜索downlaodapache2.选择windows版本http://httpd.apache.org/download.cgi3.http://httpd.apache.org/docs/current/platform/windows.html#down4.下载下载解压后,目录结构2..配置配置文件位置:Apache24/conf/httpd.conf1.配置根目录(SRVROOT)$…

    2025年6月5日
    4

发表回复

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

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