Display hibernation interface without using the "targetEntity" annotation parameter

Recently, I am trying to introduce interfaces for some of my hibernation-related objects and cannot figure out how to configure the mapping.

When I used the interface without any further declarations, I always got the following exception:

org.hibernate.MappingException: Could not determine type for: ...MyInterface 

Then I found out that everything works fine when I define targetEntity explicitly:

  @OneToOne(targetEntity=InterfaceImpl.class) private MyInterface myInterface; 

Unfortunately, this solution does not work in my case: I cannot determine targetEntity via annotation, because I want to extract this class into a shared external library that will not contain or even know the final implementation of the interface.

So, is there an alternative way to declare which implementation should be used that I could use outside the extracted library? enter image description here

+7
source share
2 answers

Define your mapping contract in the abstract @MappedSupperclass , and then override it with @AssociationOverride in the implementation class.

+1
source

I'm not sure how working with hibernation annotations works, but I did something similar described here

See the latest post for a fairly detailed explanation.

The basic need was similar in the sense that we specified the interfaces in the hibernation mapping files, and the implementations were classes created mainly at runtime with javassist (they were nonexistent compilation times).

What you need is code that can resolve at runtime the implementation class from the interface and vice versa and use the tuplizer / instantiator hibernate contracts as an extension point to allow hibernation

(a) create instances of the implementation when it is necessary to populate the entity from the database; and (b) find the metadata of the sleep mode mapping when it needs to save the object (implementation instance) in the database.

+1
source

All Articles