Search code examples
javajpaeclipselink

JPA (EclipseLink) SecondaryTable erroneous join with java 1.7


I have created an entity joining Customer table with Customer detail table using SecondaryTable annotation

@Entity
@Table(name="CUSTOMER")
@SecondaryTable(name="CUST_DETAIL", 
            pkJoinColumns=@PrimaryKeyJoinColumn(name="CUST_DETAIL_ID", referencedColumnName = "CUST_DETAIL_ID"))
public class Customer {
    @Id
    @Column(name = "CUST_ID")
    private Long id;

    @Column(name = "CUST_DETAIL_ID")
    private Long custDetailPk;

    @Column(name = "CUST_DETAIL_ID", table = "CUST_DETAIL")
    private Long custDetailId;

    ...
} 

It is working with java 1.6, the generated query is like

SELECT t0.CUST_ID, t0.CUST_DETAIL_ID, t1.CUST_DETAIL_ID, ... FROM CUSTOMER t0, CUST_DETAIL t1 WHERE t1.CUST_DETAIL_ID = t0.CUST_DETAIL_ID

but when I migrate to java 1.7, the generated query seams erroneous

SELECT t0.CUST_ID, t1.CUST_DETAIL_ID, t0.CUST_DETAIL_ID, ... FROM CUSTOMER t0, CUST_DETAIL t1 WHERE t1.CUST_DETAIL_ID = t1.CUST_DETAIL_ID

and returning multiple rows.

Is there any problem with jpa secondarytable and java 1.7?


Solution

  • The solution comes from avoiding the confusión caused by the names of the fields CUST_DETAIL_ID, JPA needs to know what table belongs, and the name of the properties custDetailId and custDetailPk that it must be ordered alphabetically.

    @Entity
    @Table(name="CUSTOMER")
    @SecondaryTable(name="CUST_DETAIL", 
                pkJoinColumns=@PrimaryKeyJoinColumn(name="CUST_DETAIL_ID", referencedColumnName = "CUST_DETAIL_ID"))
    public class Customer {
        @Id
        @Column(name = "CUST_ID")
        private Long id;
    
        @Column(name = "CUST_DETAIL_ID", table = "CUSTOMER")
        private Long custDetailId;
    
        @Column(name = "CUST_DETAIL_ID", table = "CUST_DETAIL")
        private Long custDetailPk;
    
        ...
    }