JPA(EclipseLink)SecondaryTable 与 java 1.7 错误连接

 2022-01-07 

JPA (EclipseLink) SecondaryTable erroneous join with java 1.7

我使用 SecondaryTable 注释创建了一个将客户表与客户详细信息表连接的实体

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@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;

    ...
}

它正在使用 java 1.6,生成的查询就像

1
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

但是当我迁移到 java 1.7 时,生成的查询接缝错误

1
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

并返回多行。

jpa secondarytable和java 1.7有什么问题吗?


解决方案来自于避免CUST_DETAIL_ID字段名称引起的混淆?3n,JPA需要知道属于哪个表,以及必须按字母顺序排序的属性custDetailId和custDetailPk的名称。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@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;

    ...
}