Search code examples
javahibernatejpaprimary-keyjoincolumn

Column not generated when using JoinColumn (JPA/Hibernate)


I have the following Entities:

@Entity
@IdClass(IntegrationMappingPK.class)
public class IntegrationMapping {
    @Id
    @ManyToOne
    private IntegrationProject project;
    @Id
    private String mappingName;
    ...
}

@Entity
public class MappingElement extends FlowElement {
    @ManyToOne(cascade=CascadeType.PERSIST)
    @PrimaryKeyJoinColumns({
        @PrimaryKeyJoinColumn(name="DISCOVERY_ID", referencedColumnName="DISCOVERY_ID"),
        @PrimaryKeyJoinColumn(name="SCENARIONAME", referencedColumnName="SCENARIONAME"),
        @PrimaryKeyJoinColumn(name="PROJECTNAME", referencedColumnName="PROJECTNAME"),
    })
    @JoinColumn(name="MAPPINGNAME", referencedColumnName="MAPPINGNAME")
    private IntegrationMapping mapping;
    ...
}

@Entity
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@IdClass(FlowElementPK.class)
public class FlowElement {
    @Id
    @ManyToOne
    protected IFlow flow;
    @Id
    @Basic
    protected String name;
    @Id
    @GeneratedValue
    private int id;
    ...
}

IntegrationMapping and FlowElement share the primary key fields "DISCOVERY_ID", "SCENARIONAME" and "PROJECTNAME" (essentially the PK of IntegrationProject; IFlow has a primary key composed of IntegrationProject's PK and a Name), so they are Joined.

I let Hibernate create the Tables from the Entities at startup. When doing so, the Column "MAPPINGNAME" is missing in the FLOWELEMENT table.

I have also tried to use @PrimaryKeyJoinColumn which results in the column being created, but empty altough the variable is not.

Which is the correct way to do this (@JoinColumn or @PrimaryKeyJoinColumn) and why is the Column not created/filled?


Solution

  • Thanks to Prerak Tiwari, I just found the solution. You do not use @PrimaryKeyJoinColumn at all in this scenario, only @JoinColumn. All Primary Key fields that are shared have to be marked with "insertable=false, updateable=false".