Java Querydsl 在查询中设置获取模式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23627482/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 00:23:20  来源:igfitidea点击:

Querydsl set fetch mode in a query

javahibernatequerydsl

提问by Marko

I have a situation where a Card entity has a foreign key to a Person.

我有一个卡实体有一个人的外键的情况。

public class Card  implements java.io.Serializable {
 private String cardid;    
 private Person person;

@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="USERID")
public Person getPerson() {
    return this.person;
}
}

The default fetch type for the person is LAZY. Can I specify the fetch type to EAGER within a query:

该人的默认提取类型是 LAZY。我可以在查询中指定 EAGER 的提取类型吗:

QCard qCard = QCard.card;
JPQLQuery query = getQuery().from(qCard);
query.list(qCard);

Thanks for any help.

谢谢你的帮助。

采纳答案by Timo Westk?mper

Did you try

你试过了吗

QCard qCard = QCard.card;
List<Card> cards = getQuery().from(qCard)
    .innerJoin(qCard.person).fetch()
    .list(qCard);


For QueryDSL 4.0.2+

对于 QueryDSL 4.0.2+

QCard qCard = QCard.card;
List<Card> cards = getQuery().from(qCard)
    .innerJoin(qCard.person).fetchJoin()
    .select(qCard).fetch();