質問

i have many-To-One relationship.. with all lazy initialization but everytime i query the many side of the relationship this create another select[other one] and brings the parent, i have read on other post here in stack the say is good for performance declare the class final for a VTable overriding methods but in other post they say if i have many-to-one relationship with a final class the will bring the one side of the relationship in fact i have removed the final modifier in my class and everything is OK now my question is why final modifier causes the one side of the relationship eagerly fetched even with all lazy initialization thanks..

役に立ちましたか?

解決

The final keyword prevents anyone from extending the class and override any of its methods.

To implement lazy-loading, hibernate replaces the instance of the parent object at the many side by a proxy. The first time it's called, this proxy loads the data of the parent from the database.

The proxy must be an instance of the Parent class. But it can't be of type Parent, since its methods need to do something else: execute a SQL query the first time they're called. So the proxy is in fact a subclass (dynamically generated at runtime) of Parent. And to be a subclass, the Parent class can't be final.

So final must be avoided on hibernate entities, else proxies and thus lazy-loading won't work.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top