I have a classic Hibernate @Inheritance(strategy=InheritanceType.SINGLE_TABLE) with @DiscriminatorFormula. It works fine. However, there are about 500 different values for the @DiscriminatorValue in the database and I need map just about 30 of them to Java classes (children) and the rest of them to map to the parent Java class.
The problem can be modelled as an example inheritance on Animal class.
@Entity
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorFormula("...")
public class Animal implements Serializable {
...
@Column
public String getName() { ... }
}
So I have have about 30 subclasses of Animal defined in the Java code with @DiscriminatorValue. When Hibernate founds unknown value for the discriminator, then it throws WrongClassException. However, I need to map these unknown discriminator values to one entity, the best it the Animal class. (I need to use only the method getName() in such cases.)
I know one solution is to put a SQL CASE into the @DiscriminatorFormula but then I have to state there all 30 known discriminator values (plus more when I will need to add others). So I am looking for more flexible solution.
P.S. It is a legacy code, so I cannot change the model.
forceDefault=trueoption (map to root class instead of exception) for the@DiscriminatorColumnor@DiscriminatorFormulaannotation would be nice to have. Is the@DiscriminatorValueon the root class also missing in your working code or just in the example? If no, maybe you can force the default mapping this way. Cannot try it out right now but I doubt it, if@DiscriminatorColumnis used a default value is generated for a missing@DiscriminatorValue.@DiscriminatorValuehas been missing in the code and in the example, too. And this little detail persuaded me to dig into the Hibernate code once again and find the solution. See bellow.