简体   繁体   中英

QueryDSL, Hibernate, JPA — using .fetchJoin() and getting data in first SELECT, so why N+1 queries after?

I'm trying to query for a list of entities ( MyOrder s) that have mappings to a few simple sub-entities: each MyOrder is associated with exactly one Store , zero or more Transaction s, and at most one Tender . The generated SELECT appears correct - it retrieves all the columns from all four joined tables - but afterwards, two more SELECTs are executed for each MyOrder , one for Transaction s and one for Tender .

I'm using QueryDSL 4.1.3, Spring Data 1.12, JPA 2.1, and Hibernate 5.2.

In QueryDSL, my query is:

... = new JPAQuery<MyOrder>(entityManager)
    .from(qMyOrder)
    .where(predicates)
    .join(qMyOrder.store).fetchJoin()
    .leftJoin(qMyOrder.transactions).fetchJoin()
    .leftJoin(qMyOrder.tender).fetchJoin()
    .orderBy(qMyOrder.orderId.asc())
    .transform(GroupBy
        .groupBy(qMyOrder.orderId)
        .list(qMyOrder));

which is executed as:

SELECT myorder0_.ord_id AS col_0_0_,
    myorder0_.ord_id AS col_1_0_,
    store1_.sto_id AS sto_id1_56_1_, -- store's PK
    transactions3_.trn_no AS trn_no1_61_2_, -- transaction's PK
    tender4_.tender_id AS pos_trn_1_48_3_, -- tender's PK
    myorder0_.ord_id AS ord_id1_39_0_,
    myorder0_.app_name AS app_name3_39_0_, -- {app_name, ord_num} is unique
    myorder0_.ord_num AS ord_num8_39_0_,
    myorder0_.sto_id AS sto_id17_39_0_,
    store1_.division_num AS div_nu2_56_1_,
    store1_.store_num AS store_nu29_56_1_,
    transactions3_.trn_cd AS trn_cd18_61_2_,
    tx2myOrder2_.app_name AS app_name3_7_0__, -- join table
    tx2myOrder2_.ord_no AS ord_no6_7_0__,
    tx2myOrder2_.trn_no AS trn_no1_7_0__,
    tender4_.app_name AS app_name2_48_3_,
    tender4_.ord_num AS ord_num5_48_3_,
    tender4_.tender_cd AS tender_cd_7_48_3_,
FROM data.MY_ORDER myorder0_
INNER JOIN data.STORE store1_ ON myorder0_.sto_id=store1_.sto_id
LEFT OUTER JOIN data.TX_to_MY_ORDER tx2myOrder2_
    ON myorder0_.app_name=tx2myOrder2_.app_name
    AND myorder0_.ord_num=tx2myOrder2_.ord_no
LEFT OUTER JOIN data.TRANSACTION transactions3_ ON tx2myOrder2_.trn_no=transactions3_.trn_no
LEFT OUTER JOIN data.TENDER tender4_
    ON myorder0_.app_name=tender4_.app_name
    AND myorder0_.ord_num=tender4_.ord_num
ORDER BY myorder0_.ord_id ASC

which is pretty much what I'd expect. (I cut out most of the data columns for brevity, but everything I need is SELECTed.)

When querying an in-memory H2 database (set up with Spring's @DataJpaTest annotation), after this query executes, a second query is made against the Tender table, but not Transaction . When querying a MS SQL database, the initial query is identical, but additional queries happen against both Tender and Transaction . Neither makes additional calls to load Store .

All the sources I've found suggest that the .fetchJoin() should be sufficient (such as Opinionated JPA with Query DSL ; scroll up a few lines from the anchor) and indeed if I remove them, the initial query only selects columns from MY_ORDER. So it appears that .fetchJoin() does force generation of a query that fetches all the side tables in one go, but for some reason that extra information isn't being used. What's really weird is that I do see the Transaction data being attached in my H2 quasi-unit test without a second query (if and only if I use .fetchJoin() ) but not when using MS SQL.

I've tried annotating the entity mappings with @Fetch(FetchMode.JOIN) , but the secondary queries still fire. I suspect there might be a solution involving extending CrudRepository<> , but I've had no success getting even the initial query correct there.

My primary entity mapping, using Lombok's @Data annotations, other fields trimmed out for brevity. ( Store , Transaction , and Tender all have an @Id a handful of simple numeric and string field-column mappings, no @Formula s or @OneToOne s or anything else.)

@Data
@NoArgsConstructor
@Entity
@Immutable
@Table(name = "MY_ORDER", schema = "Data")
public class MyOrder implements Serializable {

@Id
@Column(name = "ORD_ID")
private Integer orderId;

@NonNull
@Column(name = "APP_NAME")
private String appName;
@NonNull
@Column(name = "ORD_NUM")
private String orderNumber;

@ManyToOne
@JoinColumn(name = "STO_ID")
private Store store;

@OneToOne
@JoinColumns({
        @JoinColumn(name = "APP_NAME", referencedColumnName = "APP_NAME", insertable = false, updatable = false),
        @JoinColumn(name = "ORD_NUM", referencedColumnName = "ORD_NUM", insertable = false, updatable = false)})
@org.hibernate.annotations.ForeignKey(name = "none")
private Tender tender;

@OneToMany
@JoinTable(
        name = "TX_to_MY_ORDER", schema = "Data",
        joinColumns = { // note X_to_MY_ORDER.ORD_NO vs. ORD_NUM
                @JoinColumn(name = "APP_NAM", referencedColumnName = "APP_NAM", insertable = false, updatable = false),
                @JoinColumn(name = "ORD_NO", referencedColumnName = "ORD_NUM", insertable = false, updatable = false)},
        inverseJoinColumns = {@JoinColumn(name = "TRN_NO", insertable = false, updatable = false)})
@org.hibernate.annotations.ForeignKey(name = "none")
private Set<Transaction> transactions;

/**
 * Because APP_NAM and ORD_NUM are not foreign keys to TX_TO_MY_ORDER (and they shouldn't be),
 * Hibernate 5.x saves this toString() as the 'owner' key of the transactions collection such that
 * it then appears in the transactions collection's own .toString(). Lombok's default generated
 * toString() includes this.getTransactions().toString(), which causes an infinite recursive loop.
 * @return a string that is unique per order
 */
@Override
public String toString() {
    // use appName + orderNumber since, as they are the columns used in the join, they must (?) have
    // already been set when attaching the transactions - primary key sometimes isn't set yet.
    return this.appName + "\00" + this.orderNumber;
}
}

My question is: why am I getting redundant SELECTs, and how can I not do that?

I'm a little too late on the answer, but today the same problem happened to me. This response might not help you, but at least it would save someone the headache we went through.

The problem is on the relations between the entities, not in the query. I tried with QueryDSL, JPQL, and even native SQL but the problem was always the same.

The solution was to trick JPA into believing that the relations were there via annotating the child classes with @Id on those joined fields.

Basically you'll need to set Tender 's id like this and use it from MyOrder like if it was a normal relationship.

public class Tender {
    @EmbeddedId
    private TenderId id;
}

@Embeddable
public class TenderId {
    @Column(name = "APP_NAME")
    private String appName;

    @Column(name = "ORD_NUM")
    private String orderNumber;
}

The same would go for the Transaction entity.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM