Note that EclipseLink goes one step further and merges fetched state through e.g. a JOIN FETCH query into existing objects which is what
HHH-9845 Open is about. Interestingly, when bytecode enhancement is enabled getReference() for a polymorphic entity type returns the super/declared type instance whereas without bytecode enhancement, EclipseLink actually fetches and returns the subtype instance. So using getReference() with the supertype and then having a query that fetches the entity will result in an exception in EclipseLink because it fails to merge the fetched state into the instance due to expecting it to be of the correct subtype. So the question remains, what should be returned when using getReference()? Should we also query the DBMS for polymorphic types or just return an instance of the given type? If we don't query the DBMS which is IMO better, we have to decide what happens when state gets merged back into that instance. Should it fail if the object is not of the appropriate type like in EclipseLink? Or should it just merge the state which is applicable for the type? |