Why is @DiscriminatorColumn ignored when using @JoinTable?

Viewed 1975

Suppose I have the following tables:

 ______________________
|       LAWSUIT        |
|______________________|
| ID                   |
| TITLE                |
|______________________|
                        \
                         \
                          \
                             ______________________
                            | PERSONS_IN_THE_CASE  |
                            |______________________|
                            | ID_LAWSUIT           |
                            | ID_PERSON            |
                            |______________________|
                          /
                         /
                        /
 ______________________
|        PERSON        |
|______________________|
| ID                   |
| NAME                 |
| TYPE                 | TYPE values = "Plantiff, Defendant, Lawyer, Judge, ..."
|______________________|

(I know that normalizing the database I could have a table for each person type, but let's stick to the actual structure)

I've subclassed the different Persons with JPA (2.0) as follows:

@Entity
@Table(name="PERSON")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "TYPE")
public abstract class Person {
@Entity
@DiscriminatorValue("Plantiff")
public class Plantiff extends Person {  
@Entity
@DiscriminatorValue("Defendant")
public class Defendant extends Person { 
@Entity
@DiscriminatorValue("Lawyer")
public class Lawyer extends Person {    

This works correctly, because I can query a single category and the filter is automatic, eg findAll on Plantiff will get all the Plantiffs of all times.

Now I'm trying to link them to the Lawsuit through the PERSONS_IN_THE_CASE @JoinTable:

@Entity
@Table(name="LAWSUIT")
public class Lawsuit {

    @Id
    Long id;

    String title;

    @OneToMany
    @JoinTable(name="PERSONS_IN_THE_CASE",
               joinColumns=@JoinColumn(name="ID_LAWSUIT", referencedColumnName="ID"),
        inverseJoinColumns=@JoinColumn(name="ID_PERSON",  referencedColumnName="ID"))
    Plantiff plantiff;


    @ManyToMany
    @JoinTable(name="PERSONS_IN_THE_CASE",
               joinColumns=@JoinColumn(name="ID_LAWSUIT", referencedColumnName="ID"),
        inverseJoinColumns=@JoinColumn(name="ID_PERSON",  referencedColumnName="ID"))
    Set<Defendant> defendants;

    ...
}

Here is where things break: the @DiscriminatorValue is not applied, it seems to be completely bypassed by the @JoinTable.

In fact, the Set<Defendant> does not contain only the defendants, but every person in the case (every record in the PERSONS_IN_THE_CASE relational table. For example, if I have a Plantiff, a Judge, two Defendants and two Lawyers, the above Set<Defendant> will contain 6 persons instead of 2).

How can I make the @DiscriminatorValue working through the @JoinTable binding ?

EDIT: I'm using Hibernate 3.6.6.Final, and (although I always try to avoid it) I'm open to vendor-specific solutions, like @ForceDiscriminator(deprecated), @DiscriminatorOptions(force=true) and so on. I've obviosuly tried before asking (without being able to make it work, though).

3 Answers

it is still ignored in hibernate 5.4.2, our work around is using the @Where annotation from hibernate, still not optimal

Related