Mapping Hibernate entity for unknown DiscriminatorValue for InheritanceType.SINGLE_TABLE

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.