Hibernate returns the same object from one request, but in one case it is proxied and in the second case it is not. What for?

Hibernate returns the same object from one request, but in one case it is proxied and in the second case it is not. Why is it sometimes a proxy and sometimes not?

I have a hibernate request:

    String q = "From EntityCustomFields as ecf "
            + "left outer join fetch ecf.customFields "
            + "where ecf.fleetId=:fleetId and ecf.entityType=:et";

    Query query = s.createQuery(q);
    query.setInteger("fleetId", fleetId);
    query.setString("et", et.toString());
    EntityCustomFields res = (EntityCustomFields) query.uniqueResult();

      

in the res variable I get the EntityCustomFields object .

First case: customFields property contains several members with type:    CustomField _ $$ _ jvste27_9f it looks like a proxy object, but in the request "fetch" , and as I understand, hibernate should not proxy as it uses eager fetch. Right?

In the second case, I use a different value for the et parameter and get the elements of the customFields property with type:    CustomDDLField and this is not proxied!

This gets even weirder since I know that the same database object is proxied in the first case, but it is not in the second case.

One detail may be relevant: CustomDDLField extends CustomField

+3


source to share


1 answer


Check out this answer .

The objects you see as proxies have probably already been loaded as proxies earlier in the same persistence context instance, so Hibernate continues to use them until they are preempted or the persistence context is closed ...



This is the desired behavior, as it ensures that the same object instance is used in all cases while the object is being managed.

+1


source







All Articles