jpainheritancejakarta-eeone-to-one

JPA Inheritance strategy JOINED - OneToOne relation in child class - why it doesn't work?


I have 3 tables represented by JPA model.

First one:

@Entity
@Table(name = "DECISION")
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class Decision {
    @Id
    private Long id;
}

Next class extends Decision:

@Entity
@Table(name = "SPECIFIC_DECISION")
public class SpecificDecision extends Decision {
    @OneToOne
    @JoinColumn(name = "PERSON_ID")
    private Person person;
}

The last one is Person and it's just simple entity class with person info.

Now when I try to select all decisions from database I get an error with information that the persistence unit is inconsistent with the database schema, because column DECISION.PERSON_ID does not exist, while I do have it in SPECIFIC_DECISION table and that's how I mapped it in JPA model.

What's more interesting if there's no relations in SpecificDecision, only simple NUMBER and VARCHAR fields then everything works fine.

What am I doing wrong?


Solution

  • Looks like there's a solution to my problem. JPA looks for joined columns in base table by default. To change this behaviour an attribute "table" in JoinColumn annotation is needed:

    @Entity
    @Table(name = "SPECIFIC_DECISION")
    public class SpecificDecision extends Decision {
        @OneToOne
        @JoinColumn(table = "SPECIFIC_DECISION", name = "PERSON_ID")
        private Person person;
    }
    

    Problem solved.