# @ManyToOne Lazy fetch works on select, but is treated as Eager for update

**URL:** <https://discourse.hibernate.org/t/manytoone-lazy-fetch-works-on-select-but-is-treated-as-eager-for-update/5394>\
**Category:** Hibernate ORM\
**Created:** [May 26, 2021, 3:36am UTC](https://discourse.hibernate.org/t/manytoone-lazy-fetch-works-on-select-but-is-treated-as-eager-for-update/5394 "2021-05-26T03:36:47Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andrius\_Karpavicius](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/andrius_karpavicius/32/1538_2.png) [@Andrius\_Karpavicius](https://discourse.hibernate.org/u/Andrius_Karpavicius)\
**Post date:** [May 26, 2021, 3:36am UTC](https://discourse.hibernate.org/t/manytoone-lazy-fetch-works-on-select-but-is-treated-as-eager-for-update/5394/1 "2021-05-26T03:36:47Z")

</div>

Hi,

Wildlfy v18 with hibernate 5.3.

“@ManyToOne with fetch=Lazy” works as expected on select - only one select query is generated - related entities are NOT loaded.

But on any value change in main entity, uppon commit I see multiple quiries to retrieve related entities.

I have a JPA entity A and related entities B and C:

```auto
@Entity
public class A {

    @Column(name = "alias", length = 255)
    private String alias;

    @ManyToOne(fetch = FetchType.LAZY, optional = true)
    @JoinColumn(name = "b_id")
    private B b;

    @ManyToOne(fetch = FetchType.LAZY, optional = true)
    @JoinColumn(name = "c_id")
    private C c;
}

@Entity
public class B {
}

@Entity
public class C {
}

```

A find by ID works as expected - only main entity is loaded.

However a value update in main entity will trigger an update query (as expected) and will load all other @ManyToOne relationships.

Code:

```auto
A a = em.find(A.class, 15L);
a.setAlias("test");

```

will result in:

```auto
select from A where id=15 (the "em.find" line)
update A set ..
select from B where id=..
select from C where id=..

```

I dont call merge - EM handles update by itself. No caching enabled. No relaetd entity is accessed from code.

If I add a “hibernate.jpa.compliance.proxy=false” in persistence.xml, I won’t see any related entity lookup upon update:

```auto
select from A where id=15 (the "em.find" line)
update A set ..

```

**Question** - why related entities are being loaded after update? Is this what JPA specification requires to do? Any other setting to use?

Thanks

---

<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:** [May 31, 2021, 8:10am UTC](https://discourse.hibernate.org/t/manytoone-lazy-fetch-works-on-select-but-is-treated-as-eager-for-update/5394/2 "2021-05-31T08:10:28Z")

</div>

I think this might be related to the following: [HHH-14608 Merge causes StackOverflow when JPA proxy compliance is enabled by dreab8 · Pull Request #3997 · hibernate/hibernate-orm · GitHub](https://github.com/hibernate/hibernate-orm/pull/3997)

Could you try to checkout + build that PR and retest against that version?
