简体   繁体   中英

JPA join on part of composite key

I try to join two tables with JPA

The first table is associated with the entity ReportTripSingle The second table is associated with the entity TripData The primary key of the second table is described with a composite key (entity TripDataPK)

Entity 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;

}

Entity TripData

public class TripData implements java.io.Serializable {

    @EmbeddedId
    private TripDataPK tripDataPK;

    // some properties ...

}

Entity 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;


}

As you can see I want to join ReportTripSingle with TripData

It doesn't work :(

Here is the stack trace :

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)

Indeed TripData is not mapped to a single property but I want to join FTP_ID

Regards.

A better option is to use modifiers insertable = false and updatable = false in your @Joincolumn as explained in : 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;

}

I was not able to join on part of composite key. In order to solve this problem, I have modified the table associated with the entity ReportTripSingle by adding ftpBatchId and ftpDay as foreign keys

If someone is interested, here is my implementation :

Entity 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM