I have an entity class that uses a Embedded primary key and another entity class which has a foreign key column that must refer to a field in the Embedded type.
Below is my entity classes
@Entity
@DynamicUpdate
@Table(name = "LEAGUE_TEAM")
public class LeagueTeam {
@Id
@Embedded
private LeagueTeamPrimaryKey teamKey;
private int draw;
private float form;
private int loss;
private int played;
private int points;
private int position;
@Column(name = "SHRT_NAME", length = 3)
private String shortName;
@Column(name = "STRGTH")
private int strength;
@Column(name = "TEAM_DIV", length = 10)
private String teamDivision;
@Column(name = "UN_AVLBL")
private boolean unavailable;
private int win;
@Column(name = "STRGTH_OVR_HOME")
private int strengthOverallHome;
@Column(name = "STRGTH_OVR_AWAY")
private int strengthOverallAway;
@Column(name = "STRGTH_ATT_HOME")
private int strengthAttackHome;
@Column(name = "STRGTH_ATT_AWAY")
private int strengthAttackAway;
@Column(name = "STRGTH_DEF_HOME")
private int strengthDefenceHome;
@Column(name = "STRGTH_DEF_AWAY")
private int strengthDefenceAway;
@Column(name = "PULSE_ID")
private int pulseId;
@Transient
private int code;
@Transient
private String name;
}
and
@Entity
@Table(name = "PLYR_BSC_INFO")
public class PlayerBasicInformation {
@Id
private Long code;
@Column(name = "FRST_NAME")
private String firstName;
@Column(name = "SECD_NAME")
private String secondName;
@Column(name = "SQD_NO")
private Integer squadNumber;
@Column(name = "PLYR_STS")
private Character status;
@ManyToOne(optional = false)
@JoinColumn(name = "TEAM_CODE", referencedColumnName = "CODE")
private LeagueTeam teamCode;
@Column(name = "WEB_NAME")
private String webName;
}
Here is the embeddable type
@Embeddable
@Access(value = AccessType.FIELD)
public class LeagueTeamPrimaryKey {
@NonNull
@Column(unique = true, updatable = false)
private int code;
@NonNull
@Column(length = 30, updatable = false)
private String name;
}
I want the foreign key column teamCode in PlayerBasicInformation.class refer to column teamKey.code in LeagueTeam.class
However with the setup I get the below error when running the app using spring boot 3.2.1 and java 21.
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.MappingException: Property 'com.fantasy.football.model.PlayerBasicInformation.teamCode' maps to 1 columns but 2 columns are required (type 'com.fantasy.football.model.LeagueTeam' spans 2 columns)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1773) ~[spring-beans-6.1.2.jar:6.1.2]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:599) ~[spring-beans-6.1.2.jar:6.1.2]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:521) ~[spring-beans-6.1.2.jar:6.1.2]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:325) ~[spring-beans-6.1.2.jar:6.1.2]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-6.1.2.jar:6.1.2]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:323) ~[spring-beans-6.1.2.jar:6.1.2]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-6.1.2.jar:6.1.2]
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1232) ~[spring-context-6.1.2.jar:6.1.2]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:950) ~[spring-context-6.1.2.jar:6.1.2]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:625) ~[spring-context-6.1.2.jar:6.1.2]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:146) ~[spring-boot-3.2.1.jar:3.2.1]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:762) ~[spring-boot-3.2.1.jar:3.2.1]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:464) ~[spring-boot-3.2.1.jar:3.2.1]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:334) ~[spring-boot-3.2.1.jar:3.2.1]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1358) ~[spring-boot-3.2.1.jar:3.2.1]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1347) ~[spring-boot-3.2.1.jar:3.2.1]
at com.fantasy.football.web.api.FantasyFootballWebApplication.main(FantasyFootballWebApplication.java:10) ~[classes/:na]
Caused by: jakarta.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.MappingException: Property 'com.fantasy.football.model.PlayerBasicInformation.teamCode' maps to 1 columns but 2 columns are required (type 'com.fantasy.football.model.LeagueTeam' spans 2 columns)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:421) ~[spring-orm-6.1.2.jar:6.1.2]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:396) ~[spring-orm-6.1.2.jar:6.1.2]
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:352) ~[spring-orm-6.1.2.jar:6.1.2]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1820) ~[spring-beans-6.1.2.jar:6.1.2]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1769) ~[spring-beans-6.1.2.jar:6.1.2]
... 16 common frames omitted
Caused by: org.hibernate.MappingException: Property 'com.fantasy.football.model.PlayerBasicInformation.teamCode' maps to 1 columns but 2 columns are required (type 'com.fantasy.football.model.LeagueTeam' spans 2 columns)
at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:672) ~[hibernate-core-6.4.1.Final.jar:6.4.1.Final]
at org.hibernate.mapping.RootClass.validate(RootClass.java:273) ~[hibernate-core-6.4.1.Final.jar:6.4.1.Final]
at org.hibernate.boot.internal.MetadataImpl.validate(MetadataImpl.java:497) ~[hibernate-core-6.4.1.Final.jar:6.4.1.Final]
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:273) ~[hibernate-core-6.4.1.Final.jar:6.4.1.Final]
at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:450) ~[hibernate-core-6.4.1.Final.jar:6.4.1.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1507) ~[hibernate-core-6.4.1.Final.jar:6.4.1.Final]
at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:75) ~[spring-orm-6.1.2.jar:6.1.2]
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:376) ~[spring-orm-6.1.2.jar:6.1.2]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:409) ~[spring-orm-6.1.2.jar:6.1.2]
... 20 common frames omitted
Any help would be appreciated.