为什么这个 hql 语句就像一个内部连接?
Why this hql statement is like an inner join?
为什么这个 HQL 语句像内部连接?
select
user.id,
user.allocationVersion,
user.tx.statusId,
user.userId,
user.nameFirst,
user.nameLast,
user.email1,
user.statusId,
user.tx.name,
user.note1,
user.note2
from
module.bb.jpa.User as user
where user.clientId = :clientId
order by user.id DESC
用户class中引用的TX
class可以是null
。为什么我得到的结果就像我执行 inner join
一样?我刚得到 User
s 和 TX
但我想要所有这些。
因为您在用户中引用了tx.name
。如果您在 Java 中访问它,但在 HQL 中访问它以获取该列的值,它应该不起作用 Hibernate 进行隐式连接。要获取所有值,您应该明确执行并包括外部连接选项。
HQL reference 在第 14.4 节中谈到了这一点:
HQL supports two forms of association joining: implicit and explicit.
The queries shown in the previous section all use the explicit form,
that is, where the join keyword is explicitly used in the from clause.
This is the recommended form.
The implicit form does not use the join keyword. Instead, the
associations are "dereferenced" using dot-notation. implicit joins can
appear in any of the HQL clauses. implicit join result in inner joins
in the resulting SQL statement.
from Cat as cat where cat.mate.name like '%s%'
[由我突出显示]。
你的HQL基本等同于:
select
user.id,
user.allocationVersion,
user.tx.statusId,
user.userId,
user.nameFirst,
user.nameLast,
user.email1,
user.statusId,
user.tx.name,
user.note1,
user.note2
from
module.bb.jpa.User as user
join module.bb.jpa.Tx as tx -- This is an abbreviated inner join
where user.clientId = :clientId
order by user.id DESC
为了 "correct" 这种行为,您必须明确指定 left outer join
或 left join
简称。
为什么这个 HQL 语句像内部连接?
select
user.id,
user.allocationVersion,
user.tx.statusId,
user.userId,
user.nameFirst,
user.nameLast,
user.email1,
user.statusId,
user.tx.name,
user.note1,
user.note2
from
module.bb.jpa.User as user
where user.clientId = :clientId
order by user.id DESC
用户class中引用的TX
class可以是null
。为什么我得到的结果就像我执行 inner join
一样?我刚得到 User
s 和 TX
但我想要所有这些。
因为您在用户中引用了tx.name
。如果您在 Java 中访问它,但在 HQL 中访问它以获取该列的值,它应该不起作用 Hibernate 进行隐式连接。要获取所有值,您应该明确执行并包括外部连接选项。
HQL reference 在第 14.4 节中谈到了这一点:
HQL supports two forms of association joining: implicit and explicit.
The queries shown in the previous section all use the explicit form, that is, where the join keyword is explicitly used in the from clause. This is the recommended form.
The implicit form does not use the join keyword. Instead, the associations are "dereferenced" using dot-notation. implicit joins can appear in any of the HQL clauses. implicit join result in inner joins in the resulting SQL statement.
from Cat as cat where cat.mate.name like '%s%'
[由我突出显示]。
你的HQL基本等同于:
select
user.id,
user.allocationVersion,
user.tx.statusId,
user.userId,
user.nameFirst,
user.nameLast,
user.email1,
user.statusId,
user.tx.name,
user.note1,
user.note2
from
module.bb.jpa.User as user
join module.bb.jpa.Tx as tx -- This is an abbreviated inner join
where user.clientId = :clientId
order by user.id DESC
为了 "correct" 这种行为,您必须明确指定 left outer join
或 left join
简称。