How to join composite primary key by no-key fields

Hello all,
Entity MrsVehIncd’s primary key is incdNo, and it has all the 4 fields of the primary key of entity MrsConHist, now I need to join
MrsVehIncd with MrsConHist on these 4 fields, I got error. please let me show the entities first.

  1. MrsVehIncd
@Table(name = "MRS_VEH_INCD")
@Entity
public class MrsVehIncd  {
    @Column(name = "INCD_NO")
    @Id
    private String incdNo;
    @Column(name = "VEH_INCD_STAT")
    private String vehIncdStat;
    @Column(name = "CAN_NO")    
    private String conNo;
    @Column(name = "DASH_NO")
    private Integer dashNo;
    @Column(name = "DATE_INSRV_TREV")
    private java.util.Date dateInsrvRev;
    @Column(name = "DATETIME_INSRV")
    private Date datetimeInsrv;
       
    @ManyToOne (fetch = FetchType.LAZY)
       @JoinColumns({
           @JoinColumn(name="CON_NO", referencedColumnName = "CON_NO"), 
           @JoinColumn(name = "DASH_NO", referencedColumnName = "DASH_NO"),
           @JoinColumn(name = "DATE_INSRV_TREV", referencedColumnName = "DATE_INSRV_TREV"),
           @JoinColumn(name = "DATETIME_INSRV", referencedColumnName = "DATETIME_INSRV")
       })    
    private MrsConHist mrsConHist;    
    //getter, setter, ...
 }

2)MrsConHist

@Table(name = "MRS_CON_HIST")
@Entity
public class MrsConHist { 
    @EmbeddedId
    private MrsConHistPK comp_id;
    @Column(name = "DATETIME_OUTSRV")
	private Date datetimeOutsrv;

    @OneToMany(mappedBy="mrsConHist")
    private Set<MrsVehIncd> mrsVehIncds = new HashSet<MrsVehIncd>(0);
    //getter, setter
}
Composite key:
@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_TREV")
    private java.util.Date dateInsrvRev;
    @Column(name = "DATETIME_INSRV")
    private Date datetimeInsrv;

    public MrsConHistPK() {
    }

    public MrsConHistPK(String conNo, Integer dashNo, java.util.Date dateInsrvRev, Date datetimeInsrv) {
      this.conNo = conNo;
      this.dashNo = dashNo;
      this.dateInsrvRev = dateInsrvRev;
      this.datetimeInsrv = datetimeInsrv;
    }
    //getter, setter, ..
}

Error:

Initial SessionFactory creation failed.org.hibernate.MappingException: 
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 

I am sure the error is related to

@JoinColumn(name="CON_NO", referencedColumnName = "CON_NO") 
 in MrsVehIncd, 

because if I change the “CON_NO” to another existed column,
it starts to complain the “DASH_NO”.

please help.
Thanks,Eric

Hi @ezuo, what version of Hibernate are you using? If this behavior persists with the latest supported version, this might be a bug. Please try to create a reproducer with our test case template and if you are able to reproduce the issue, create a new ticket in our issue tracker and attach that reproducer.

1 Like

Hi mbladel,
Yes, I see it at [HHH-16501] - Hibernate JIRA , hibernate is working on it. I will try the test template.

My version is 6.4.9

org.hibernate.orm
hibernate-core
6.4.9.Final
org.hibernate.javax.persistence
hibernate-jpa-2.1-api
1.0.2.Final

Do you know which version does not have this issue, V 6.3? and when V 6.4.9 will be fixed?

Thanks,
Eric

That bug report was fixed in version 6.3 and newer. I believe the error you’re seeing might be different, though they look similar. Once again, I would suggest to upgrade to the latest supported version and see if the issue persists.

Hi mbladel,
After I added this dependency:

    <dependency>
	    <groupId>jakarta.persistence</groupId>
	    <artifactId>jakarta.persistence-api</artifactId>
	    <version>3.1.0</version>
    </dependency>

The error did not appear, but new exception looks like thrown in a old log4j lib files.:

Exception in thread "main" java.lang.UnsupportedClassVersionError: org/springframework/boot/logging/logback/RootLogLevelConfigurator has been compiled by a more recent version of the Java Runtime (class file version 61.0), this version of the Java Runtime only recognizes class file versions up to 55.0
	at java.base/java.lang.ClassLoader.defineClass1(Native Method)
	at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1016)
	at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:174)
	at java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(BuiltinClassLoader.java:800)
	at java.base/jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(BuiltinClassLoader.java:698)
	at java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(BuiltinClassLoader.java:621)
	at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:579)
	at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
	at java.base/java.lang.Class.forName0(Native Method)
	at java.base/java.lang.Class.forName(Class.java:398)
	at java.base/java.util.ServiceLoader$LazyClassPathLookupIterator.nextProviderClass(ServiceLoader.java:1210)
	at java.base/java.util.ServiceLoader$LazyClassPathLookupIterator.hasNextService(ServiceLoader.java:1221)
	at java.base/java.util.ServiceLoader$LazyClassPathLookupIterator.hasNext(ServiceLoader.java:1265)
	at java.base/java.util.ServiceLoader$2.hasNext(ServiceLoader.java:1300)
	at java.base/java.util.ServiceLoader$3.hasNext(ServiceLoader.java:1385)
	at ch.qos.logback.classic.util.ClassicEnvUtil.loadFromServiceLoader(ClassicEnvUtil.java:50)
	at ch.qos.logback.classic.util.ContextInitializer.autoConfig(ContextInitializer.java:84)
	at ch.qos.logback.classic.util.ContextInitializer.autoConfig(ContextInitializer.java:66)
	at ch.qos.logback.classic.spi.LogbackServiceProvider.initializeLoggerContext(LogbackServiceProvider.java:52)
	at ch.qos.logback.classic.spi.LogbackServiceProvider.initialize(LogbackServiceProvider.java:41)
	at org.slf4j.LoggerFactory.bind(LoggerFactory.java:152)
	at org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:139)
	at org.slf4j.LoggerFactory.getProvider(LoggerFactory.java:422)
	at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:408)
	at org.apache.logging.slf4j.SLF4JLogger.<clinit>(SLF4JLogger.java:39)
	at org.apache.logging.slf4j.SLF4JLoggerContext.getLogger(SLF4JLoggerContext.java:36)
	at org.apache.logging.log4j.LogManager.getLogger(LogManager.java:678)
	at com.eng.maris.utility.LoggerService.<clinit>(LoggerService.java:14)
	at com.eng.maris.BusinessServices.MaximoIntegrationService.synchData(MaximoIntegrationService.java:137)
	at com.eng.maris.BusinessServices.MaximoIntegrationService.startService(MaximoIntegrationService.java:102)
	at com.eng.BusinessServices.MaximoIntegrationService.main(MaximoIntegrationService.java:75)

Thanks,
Eric

Spring Boot 3 requires Java 17 and you seem to run Java 11. But this has nothing to do with Hibernate ORM, so please ask these questions in other forums.

1 Like

yes, I will try to fix it. it means my original question is resolved.
Thank you,