Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate Subclass which has multiple discriminator values

Tags:

hibernate

Hello I have a Subclass which needs to cover multiple Discriminator Values.

something like:

@DiscriminatorValue(value = "ACT","DNR","ATT" )

would do me perfect.

we have existing data where several discriminators can be mapped to one class (as they are similar types of what our system will consider the same thing)

like image 811
Shaun Stone Avatar asked Mar 10 '10 00:03

Shaun Stone


People also ask

What is discriminator value in Hibernate?

Annotation Type DiscriminatorValueSpecifies the value of the discriminator column for entities of the given type. The DiscriminatorValue annotation can only be specified on a concrete entity class.

Which of these inheritance models is not supported by Hibernate?

However, Hibernate does not support mixing <subclass> , <joined-subclass> and <union-subclass> mappings under the same root <class> element.

Which of the following JPA inheritance strategies require discriminator column?

Only SINGLE_TABLE inheritance hierarchies require a discriminator column and values. JOINED hierarchies can use a discriminator to make some operations more efficient, but do not require one. TABLE_PER_CLASS hierarchies have no use for a discriminator.


2 Answers

You can use DiscriminatorFormula:

// Base class
@DiscriminatorFormula("case when value in ('ACT','DNR','ATT') then 1 
   when 'OTH' then 2 else 3 end")

// Subclass 
@DiscriminatorValue("1") // maps to ACT, DNR, ATT
like image 135
Brian Deterling Avatar answered Sep 23 '22 19:09

Brian Deterling


A subclass has exactly 1 discriminator value.

You can add additional subclasses under the existing subclass for the extra discriminator values. Subclasses need not have additional properties or behavior.

like image 39
Lachlan Roche Avatar answered Sep 21 '22 19:09

Lachlan Roche