# Generated select contains some fields multiple times

**URL:** <https://discourse.hibernate.org/t/generated-select-contains-some-fields-multiple-times/5829>\
**Category:** Hibernate ORM\
**Created:** [November 9, 2021, 3:15pm UTC](https://discourse.hibernate.org/t/generated-select-contains-some-fields-multiple-times/5829 "2021-11-09T15:15:06Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![vramik](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vramik/32/1698_2.png) [@vramik](https://discourse.hibernate.org/u/vramik)\
**Post date:** [November 9, 2021, 3:15pm UTC](https://discourse.hibernate.org/t/generated-select-contains-some-fields-multiple-times/5829/1 "2021-11-09T15:15:06Z")

</div>

Hello,

I’m not very experienced with hibernate. I have two entities Client and Attribute like following ones:

```auto
@Entity
public class Client {
    @Id 
    @Column 
    private UUID id;

    @OneToMany(mappedBy = "client", cascade = CascadeType.PERSIST, orphanRemoval = true)
    private final Set<Attribute> attributes = new HashSet<>();
}

```

```auto
@Entity
public class Attribute {
    @Id 
    @Column 
    @GeneratedValue
    private UUID id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name="fk_client")
    private Client client;

    @Column
    private String name;

    @Nationalized
    @Column
    private String value;
}

```

There is a bidirectional association between the two. It’s managed by child entity.

I was investigating generated queries by hibernate when attributes collection is initialized in the moment when it’s firstly accessed (I’m aware of n+1 queries issue, but in my use-case I’ll need attributes not for all clients and only in few cases, so it seems default lazy loading is appropriate here) and the query looks like:

```auto
select
    attributes0_.fk_client as fk_clien4_1_0_,
    attributes0_.id as id1_1_0_,
    attributes0_.id as id1_1_1_,
    attributes0_.fk_client as fk_clien4_1_1_,
    attributes0_.name as name2_1_1_,
    attributes0_.value as value3_1_1_ 
from
    attribute attributes0_ 
where
    attributes0_.fk_client=?

```

When i was debugging it it turned out that

- `fk_clien4_1_0_` comes from collection key column - `AbstractCollectionPersister.keyColumnNames`
- `id1_1_0_` comes from collection element column - `AbstractCollectionPersister.elementColumnReaderTemplates`
- `id1_1_1_`, `fk_clien4_1_1_`, `name2_1_1_` and `value3_1_1_` comes from `AbstractEntityPersister`

I was wondering if all fields in the select is really necessary. Is that expected behavior? It seems that `fk_client` and `id` is fetched two times unnecessarily.

Is there a way how to instruct the hibernate to select only entity fields and not collection fields? Or the other way around? Or I might have wrongly declared the association?

Any thought would be more than welcome, thank you.

---

<div class="post-metadata">

**Author:** ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)\
**Post date:** [November 11, 2021, 7:19am UTC](https://discourse.hibernate.org/t/generated-select-contains-some-fields-multiple-times/5829/2 "2021-11-11T07:19:04Z")

</div>

> I was wondering if all fields in the select is really necessary. Is that expected behavior? It seems that fk\_client and id is fetched two times unnecessarily.  
> Is there a way how to instruct the hibernate to select only entity fields and not collection fields? Or the other way around? Or I might have wrongly declared the association?  
> Any thought would be more than welcome, thank you.

Hi! Your analysis is correct and that is one of the things that we fixed in Hibernate 6.0.
