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

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:

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

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

will result in:

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:

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

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

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