2

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)

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

So what could I do to prevent this?
Not so many options as I know. One way is "buildtime bytecode instrumentation". docs.jboss.org/hibernate/orm/3.3/reference/en/html/…
So why this function is even there when it cannot never work? I even tried nullable = false with no effect.
@Vojtěch The function works in some cicrumstances, e.g. when you are using the instrumentation. But it is not guaranteed, just "best effort".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.