Another way to disable enitity directly inside the getter in Hibernate 5.2?

We are currently using Hibernate 5.1 in our application and we have lazy OnyToOne relationships in many places. Inside the getter method, we don't proxy the entities so that the caller / client doesn't worry about spontaneous self-production. This is actually a very nice solution because the non-drinking is done in a central place and there is no risk that the caller forgets the proxy. The caller often makes an object instance or calls to .getClass () mean that it must be unable to work.

This is what such a getter looks like:

@OneToOne(cascade=CascadeType.ALL, orphanRemoval=true, fetch=FetchType.LAZY)
@JoinColumn(name="ContractAgreementId")
public ContractAgreement getContractAgreement() {
    return ORMUtils.initializeAndUnproxy(contractArgreement);
}

      

The initializeAndUnproxy () method basically does this:

Hibernate.initialize(entity);
return (T)((HibernateProxy)entity).getHibernateLazyInitializer().getImplementation();

      

This worked fine until Hibernate 5.1, but when we tried to upgrade to 5.2, we found out that it behaves a little differently than before. If the ALL cascading type is used as in the above example, Hibernate tries to delete the associated object (contractArgreement in the above example) at the end of the session, although there is no update for the object (just loading the parent from the object manager is enough to deal with this issue) ... I assume this is done because Hibernate does not find the same object (its proxy) when the getter is called, but instead finds a different (unsinked object). Therefore, he believes that the object should be removed from the database. I hope you know what I mean. Hibernate thinks that the relationship has been updated (replaced with another object), which is actually not the case; it was simply not blamed.

My question is, is this an intentional behavior in Hibernate 5.2, or should Hibernate (like in 5.1 or earlier) recognize that the object hasn't changed when it detects an unprocessed object instead of a proxy? And if this is intentional behavior, is there another way of doing opacity inside the getter without running into this issue?

Thank you for your help!

+3


source to share





All Articles