How to set association with partial composite primary key in Hibernate 6.5

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 entities, help in one will be appreciated
3 entities: MrsConHist, MrsVehIncd, MrsVehIncdHex. 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
}

Association in MrsVehIncdHex is he most complicate to me

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

What is the problem you are facing? Do you encounter startup errors? If so, please post the stack trace.

Hi Beikov,
Thank you for asking. I am new in hibernate and upgrading hibernate 3 app to 6.5. I can set some simple association and mapping, but when with composite primary (or mixed with partial with no key fields) key, I have not ideal. Read a lot online, different people post samples in deprecated versions and different approaches, not work for me. I find 3 typical entities in my app. Resolving 1 or 2 could open a big door for me, actually resolve a lot cases.
set the association on:

IN MrsVehIncd:
private Set<MrsConHist > MrsConHists = new HashSet<MrsConHist >(0);
 In MrsConHist:
private Set<MrsVehIncd> mrsVehIncds = new HashSet<MrsVehIncd>(0);
in MrsConHistsHex:
private Set<MrsConHist > MrsConHists_Hex = new HashSet<MrsConHist >(0); 

Could you please help?
Thanks,
Eric

How does your database schema look like? Do you have join tables? How would you join the data between the tables?

Without that information, I can’t really help you.

In case my reply not professional, please excuse, I need help from experts as you to make the heavy door open wider, and I will be able to well deal with or judge the posts online.
I list 3 entities above, each has collection of another entity. the associations are between each other. entity 1) contain entity 2, etc. I have difficulty to add the association.
my app use tables from 3 schema. these 3 entities are in the same schema.
Mrs_Con_Hist table are joined a lot with 4 key, MRS_VEH_INCD is mostly joined by INCD_NO.
Right now I have only worked on 10% of my entities. these 3 are the first complicated associations. The joins are between them. I marked primary key in the entities, If I can fix these three collections, I believed it will cover most of the joins.
In addition, if possible, I try not to use another table to bridge the association between 2 tables. For this point I may be wrong. Just started working with hibernate recently.

Thanks,

Eric

Good morning Beikov,
I added some words for explanation, and refined the wording. the 3 entities are related each other. In each entity, There is a collection for another entity, I ask for how to set the association.
I tried different approaches, not worked.
Please help, resolve one of the entities will help lot.
Thanks,
Eric

You’re not providing enough information. I need to see the table definitions and foreign keys to help you with defining join columns.

Assuming you write a SQL query like the following to join data

select *
from MRS_CON_HIST h 
join MRS_VEH_INCD i 
  on i.CAN_NO = h.CAN_NO
 and i.DASH_NO = h.DASH_NO
 and i.DATE_INSRV_REV = h.DATE_INSRV_REV
 and i.DATETIME_INSRV = h.DATETIME_INSRV

You can map it like this:

@Entity
public class MrsConHist { 
    @OneToMany(mappedBy = "vehicle")
    private Set<MrsVehIncd> mrsVehIncds = new HashSet<MrsVehIncd>(0);
    // functions skipped
}
@Entity
public class MrsVehIncd {   
    @ManyToOne
    @JoinColumn(name = "CAN_NO", referencedColumnName = "CAN_NO")
    @JoinColumn(name = "DASH_NO", referencedColumnName = "DASH_NO")
    @JoinColumn(name = "DATE_INSRV_REV", referencedColumnName = "DATE_INSRV_REV")
    @JoinColumn(name = "DATETIME_INSRV", referencedColumnName = "DATETIME_INSRV")
    MrsConHist vehicle;
}

For further information about mapping, read the documentation or the Hibernate 6 Guide.

1 Like

Hi beikov,
( please see my next reply, I find something interesting) Thank you for the reply. Yes, the join is correct.
{CON_NO, DASH_NO, DATE_INSRV_TREV, DATETIME_INSRV} are primary key set of MrsConHist, but ordinary columns of MrsVehIncident, data types are the same. MrsVehIncident vs MrsConHist is a many to one…

I did this way in both sides, and also tried with @JoinColumns, always get the same error:

A ‘@JoinColumn’ references a column named ‘CON_NO’ but the target entity ‘com.gov.bart.maris.entity.MrsConHist’ has no property which maps to this column.

MrsConHistPK has the definition. Should also define these 4 column in MrsConHist? I replaced CON_NO with another variable for a test, the error turned to complain DASH_NO. So I am at the right place.
Thanks,
Eric

Hi Beikov,
As I mentioned above, I got error:

A '@JoinColumn' references a column named 'CON_NO' but the target entity 'com.gov.bart.maris.entity.MrsConHist' has no property which maps to this column.

A guy has exactly the same issue, and he post his issue at:

https://stackoverflow.com/questions/76079159/joincolumn-references-a-column-but-the-target-entity-has-no-property-which-maps

At [HHH-16501] - Hibernate JIRA , a guy responded :

### Noah Nguyen July 27, 2024 at 10:01 AM
@Gavin King can u please fix this issue in the upcoming release plz ? it seems the later final versions still got this issue.

My version is 6.4.9.

	<dependency>
	    <groupId>org.hibernate.orm</groupId>
	    <artifactId>hibernate-core</artifactId>
	    <version>6.4.9.Final</version>
	</dependency>	
	<dependency>
	    	<groupId>org.hibernate.javax.persistence</groupId>
	    	<artifactId>hibernate-jpa-2.1-api</artifactId>
	    	<version>1.0.2.Final</version>
	</dependency>

Please teach me what should I do. the updated 6.3 is available, or wait for the update?
Thanks,
Eric

I asked you multiple times now to share SQL example of how to join data and provide the DDL schema for the tables. Without that information you are just wasting both our time. I won’t respond any further unless you provide the requested information.

You helped a lot. thanks for your instruction. This issue is resolved. my dependency setting had a problem, but the error not gives direct meaning.
Thanks,
Eric