Vojtěch
Vojtěch

Reputation: 12416

Hibernate @ManyToOne(fetch = FetchType.LAZY) ignored

I have a lazy @ManyToOne column on Purchase defined as:

@ManyToOne(fetch = FetchType.LAZY)
Sale sale = null

However, when I load the entity from db, it gets directly loaded:

Purchase purchase = em.find(Purchase.class, id);
PersistenceUnitUtil unitUtil = em
        .getEntityManagerFactory()
        .getPersistenceUnitUtil();
System.err.println(unitUtil.isLoaded(purchase, "sale"));

This will return true even though the field should not yet be loaded.

What am I doing wrong?

(Hibernate 4.3.11.Final)

Upvotes: 3

Views: 1065

Answers (1)

kan
kan

Reputation: 28951

Because it is nullable field. Hibernate cannot know if the value exists in the db or not, so it must query db to assign null or a value to the field.

Upvotes: 3

Related Questions