# @JoinColumnsOrFormulas cause n+1 issue

**URL:** https://discourse.hibernate.org/t/joincolumnsorformulas-cause-n-1-issue/3169
**Category:** Hibernate ORM
**Created:** [August 30, 2019, 6:06am UTC](https://discourse.hibernate.org/t/joincolumnsorformulas-cause-n-1-issue/3169 "2019-08-30T06:06:02Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![illiakarpenko](https://avatars.discourse-cdn.com/v4/letter/i/fbc32d/32.png) [@illiakarpenko](https://discourse.hibernate.org/u/illiakarpenko)
#### Post date: [August 30, 2019, 6:06am UTC](https://discourse.hibernate.org/t/joincolumnsorformulas-cause-n-1-issue/3169/1 "2019-08-30T06:06:02Z")

</div>

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:

```auto
@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:

```auto
@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.

---

<div class="post-metadata">

### Author: ![slavick](https://avatars.discourse-cdn.com/v4/letter/s/9fc348/32.png) [@slavick](https://discourse.hibernate.org/u/slavick)
#### Post date: [September 2, 2019, 5:28pm UTC](https://discourse.hibernate.org/t/joincolumnsorformulas-cause-n-1-issue/3169/2 "2019-09-02T17:28:36Z")

</div>

Maybe entity graph annotation does not work on spring data jpa methods with specification and pageable.  
Try to use [https://github.com/Cosium/spring-data-jpa-entity-graph](https://github.com/Cosium/spring-data-jpa-entity-graph)

---

<div class="post-metadata">

### Author: ![illiakarpenko](https://avatars.discourse-cdn.com/v4/letter/i/fbc32d/32.png) [@illiakarpenko](https://discourse.hibernate.org/u/illiakarpenko)
#### Post date: [September 2, 2019, 5:37pm UTC](https://discourse.hibernate.org/t/joincolumnsorformulas-cause-n-1-issue/3169/3 "2019-09-02T17:37:40Z")

</div>

Thank you, I will have a look!
