@JoinColumnsOrFormulas导致n + 1问题

时间:2019-08-29 12:24:03

标签: java spring-boot spring-data-jpa

当我将@JoinColumnsOrFormulas或@JoinColumns与@NamedEntityGraph(includeAllAttributes = true)一起使用时,我面临n + 1查询问题

我也尝试在规范中保留该实体的加入,这也导致n + 1查询问题。

实体:

@Data
@Entity
@Table(name = "book")
@NamedEntityGraph(name = "all", includeAllAttributes = true)
public class BookEntity implements JpaEntity {

    @ManyToOne(targetEntity = CustomerEntity.class)
    @JoinColumnsOrFormulas({
            @JoinColumnOrFormula(column = @JoinColumn(name = "customer", referencedColumnName = "number", insertable = false, updatable = false)),
            @JoinColumnOrFormula(column = @JoinColumn(name = "sub_customer", referencedColumnName = "sub_number", insertable = false, updatable = false)),
            @JoinColumnOrFormula(formula = @JoinFormula(value = "'2019'", referencedColumnName = "year"))
    })
    private CustomerEntity customerEntity;
}
@Entity
@Data
@Table(name = "customer")
public class CustomerEntity implements JpaEntity {

    @Id
    @Column(name = "id")
    private Integer id;
    @Column(name = "number", columnDefinition = "INTEGER")
    private Integer number;
    @Column(name = "sub_number", columnDefinition = "INTEGER")
    private Integer subNumber;
    @Column(name = "year")
    private String year;
}

存储库:

@Override
    @EntityGraph(value = "all")
    Page<BookEntity> findAll(Specification<BookEntity> spec, Pageable pageable);

我看到查询是正确的。我看到了第一个查询的联接,因此看起来我有完整的实体,不需要进行n + 1查询。但是在正确查询之后,我看到了CustomerEntity的其他选择。

有人可以帮助我避免这种情况下的n + 1问题吗?

0 个答案:

没有答案