简体   繁体   English

外键必须与引用的主键错误具有相同的列数,但没有具有复合键的实体

[英]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.

相关问题 收到错误“外键必须与引用的主键具有相同的列数”,尽管未使用复合键 - Receiving Error “Foreign key must have same number of columns as the referenced primary key” despite not using composite keys 外键必须具有与引用的主键相同的列数。 但是我没有使用复合键 - Foreign key must have same number of columns as the referenced primary key. But I'm not using a composite key 外键必须与引用的主键具有相同的数字列 - Foreign key must have the same number columns as the referenced primary key 外键必须与@ManyToMany 链接表的引用主键具有相同的列数 - Foreign key must have same number of columns as the referenced primary key of the @ManyToMany linked table org.hibernate.MappingException:外键必须具有与引用的主键相同的列数 - org.hibernate.MappingException: Foreign key must have same number of columns as the referenced primary key Hibernate 外键必须与多对多上引用的主键具有相同的列数 - Hibernate Foreign key must have same number of columns as the referenced primary key on many-to-many Hibernate MappingException:外键必须具有与引用的主键相同的列数 - Hibernate MappingException: Foreign key must have same number of columns as the referenced primary key Hibernate问题:外键必须与引用的主键具有相同数量的列 - Hibernate Issue : Foreign key must have same number of columns as referenced primary key org.hibernate.MappingException:外键XXX必须具有与引用的主键YYY相同的列数 - org.hibernate.MappingException: Foreign key XXX must have same number of columns as the referenced primary key YYY JPA映射注释错误org.hibernate.MappingException:外键必须具有与引用的主键相同的列数 - JPA mapping annotation error org.hibernate.MappingException: Foreign key must have same number of columns as the referenced primary key
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM