Foreign key must have same number of columns as the referenced primary key 错误,但没有具有复合键的实体

Foreign key must have same number of columns as the referenced primary key error, but a have no entities with composite key

我有这个实体:

EmployeeExpert.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

@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...
}

编译器抛出此异常:

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[=12=](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

它说我在 EmployeeCompetency class 中有一个复合主键,但只有一列有 @Id 注释。我不知道什么可能导致这个问题。 是什么原因导致此错误以及如何解决?

作为referencedColumnName你需要在父table中传递id的名字:

@ManyToOne()
@JoinColumn(name = "employee_competency_id",referencedColumnName = "competency_id")
private EmployeeCompetency employeeCompetency;