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
No comments:
Post a Comment