简体   繁体   中英

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM