[英]How to perform an EAGER fetch in JPQL
I was wondering in JPQL (not on the entity mappings) how to explicitly fetch an associated field eagerly. 我想知道在JPQL中(而不是在实体映射上)如何急切地显式获取关联的字段。
I have ContactAddressLink
(took out the annotations for brevity) 我有
ContactAddressLink
(为简洁起见,请注意注释)
class ContactAddressLink {
Contact contact;
Address address;
... some extra fields ...
}
So I have a query that would go 所以我有一个查询
select cal from Contact c, Address a, ContactAddressLink cal where
cal.contact = c and cal.address = a
Which gives me the query I expected. 这给了我期望的查询。 However, since I would use the addresses after I see a bunch of queries getting the each address.
但是,由于我会在看到一系列查询获取每个地址之后使用这些地址。
What I want to do is something like 我想做的是
select cal eager fetch cal.a from Contact c, Address a, ContactAddressLink cal where
cal.contact = c and cal.address = a
I recall seeing something like that, but I can't remember the exact syntax. 我记得看到过类似的东西,但我不记得确切的语法。
Yup. 对。 You are right.
你是对的。 The syntax is
[inner|left] join fetch
. 语法为
[inner|left] join fetch
。 Example : 范例:
select cal from ContactAddressLink cal
inner join fetch cal.contact c
inner join fetch cal.address a
where cal.id = 123456789
If you want to match ContactAddressLink
if it has Contact
/ Address
, use inner join fetch
. 如果要与具有
Contact
/ Address
ContactAddressLink
匹配,请使用inner join fetch
。
If you want to match ContactAddressLink
even if it does not have Contact
/ Address
, use left join fetch
. 如果即使没有
Contact
/ Address
也要匹配ContactAddressLink
,请使用left join fetch
。
Remember as per JPA Specification
all @OneToOne
and @ManyToOne
are fetched eagerly. 请记住,按照
JPA Specification
所有@OneToOne
和@ManyToOne
被急切获取。 You should change it to lazy if not, you will fetch those objects always. 如果没有,则应将其更改为惰性,否则将始终获取那些对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.