Search code examples
javajpaeclipselinkjpa-criteria

EclipseLink composite PK with FK


I am using eclipselink 2.5.1.

Let's say I have these two class.

JAVA

@Entity
public class Car implements Serializable {
    @EmbeddedId
    protected CarPK carPK;

    private String color;
    @ManyToOne(fetch = FetchType.LAZY)
    private Manufacturor manufacturor;
    //constructors, getters & setters...
}

@Embeddable
public class CarPK implements Serializable {
    @NotNull
    private int idManufacturor;
    @Temporal(javax.persistence.TemporalType.DATE)
    private Date date;
    //constructors, getters & setters...
}

Car has a composite primary key (idManufacturor and date) and idManufacturor is also a foreign key referencing the class Manufacturor.

I'm having issue with the mapping. EclipseLink understand the manufacturor object as a column in my Car table.

Error

Internal Exception: com.microsoft.sqlserver.jdbc.SQLServerException: invalid column name : 'manufacturor'.

I know the problem will be solved if I add a column manufacturor FK but it would be repeating.

Please feel free to ask for any precision if I'm not clear enough.

Thank you for your help.


Solution

  • Add the JoinColumn Annotation

    @JoinColumn(name = "id_manufacturor", referencedColumnName = "id")
    

    Name is the FK column name in your database (not entity).

    The referencedColumnName "id" must correspond to the defined id in manufacturer table.