Why does hibernate require it’s entities to be written to an interface in order for them to work correctly with lazy loading?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The only scenario where hibernate requires interfaces is collections. It’s because hibernate uses its own collection implementations for lazy loading –
PersistentBag,PersistentSet, etc. and assigns them to your fields. The implementations hold a reference to the session so that they can fill their data whenever required.Hibernate can assign
PersistentSettoprivate Set<Foo> set;(they do it with reflection), but it is not possible to do so forprivate HashSet<Foo> set;, becausePersistentSetdoes not extendHashSetAs for lazy
@*ToOneassociations – hibernate creates a proxy object, using javassist (or cglib), and it does not require an interface. This is a rarely used feature anyway, and the proxy is a subclass of the real object, so unless you usegetclass()(which you should not do), it works fine.