Guillaume Toison (
https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=70121%3...
) *created* an issue
Hibernate ORM (
https://hibernate.atlassian.net/browse/HHH?atlOrigin=eyJpIjoiNjk3YzFiY2M5...
) / Improvement (
https://hibernate.atlassian.net/browse/HHH-16729?atlOrigin=eyJpIjoiNjk3Yz...
) HHH-16729 (
https://hibernate.atlassian.net/browse/HHH-16729?atlOrigin=eyJpIjoiNjk3Yz...
) Instantiate a proxy of the correct class for ManyToOne associated to an inheritance
hierarchy (
https://hibernate.atlassian.net/browse/HHH-16729?atlOrigin=eyJpIjoiNjk3Yz...
)
Issue Type: Improvement Assignee: Unassigned Created: 31/May/2023 13:15 PM Priority: Major
Reporter: Guillaume Toison (
https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=70121%3...
)
Consider the following entities (getters, setters and PK omitted):
class Container {
@ManyToOne(fetch = FetchType.LAZY)
@Fetch(FetchMode.SELECT)
Food food;
}
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
class Food {
}
class Cheese extends Food {
}
class Fruit extends Food {
}
When loading a Container object, Hibernate creates an uninitialized proxy for the
associated Food. The class of that proxy extends Food
So tests like container.getFood() instanceof Cheese will return false, even if the
underlying instance might be of that type.
Since Hibernate 6.2 @Proxy(lazy = false) is deprecated. This was one way to get the
correct java class, with the drawback that it effectively disabled lazy-loading.
Assuming that the Container table has a new food_type discriminator column next to the
food_id FK column, Hibernate could use that new information to create a proxy of the
correct class.
This would probably entail modifying the mapping, for instance adding a new
@ManyToOneDiscriminatorColumn on the association:
class Container {
@ManyToOne(fetch = FetchType.LAZY)
@Fetch(FetchMode.SELECT)
@ManyToOneDiscriminatorColumn(name = "food_type")
Food food;
}
An alternative would be that when the discriminator is part of an embedded id, Hibernate
uses it create the correct class.
Another solution would be an user-supplied service (injected in the SessionFactory)
responsible for locating the correct type, based on the ID.
This is a continuation of the discussion started here:
https://discourse.hibernate.org/t/proxy-deprecated-in-hibernate-6-2/7707
(
https://hibernate.atlassian.net/browse/HHH-16729#add-comment?atlOrigin=ey...
) Add Comment (
https://hibernate.atlassian.net/browse/HHH-16729#add-comment?atlOrigin=ey...
)
Get Jira notifications on your phone! Download the Jira Cloud app for Android (
https://play.google.com/store/apps/details?id=com.atlassian.android.jira....
) or iOS (
https://itunes.apple.com/app/apple-store/id1006972087?pt=696495&ct=Em...
) This message was sent by Atlassian Jira (v1001.0.0-SNAPSHOT#100225- sha1:a994ca2 )