Hibernate, writing domain objects to an interface for lazy loading
Why does hibernate require i开发者_如何学编程t's entities to be written to an interface in order for them to work correctly with lazy loading?
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 PersistentSet
to private Set<Foo> set;
(they do it with reflection), but it is not possible to do so for private HashSet<Foo> set;
, because PersistentSet
does not extend HashSet
As for lazy @*ToOne
associations - 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 use getclass()
(which you should not do), it works fine.
精彩评论