After migration from hibernate 5.6.12.Final to 6.X.X.Final, following model stopped working:
@Entity
public class Person {
@Id
@Column(name = "USER_ID")
private String userId;
@OneToOne(optional = false, fetch = FetchType.EAGER)
@Fetch(FetchMode.JOIN)
@JoinColumn(name = "USER_ID", referencedColumnName = "USER_ID")
private Employee employee;
}
@Entity
public class Employee implements Serializable {
@Id
@Column(name = "USER_ID")
private String userId;
@Column(name = "PARTNER_ID")
private Long partnerId;
@OneToMany(fetch = FetchType.EAGER)
@Fetch(FetchMode.JOIN)
@JoinColumn(name = "PARTNER_ID", referencedColumnName = "PARTNER_ID")
private List<Roles> roles;
}
@Entity
public class Roles implements Serializable {
@Id
@Column(name = "PARTNER_ID")
private Long partnerId;
@Id
@Column(name = "CODE")
private String code;
@Column(name = "NAME")
private String name;
}
When launching application face following error:
Caused by: org.hibernate.AnnotationException: An association that targets entity 'Employee' from entity 'Roles' has no '@JoinColumn' referencing column 'user_id'
at org.hibernate.boot.model.internal.TableBinder.bindPrimaryKeyReference(TableBinder.java:640)
at org.hibernate.boot.model.internal.TableBinder.bindExplicitColumns(TableBinder.java:589)
at org.hibernate.boot.model.internal.TableBinder.bindForeignKey(TableBinder.java:567)
at org.hibernate.boot.model.internal.CollectionBinder.bindCollectionSecondPass(CollectionBinder.java:2650)
at org.hibernate.boot.model.internal.CollectionBinder.bindOneToManySecondPass(CollectionBinder.java:1690)
at org.hibernate.boot.model.internal.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:1588)
at org.hibernate.boot.model.internal.CollectionBinder$1.secondPass(CollectionBinder.java:1577)
at org.hibernate.boot.model.internal.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:45)
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1815)
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1774)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:331)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:1432)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1503)
at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:75)
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:376)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:409)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:396)
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:352)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1822)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1771)
Issue is reproducable on version 6.0.0.Final, 6.2.2.Final and also tried latest 6.4.4.Final. Can you please assist with this problem?