Hibernate @ManyToOne (fetch = FetchType.LAZY) is ignored

I have a lazy @ManyToOne buy column defined as:

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

However, when I load an object 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 if the field is not loaded yet.

What am I doing wrong?

(Hibernate 4.3.11.Final)

+5
source share
1 answer

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

+1
source

Source: https://habr.com/ru/post/1271179/


All Articles