Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Querydsl set fetch mode in a query

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:

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

Thanks for any help.

like image 961
Marko Avatar asked May 13 '14 09:05

Marko


1 Answers

Did you try

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

For QueryDSL 4.0.2+

QCard qCard = QCard.card;
List<Card> cards = getQuery().from(qCard)
    .innerJoin(qCard.person).fetchJoin()
    .select(qCard).fetch();
like image 63
Timo Westkämper Avatar answered Sep 22 '22 05:09

Timo Westkämper