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.
tscho has pointed me in the right direction, so I was able to find a solution for my case. The @DiscriminatorValue
evaluates special values @DiscriminatorValue("null")
and @DiscriminatorValue("not null")
. The second one is the right for me.
@Entity
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorFormula("...")
@DiscriminatorValue("not null")
public class Animal implements Serializable {
...
@Column
public String getName() { ... }
}
Good news. I have just documented this behavior in the new User Guide. This is the JIRA issue.
Basically, you have two options:
@DiscriminatorValue("not null")
as a fall-back strategy when there is no explicit discriminator value mapping matching the underlying database column value.@DiscriminatorValue("null")
for when the database column value is null. Usually, you'd use this for the base class.There's a detailed example on the Hibernate blog as well.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With