JPA 加入复合键的一部分

JPA join on part of composite key

我尝试使用 JPA

加入两个 table

第一个 table 与实体 ReportTripSingle 关联 第二个 table 与实体 TripData 关联 第二个table的主键用复合键描述(实体TripDataPK)

实体 TripDataPK

@Embeddable
public class TripDataPK implements Serializable {


    public TripDataPK() {
    }

    @Column(name = "FTP_ID", nullable = false)
    private long ftpId;

    @Column(name = "FTP_BATCH_ID", nullable = false)
    private long ftpBatchId;

    @Column(name = "FTP_DAY", nullable = false)
    private long ftpDay;

}

实体旅行数据

public class TripData implements java.io.Serializable {

    @EmbeddedId
    private TripDataPK tripDataPK;

    // some properties ...

}

实体 ReportTripSingle

public class ReportTripSingle implements java.io.Serializable {


    @Id
    @Column(name = "TRE_ID", precision = 10, scale = 0)
    private long id;

    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name = "TRE_FTP_ID", referencedColumnName = "FTP_ID")
    private TripData   tripData;


}

如您所见,我想加入 ReportTripSingle 和 TripData

没用:(

这是堆栈跟踪:

Caused by: org.hibernate.AnnotationException: referencedColumnNames(FTP_ID) of com.nexo.susan.be.model.ReportTripSingle.tripData referencing com.nexo.susan.be.model.TripData not mapped to a single property at org.hibernate.cfg.BinderHelper.createSyntheticPropertyReference(BinderHelper.java:336) at org.hibernate.cfg.ToOneFkSecondPass.doSecondPass(ToOneFkSecondPass.java:116) at org.hibernate.cfg.Configuration.processEndOfQueue(Configuration.java:1522) at org.hibernate.cfg.Configuration.processFkSecondPassInOrder(Configuration.java:1443) at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1346) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1737)

确实 TripData 未映射到单个 属性 但我想加入 FTP_ID

此致。

我无法加入部分组合键。 为了解决这个问题,我通过添加 ftpBatchId 和 ftpDay 作为外键修改了与实体 ReportTripSingle 关联的 table

如果有人感兴趣,这是我的实现:

实体 ReportTripSingle

public class ReportTripSingle implements java.io.Serializable {

@Id
@Column(name = "TRE_ID", precision = 10, scale = 0)
private long id;

@Column(name = "TRE_FTP_ID", length = 15, nullable = false, precision = 10, scale = 0, insertable = false, updatable = false)
private long tripDataId;

@Column(name = "TRE_BATCH_ID", nullable = false, insertable = false, updatable = false)
private long tripBatchId;

@Column(name = "TRE_FTP_DEP_DAY", nullable = false,insertable = false, updatable = false)
private long tripDay;

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumns({
        @JoinColumn(name = "TRE_FTP_ID", referencedColumnName = "FTP_ID"),
        @JoinColumn(name = "TRE_BATCH_ID", referencedColumnName = "FTP_BATCH_ID"),
        @JoinColumn(name = "TRE_FTP_DEP_DAY", referencedColumnName = "FTP_DAY")

})
private TripData tripData;
}

更好的选择是在您的@Joincolumn 中使用修饰符 insertable = false 和 updatable = false,如以下所述:https://dwuysan.wordpress.com/2012/02/22/joincolumn-is-part-of-the-composite-primary-keys

public class ReportTripSingle implements java.io.Serializable {


    @Id
    @Column(name = "TRE_ID", precision = 10, scale = 0)
    private long id;

    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name = "TRE_FTP_ID", referencedColumnName = "FTP_ID", **insertable = false, updatable = false**)
    private TripData   tripData;

}