Thursday 10 February 2022

Being Glue

Found the following blog on my twitter feeds, and I thought it was important and insightful.

Making a note of it here for future reference.

References

No Idea Blog - Being Glue
https://noidea.dog/glue

Thursday 3 February 2022

Hibernate Proxies and the Visitor Pattern

In short, I had a superclass, and two subclasses, each with their own discriminator.

And for each superclass Hibernate returned, it was very important to verify (because business logic) which one was loaded.

But Hibernate creates proxies of the superclass, so the only way to tell is by making sure the business logic is put into the entity itself.

But what if you cannot do that, or do not want to do that, because dependencies and libraries and all that joy.

It can be done by means of the Visitor pattern1.

Performance

It does take a bit of a performance hit, because the Entity needs to be loaded (during the execution of the Visitor pattern) in order to determine the Discriminator.

In our case this wasn't an issue, as the problem occurred in a many-to-one relationships.

In other cases, the query just loads the specific subclass at once.

References

[1] JBoss Community Archive - Proxy Visitor Pattern
https://developer.jboss.org/docs/DOC-13931