简体   繁体   中英

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)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM