简体   繁体   English

外键必须与@ManyToMany 链接表的引用主键具有相同的列数

[英]Foreign key must have same number of columns as the referenced primary key of the @ManyToMany linked table

I have a many to many relation between an entity Function and Result and I used an associative table called Function_produces_result .我在实体FunctionResult之间存在多对多关系,我使用了一个名为Function_produces_result的关联表。

图式

AbstractEntity.java:抽象实体.java:

@Getter
@Setter
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
@ToString
public abstract class AbstractEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
}

FunctionEntity.java:功能实体.java:

@Entity
@Table(name = "function")
@Getter
@Setter
public class FunctionEntity extends AbstractEntity {

    private String description;

    @ManyToMany
    @JoinTable(
        name = "function_produces_result",
        joinColumns = @JoinColumn(name = "function_id"),
        inverseJoinColumns = @JoinColumn(name = "result_id")
    )
    Set<ResultEntity> producedResults;
}

FunctionProduceResultEntity.java: FunctionProduceResultEntity.java:

@Entity
@Table(name="function_produces_result")
@Getter
@Setter
public class FunctionProduceResultEntity extends AbstractEntity {
    
    private int rank;

    @ManyToOne
    @JoinColumn(name = "function_id", nullable = false)
    private FunctionEntity function;
    
    @ManyToOne
    @JoinColumn(name = "result_id", nullable = false)
    private ResultEntity result;
}

ResultEntity.java结果实体.java

@Entity
@Table(name = "result")
@Getter
@Setter
public class ResultEntity extends AbstractEntity {

    @Column(nullable = false, unique = true)
    private String code;

    private String name;

    @Column(nullable = false)
    private String type;
}

FunctionInputEntity:函数输入实体:

@Entity
@Table(name="function_input")
@Getter
@Setter
public class FunctionInputEntity extends AbstractEntity {

    @Enumerated(EnumType.STRING)
    @Column(nullable = false)
    private FunctionInputTypeEnum type;
    
    @ManyToOne
    @JoinColumn(name = "function_id", nullable = false)
    private FunctionEntity function;
    
    @ManyToOne
    @JoinColumn(name = "driver_id")
    private DriverEntity driver;
    
    @ManyToOne
    @JoinColumn(name = "function_result_id")
    private FunctionProduceResultEntity sourceResource;
}

When I launch the server I got this error:当我启动服务器时出现此错误:

Invocation of init method failed; nested exception is org.hibernate.MappingException: Foreign key (FK4t4snpmx4ck9oj8wkjxvjkg88:function_input [function_result_id])) must have same number of columns as the referenced primary key (function_produces_result [function_id,result_id])
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:342)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:113)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1697)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1442)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:593)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:516)
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:324)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:226)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:322)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:624)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:612)
    at org.springframework.data.repository.config.DeferredRepositoryInitializationListener.onApplicationEvent(DeferredRepositoryInitializationListener.java:51)
    at org.springframework.data.repository.config.DeferredRepositoryInitializationListener.onApplicationEvent(DeferredRepositoryInitializationListener.java:36)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)
    at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:404)
    at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:361)
    at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:898)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:554)
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:143)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:315)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1237)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226)
    at com.socgen.marvel.ego.Application.main(Application.java:10)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)

There are similar questions such as foreign key must have same number of columns as the referenced primary key for many-to-one mapping and Hibernate MappingException: Foreign key must have same number of columns as the referenced primary key but don't fix my problem.还有类似的问题,例如foreign key must have same number of columns as the referenced primary key for many-to-one mappingHibernate MappingException: Foreign key must have same number of columns as the referenced primary key但不要解决我的问题.

Your problem is related to:您的问题与以下内容有关:

@JoinTable(
            name = "function_produces_result",
            joinColumns = @JoinColumn(name = "function_id"),
            inverseJoinColumns = @JoinColumn(name = "result_id")
    )

You tell Spring how the many-to-many relationship starts FunctionEntity s but have not provided an instruction that tells spring what the field result_id maps to on the other side (ie you've not specified where it ends), only that it is part of the many-to-many relationship.您告诉 Spring 多对多关系如何开始FunctionEntity ,但没有提供说明告诉 spring 字段result_id在另一侧映射到什么(即您没有指定它结束的位置),只是它是一部分的多对多关系。

To fix this you can add the following to your ResultEntity class definition:要解决此问题,您可以将以下内容添加到您的ResultEntity class 定义中:

@ManyToMany(mappedBy = "producedResults")
Set<FunctionEntity> functionEntities = new HashSet<>();

And it should work from what I can see online here: https://www.infoworld.com/article/3387643/java-persistence-with-jpa-and-hibernate-part-2-many-to-many-relationships.html它应该根据我在这里在线看到的内容工作: https://www.infoworld.com/article/3387643/java-persistence-with-jpa-and-hibernate-part-2-many-to-many-relationships。 html

In FunctionInputEntity , Hibernate expects both the foreign key references in it.FunctionInputEntity中, Hibernate 需要其中的两个外键引用。 Can you please try with the below?你能试试下面的吗?

@ManyToOne
    @JoinColumns({
       @JoinColumn(name = "function_id"),
       @JoinColumn(name = "result_id")
    })
private FunctionProduceResultEntity sourceResource;

The root cause of your problem is that you try to mix up @ManyToMany mapping with hidden linked table via @JoinTable and mapping via linked entity .问题的根本原因是您试图通过@JoinTable@ManyToMany 映射与隐藏链接表混合,并通过链接实体进行映射。 Actually your linked table function_produces_result has the id primary key, but when you use the mapping via the @JoinTable annotation, hibernate assumes that linked table should have a composite primary key based on the foreign keys of the @ManyToMany association.实际上,您的链接表function_produces_result具有id主键,但是当您通过@JoinTable注释使用映射时,hibernate 假定链接表应具有基于@ManyToMany关联的外键的复合主键。

So, I would suggest you to rewrite the mapping of the FunctionEntity entity in the following way:因此,我建议您按以下方式重写FunctionEntity实体的映射:

@Entity
@Table(name = "function")
public class FunctionEntity extends AbstractEntity
{
   @OneToMany(mappedBy = "function")
   @MapKey(name = "result")
   private Map<ResultEntity, FunctionProduceResultEntity> producedResults;
}

It will simplify access to the ResultEntity entity from the FunctionEntity entity.它将简化从FunctionEntity实体访问ResultEntity实体。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 外键必须与引用的主键具有相同的数字列 - Foreign key must have the same number 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 外键必须具有与引用的主键相同的列数。 但是我没有使用复合键 - Foreign key must have same number of columns as the referenced primary key. But I'm not using a composite key 收到错误“外键必须与引用的主键具有相同的列数”,尽管未使用复合键 - Receiving Error “Foreign key must have same number of columns as the referenced primary key” despite not using composite keys 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 - 字段必须与引用的主键具有相同的列数 - JPA - field must have same number of columns as the referenced primary key
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM