Search code examples
javainheritancejpajpa-2.0eclipselink

Invalid insert order when use @Inheritance JPA attribute


I have Eclipselink persistence provider tuned on DB2 DB. Where is 3 tables which simplified definition are listed below:

CREATE TABLE root
(
    id       CHAR(32) NOT NULL PRIMARY KEY,
    rec_type VARCHAR(20)
);

CREATE TABLE derived
(
    id CHAR(32) NOT NULL PRIMARY KEY,
    ...
);
ALTER TABLE derived ADD CONSTRAINT fk_derived_to_root FOREIGN KEY (id) REFERENCES root(id);

CREATE TABLE secondary
(
    derived_id NOT NULL PRIMARY KEY,
    ...
);
ALTER TABLE secondary ADD CONSTRAINT fk_secondary_to_derived FOREIGN KEY (derived_id) REFERENCES derived(id);

Java entity classes for these entities are listed below, RootEntity:

@javax.persistence.Table(name = "ROOT")
@Entity
@DiscriminatorColumn(name = "REC_TYPE")
@Inheritance(strategy = InheritanceType.JOINED)
public class RootEntity {
    private String id;

    @javax.persistence.Column(name = "ID")
    @Id
    @GeneratedValue(generator = "system-uuid")
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    private String principalType;
    @Column(name = "PRINCIPAL_TYPE")
    public String getPrincipalType() {
        return principalType;
    }

    public void setPrincipalType(String principalType) {
        this.principalType = principalType;
    }
   ...
}

DerivedEntity:

@javax.persistence.Table(name = "DERIVED")
@Entity
@DescriminatorValue("DERIVED")
public class DerivedEntity extends RootEntity {
    private SecondaryEntity secondaryEntity;

    @OneToOne(mappedBy = "derived_id")
    public SecondaryEntity getSecondaryEntity() {
        return secondaryEntity;
    }

    public void setSecondaryEntity(SecondaryEntity secondaryEntity) {
        this.secondaryEntity = secondaryEntity;
    }
 ...
}

I see no derived table insertion in the test logs:

--INSERT INTO ROOT (ID, REC_TYPE) VALUES (?, ?)
    bind => [241153d01c204ed79109ce658c066f4c, Derived]
--INSERT INTO SECONDARY (DERIVED_ID, ...) VALUES (?, ...)
    bind => [241153d01c204ed79109ce658c066f4c, ...]
Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.ibm.db2.jcc.am.fo: DB2 SQL Error: SQLCODE=-530, SQLSTATE=23503, SQLERRMC=SCHEM.SECONDARY.FK_SECONDARY_TO_DERIVED, DRIVER=3.57.82

So question is: why Eclipselink don't insert new record into DERIVED table prior to insertion to SECONDARY table?

P.S. Everything is working fine when no SECONDARY table (ROOT and DERIVED tables only) or no inheritance used (DERIVED tables generates id).


Solution

  • For inheritance JPA assumes the foreign key constraints in related table refer to the root table.

    You can change your constraint to refer to the root table, or,

    use a DescriptorCustomizer to set,

    descriptor.setHasMultipleTableConstraintDependecy(true);
    

    or,

    customizer the OneToOneMapping to have its foreign key refer to the secondary table (JPA annotation always make it refer to the root table).

    Please log a bug though, as JPA join columns should allow you to define a foreign key to the secondary table.

    The reason that EclipseLink does defer the insert into the secondary table is to allow inserts to be grouped by tables to allow batch writing and avoid database deadlocks.