| Thanks for taking the time to reply and offer suggestions. The warning would simply be helpful to let me know that the eventual ClassCastException is due to my usage pattern, not my mapping. I understand that it's ideal for getReference to not run a query. Ordinarily it's not an issue, however with @Inheritance there are some subtle problems. The current situation causes a difficult-to-understand ClassCastException, which may lead some developers (read: me) to suspect a problem with their @Inheritance scheme when, in fact, it's a problem with the pattern of usage ... it's just not immediately obvious from the place in code where the error occurs, since the getReference() may have been called long before the find() where the error eventually shows up. Just recapping the potential solutions that come to mind (maybe adding one or two):
- Have getReference() make a minimal query when an entity is marked with @Inheritance – not ideal since we'd like to avoid a query
- Have find(), which ordinarily would make a DB call,determine that it's about to return a proxy of the wrong type and emit a warning – may be complicated to implement?
- Have getReference() emit a warning in the case it's returns an Entity marked with @Inheritance – not ideal, since we'd be warned about cases where we don't care about the type.
- Do #1 only optionally according to some setting – this would only help people who know about the setting
- Find a way to dynamically change the runtime interfaces implemented by the proxy – this may not be possible, I haven't found a way to do it but don't have a lot of experience with manipulating proxies
- In the case of an Entity with @Inheritance where a proxy is already in the session, have find() create a new proxy, proxying the original proxy, but with a correct type – this may violate the JPA spec
Maybe some others on the Hibernate dev team may have thoughts on the subject? For now, I'll do my best to avoid using getReference() on entities with @Inheritance and I have asked the people I work with to do the same. |