# @OneToOne with FetchType.LAZY doesn't work with Hibernate

**URL:** <https://discourse.hibernate.org/t/onetoone-with-fetchtype-lazy-doesnt-work-with-hibernate/2039>\
**Category:** Hibernate ORM\
**Created:** [January 14, 2019, 4:56pm UTC](https://discourse.hibernate.org/t/onetoone-with-fetchtype-lazy-doesnt-work-with-hibernate/2039 "2019-01-14T16:56:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sergio\_Perez](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/sergio_perez/32/1248_2.png) [@Sergio\_Perez](https://discourse.hibernate.org/u/Sergio_Perez)\
**Post date:** [January 14, 2019, 4:56pm UTC](https://discourse.hibernate.org/t/onetoone-with-fetchtype-lazy-doesnt-work-with-hibernate/2039/1 "2019-01-14T16:56:35Z")

</div>

I have an entity with @OneToOne relation between entities country -\> language.

**EntityCountry relation with language works as FetchType EAGER (default):**

```auto
@JoinColumn(name = "language_id", updatable = false, insertable = false)
@OneToOne
private LanguageEntity language;

```

But I don’t want EAGER, so I try LAZY, this both configurations work:  
**This two, although it is EAGER, throws a query during retrieveing the information to get the language:**

```auto
    @JoinColumn(name = "language_id", updatable = false, insertable = false)
    @Fetch(FetchMode.SELECT)
    @OneToOne(fetch = FetchType.EAGER)
    private LanguageEntity language;

```

```auto
    @JoinColumn(name = "language_id", updatable = false, insertable = false)
    @Fetch(FetchMode.JOIN)
    @OneToOne(fetch = FetchType.LAZY)
    private LanguageEntity language;

```

Anyway, I want the easiest configuration and this should work (neither of this two work), and I don’t know why is not working ( **just default LAZY in a @OneToOne relation** ):

```auto
    @JoinColumn(name = "language_id", updatable = false, insertable = false)
    @OneToOne(fetch = FetchType.LAZY)
    private LanguageEntity language;

```

```auto
    @JoinColumn(name = "language_id", updatable = false, insertable = false)     
    @Fetch(FetchMode.SELECT)
    @OneToOne(fetch = FetchType.LAZY)
    private LanguageEntity language;

```

**I’m using Hibernate 5.2.17 and elide io 4.2.8**

---

<div class="post-metadata">

**Author:** ![vlad](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vlad/32/658_2.png) [@vlad](https://discourse.hibernate.org/u/vlad)\
**Post date:** [January 14, 2019, 6:12pm UTC](https://discourse.hibernate.org/t/onetoone-with-fetchtype-lazy-doesnt-work-with-hibernate/2039/2 "2019-01-14T18:12:38Z")

</div>

You don’t need to combine the `@Fetch` annotation with the `fetch` attribute.

So, this is enough:

```
@JoinColumn(name = "language_id", updatable = false, insertable = false)
@OneToOne(fetch = FetchType.LAZY)
private LanguageEntity language;

```

Anyway, using [EAGER is a bad idea](https://vladmihalcea.com/eager-fetching-is-a-code-smell/) since it can easily lead to [N+1](https://vladmihalcea.com/how-to-detect-the-n-plus-one-query-problem-during-testing/) query issues if you forget to join fetch all EAGER attributes during fetching.

---

<div class="post-metadata">

**Author:** ![Sergio\_Perez](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/sergio_perez/32/1248_2.png) [@Sergio\_Perez](https://discourse.hibernate.org/u/Sergio_Perez)\
**Post date:** [January 14, 2019, 6:59pm UTC](https://discourse.hibernate.org/t/onetoone-with-fetchtype-lazy-doesnt-work-with-hibernate/2039/3 "2019-01-14T18:59:29Z")

</div>

My problem is that the example that you tell me doesn’t work (I don’t know where is the problem… what configuration can modify this behaviour? Your example doesn’t throw the SQL statement).

To see the SQL statement during fetching on that onetoone relation, I can do only this two:

```auto
    @JoinColumn(name = "language_id", updatable = false, insertable = false)
    @Fetch(FetchMode.SELECT)
    @OneToOne(fetch = FetchType.EAGER)
    private LanguageEntity language;

```

```auto
    @JoinColumn(name = "language_id", updatable = false, insertable = false)
    @Fetch(FetchMode.JOIN)
    @OneToOne(fetch = FetchType.LAZY)
    private LanguageEntity language;

```

I don’t know what can be happening to make your example work.  
Anyway I don’t know which of this two is better to use.

---

<div class="post-metadata">

**Author:** ![vlad](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vlad/32/658_2.png) [@vlad](https://discourse.hibernate.org/u/vlad)\
**Post date:** [January 14, 2019, 7:13pm UTC](https://discourse.hibernate.org/t/onetoone-with-fetchtype-lazy-doesnt-work-with-hibernate/2039/4 "2019-01-14T19:13:02Z")

</div>

You can find plenty of examples in the Hibernate documentation folder.

Only via logging, you can see that lazy loading works. If you assess it during debugging in the IDE, the IDE will trigger the load during property inspection, hence it will mislead you.

---

<div class="post-metadata">

**Author:** ![Sergio\_Perez](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/sergio_perez/32/1248_2.png) [@Sergio\_Perez](https://discourse.hibernate.org/u/Sergio_Perez)\
**Post date:** [January 14, 2019, 8:24pm UTC](https://discourse.hibernate.org/t/onetoone-with-fetchtype-lazy-doesnt-work-with-hibernate/2039/5 "2019-01-14T20:24:05Z")

</div>

Thank you vlad, anyway do you know if some configuration can do what my @OneToOne is doing?

---

<div class="post-metadata">

**Author:** ![vlad](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vlad/32/658_2.png) [@vlad](https://discourse.hibernate.org/u/vlad)\
**Post date:** [January 14, 2019, 8:51pm UTC](https://discourse.hibernate.org/t/onetoone-with-fetchtype-lazy-doesnt-work-with-hibernate/2039/6 "2019-01-14T20:51:17Z")

</div>

If you provide a replicating test case, I can tell you then.
