Hibernate
Found an interesting feature of hibernate that I wanted to share. The problem is I wanted an instance of one of the subclass and not a proxy.
When you define a mapping for an object like say
And if Account Class itself can have subclasses
When you get an instance of MyClass from hibernate, the under lying account MAY BE an instance of Hibernate generated subclass of account in other words a proxy that delegates all method invocations to a different instance and the type will not be one of the concrete class (i.e PersonAccount or EmployerAccount) but of type Account.
This may not be necessarily a bad thing, but in my situation, I needed a concrete sub class of Account.
To overcome this problem the solution is when you define your mapping do the following
<many-to-one name=“account” column=“ACCOUNT_ID” not-null=“false” cascade=“all” outer-join=“true”/>
Setting outer-join to true, cause hibernate to fetch eagerly even if proxying is enabled (lazy =true)
Comments
Sign in with GitHub to comment. Or just say hi on GitHub.