Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Hibernate 6.5, how to set mapping between entities with partial composite primary key

Eric ZuoAug 9 2024

Hello Experts,
I understand ManyToMany, ManyToOne, OneToMany in database table relationship.
but still confused in setting fields to associate entities with composite (whole or partial) primary keys. I am stopped in days by this problem.
In the following 3 entities, each has a collection of another entity, I have difficulty to add the association.
Please find my questions for the connection in each of the entities. I deleted some fields to short the post.
Entity 1

@Table(name = "MRS_CON_HIST")
@Entity
public class MrsConHist { 
    @EmbeddedId
    private MrsConHistPK comp_id;
    @Column(name = "DATETIME_OUTSRV")
    private Date datetimeOutsrv;    
    @Column(name = "RUN_TIME_HRS")
    private int runTimeHrs;
    @Column(name = "RUN_TIME_MIN")
    private int runTimeMin;
    @Column(name = "CON_STAT")
    private String conStat;
    //How to set the association here:
    private Set<MrsVehIncd> mrsVehIncds = new HashSet<MrsVehIncd>(0);
    // functions skipped
}
*Key of Entity 1*
@Embeddable
public class MrsConHistPK  implements java.io.Serializable {
    private static final long serialVersionUID = 1L;
    @Column(name = "CAN_NO")
    private String conNo;
    @Column(name = "DASH_NO")
    private Integer dashNo;
    @Column(name = "DATE_INSRV_REV")
    private java.util.Date dateInsrvRev;
    @Column(name = "DATETIME_INSRV")
    private Date datetimeInsrv;
    // functions skipped
}

Entity 2.

@Table(name = "MRS_VEH_INCD")
@Entity
public class MrsVehIncd {   
private static final long serialVersionUID = 1L;
    @Column(name = "INCD_NO") 
    @Id
    private String incdNo;          //primary key,  also has the 4 fields of MrsConHistPK,
    @Column(name = "VEH_INCD_STAT")
    private String vehIncdStat;
    @Column(name = "CAN_NO")
    private String conNo;
    @Column(name = "DASH_NO")
    private Integer dashNo;
    @Column(name = "WORK_TYPE")
    private Integer workType;
    @Column(name = "DATE_INSRV_REV")
    private java.util.Date dateInsrvRev;
    @Column(name = "DATETIME_INSRV")
    private Date datetimeInsrv;
    //How to set the association here:
    private Set<MrsConHist > MrsConHists = new HashSet<MrsConHist >(0);
    // functions skipped
}

Entity 3.

@Table(name = MRS_VEH_INCDHEX)
@Entity
public class MrsVehIncdHex {  
    @EmbeddedId
    private MrsConHistHexPK comp_id;
     @Column(name = "DATE_INSRV_REV")
    private java.util.Date dateInsrvRev;
    ......
    //How to set the association here:
    private Set<MrsConHist > MrsConHists_Hex = new HashSet<MrsConHist >(0);
    // functions skipped
}

Key of Entity 3 // it has part of the key fields in the entities 1. and 2. above.

@Embeddable
public class MrsVehIncdHexPK  implements java.io.Serializable {
    private static final long serialVersionUID = 1L;
    @Column(name = "CON_NO")
    private String conNo;
    @Column(name = "INCD_NO")
    private String incdNo;
    @Column(name = "VTDCODE")
    private String vtdCode;
    // functions skipped
}

In case you could spare some time, please help.
Thank you,
Eric

Comments
Post Details
Added on Aug 9 2024
0 comments
206 views