[英]Foreign key must have same number of columns as the referenced primary key error, but a have no entities with composite key
I have this entities:我有这个实体:
EmployeeExpert.java员工专家.java
@Entity
@Table(name = "employee_expert")
public class EmployeeExpert {
@Id
@Column(name = "employee_expert_id")
private Integer employeeExpertId;
@ManyToOne
@JoinColumn(name = "employee_id",referencedColumnName = "employee_id")
private Employee employee;
@ManyToOne()
@JoinColumn(name = "employee_competency_id",referencedColumnName = "employee_competency_id")
private EmployeeCompetency employeeCompetency;
@ManyToOne
@JoinColumn(name = "expert_id",referencedColumnName = "employee_id")
private Employee expert;
//getters and setters...
}
EmployeeCompetency.java员工能力.java
@Entity
@Table(name = "employee_competency")
public class EmployeeCompetency {
@Id
@Column(name = "employee_competency_id")
private Integer employeeCompetencyId;
@Column(name = "employee_id")
private Integer employeeId;
@Column(name = "competency_id")
private Integer competencyId;
//getters and setters...
}
Compiler trows this exception:编译器抛出此异常:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.MappingException: Foreign key (FKhwtdqhu7kcw95mqa1i28m50e8:employee_expert [employee_competency_id])) must have same number of columns as the referenced primary key (employee_competency [competency_id,employee_id])
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1694) ~[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:573) ~[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:495) ~[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317) ~[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315) ~[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1087) ~[spring-context-5.0.10.RELEASE.jar:5.0.10.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:857) ~[spring-context-5.0.10.RELEASE.jar:5.0.10.RELEASE]
at main.Application.main(Application.java:11) [classes/:na]
Caused by: org.hibernate.MappingException: Foreign key (FKhwtdqhu7kcw95mqa1i28m50e8:employee_expert [employee_competency_id])) must have same number of columns as the referenced primary key (employee_competency [competency_id,employee_id])
Process finished with exit code 1
It says that that i have a composite primary key in EmployeeCompetency class, but there is only one column have @Id annotation on it.它说我在 EmployeeCompetency 类中有一个复合主键,但只有一列有 @Id 注释。 I don't know what can possibly cause this problem.我不知道什么可能导致这个问题。 What causes this error and how to solve it?导致此错误的原因以及如何解决?
As referencedColumnName
you need to pass the name of id in the parent table:作为referencedColumnName
您需要在父表中传递id 的名称:
@ManyToOne()
@JoinColumn(name = "employee_competency_id",referencedColumnName = "competency_id")
private EmployeeCompetency employeeCompetency;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.