I am facing the n+1 query issue when I use @JoinColumnsOrFormulas or @JoinColumns with @NamedEntityGraph(includeAllAttributes = true)
I tried to also left join the entity in Specification and it also cause n+1 query issue.
Entity:
@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;
}
Repository:
@Override
@EntityGraph(value = "all")
Page<BookEntity> findAll(Specification<BookEntity> spec, Pageable pageable);
I see that the query is correct. I see the join for the first query, so it looks like I have the full entity and no need to make n+1 query. But after correct query I see additional selects for the CustomerEntity.
Can someone help me to avoid n+1 issue with this case? Maybe it is a bug.