开发者

Hibernate, writing domain objects to an interface for lazy loading

开发者 https://www.devze.com 2023-03-25 04:21 出处:网络
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

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消