简体   繁体   English

Hibernate MappingException:外键必须具有与引用的主键相同的列数

[英]Hibernate MappingException: Foreign key must have same number of columns as the referenced primary key

I have this entity, called FatRabbitCarrot: 我有一个名为FatRabbitCarrot的实体:

@Entity
public class FatRabbitCarrot {
    private Long id;
    private FatRabbit fatRabbit;
    private Carrot carrot;

@Id
@Column(name = "id", nullable = false)
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

@ManyToOne
@JoinColumn(name = "fatRabbit_id", foreignKey = @ForeignKey(name = "FK_FatRabbitCarrot_fatRabbit"))
public FatRabbit getFatRabbit() {
    return fatRabbit;
}

public void setFatRabbit(FatRabbit fatRabbit) {
    this.fatRabbit = fatRabbit;
}

@ManyToOne
@JoinColumn(name = "carrot_id", foreignKey = @ForeignKey(name = "FK_FatRabbitCarrot_carrot"))
public Carrot getCarrot() {
    return carrot;
}

public void setCarrot(Carrot carrot) {
    this.carrot = carrot;
}
}

And it works. 而且有效。 Now the above class had field names replaced, but the structure is the same as in our repository. 现在,上述类已替换了字段名称,但结构与我们的存储库中的相同。

Then I tried to add a new entity, that has a foreign key to the class above. 然后,我尝试添加一个新实体,该实体具有上述类的外键。 Let's call this class NutToffee. 我们称此类为NutToffee。 FatRabbitCarrot have a OneToMany relationship to this new entity, while the entity itself should have a ManyToOne relationship: FatRabbitCarrot与此新实体具有OneToMany关系,而实体本身应具有ManyToOne关系:

@Entity
public class NutToffee {

private Long id;
private String text;
private FatRabbitCarrot fatRabbitCarrot;

@Id
@Column(name = "id", nullable = false)
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Long getId() {
    return id;
}

public void setId(Long id){
    this.id = id;
}

@Basic
@Column(name = "text")
public String getText() {
    return text;
}

public void setText(String text) {
    this.text = text;
}

@ManyToOne
@JoinColumn(name="fatRabbitCarrot_id", foreignKey = @ForeignKey(name = "FK_NutToffee_fatRabbitCarrot"))
public FatRabbitCarrot getFatRabbitCarrot() {
    return fatRabbitCarrot;
}

public void setFatRabbitCarrot(FatRabbitCarrot fatRabbitCarrot) {
    this.fatRabbitCarrot = fatRabbitCarrot;
}
}

Now this seems like a valid class to me. 现在这对我来说似乎是一个有效的课程。 But it doesn't look like it is. 但这看起来并不像。 We are using Java 8, Hibernate JPA 2.1, Java EE 7 and gradle to build the artifact we want to deploy. 我们正在使用Java 8,Hibernate JPA 2.1,Java EE 7和gradle构建我们要部署的工件。 We attempt to deploy it on a Wildfly 10 application server, but we get the following error: 我们尝试将其部署在Wildfly 10应用程序服务器上,但是出现以下错误:

[2019-07-08 03:53:45,441] Artifact Gradle : com.solveralynx.wildrunner : fatties.war: java.lang.Exception: {"WFLYCTL0080: Failed services" => {"jboss.persistenceunit.\"fatties.war#FatUnit\"" => "org.jboss.msc.service.StartException in service jboss.persistenceunit.\"fatties.war#FatUnit\": org.hibernate.MappingException: Foreign key (FK_NutToffee_fatRabbitCarrot:NutToffee [fatRabbitCarrot_id])) must have same number of columns as the referenced primary key (FatRabbitCarrot [fatRabbit_id,carrot_id])
Caused by: org.hibernate.MappingException: Foreign key (FK_NutToffee_fatRabbitCarrot:NutToffee [fatRabbitCarrot_id])) must have same number of columns as the referenced primary key (FatRabbitCarrot [fatRabbit_id,carrot_id])"},"WFLYCTL0412: Required services that are not installed:" => ["jboss.persistenceunit.\"fatties.war#FatUnit\""],"WFLYCTL0180: Services with missing/unavailable dependencies" => undefined}

From my understanding, Hibernate found a composite primary key for FatRabbitCarrot? 据我了解,Hibernate找到了FatRabbitCarrot的复合主键? Even though we never defined one? 即使我们从未定义一个? It seems to pick up a fake primary key, where it uses both foreign keys from entity FatRabbitCarrot. 似乎捡到了一个伪造的主键,在其中它使用了来自实体FatRabbitCarrot的两个外键。

As for my testing. 至于我的测试。 I am confident this is a Hibernate issue. 我相信这是一个休眠问题。 No matter the database state, I always get this error. 无论数据库状态如何,我总是会收到此错误。 I tested with various parameters on the getters, that connect that entities, but no success. 我在连接该实体的吸气剂上测试了各种参数,但没有成功。 If I remove both new OneToMany and ManyToOne connections, the artifact deploys. 如果我同时删除了新的OneToMany和ManyToOne连接,则将部署工件。

Does anyone have any idea why Hibernate is doing this? 有谁知道为什么Hibernate这样做?

You are using @JoinColumn annotation incorrectly. 您使用的@JoinColumn注解不正确。

@Entity
public class FatRabbitCarrot {

    @Id
    @GeneratedValue
    private Long id;

    @OnToMany
    private List<NutToffee> toffies;

}

@Entity
public class NutToffee {

    @Id
    @GeneratedValue
    private Long id;

    @ManyToOne
    @JoinColumn(name = "fatRabbitCarrot_id")
    private FatRabbitCarrot fatRabbitCarrot;

}

This means that you will have association between FatRabbitCarrot and NutToffee using a join table. 这意味着您将使用NutToffee表在FatRabbitCarrotNutToffee之间FatRabbitCarrot关联。 An you will have an additional fatRabbitCarrot_id column in the NutToffee table. 您将在NutToffee表中增加一个fatRabbitCarrot_id列。

You need to use mappedBy 您需要使用mappedBy

    @Entity
    public class FatRabbitCarrot {

        @Id
        @GeneratedValue
        private Long id;

        @OnToMany(mappedBy = "fatRabbitCarrot")
        private List<NutToffee> toffies;

    }

    @Entity
    public class NutToffee {

        @Id
        @GeneratedValue
        private Long id;

        @ManyToOne
        @JoinColumn(name = "fatRabbitCarrot_id")
        private FatRabbitCarrot fatRabbitCarrot;

    }

if you don't need the @ManyToOne association, you can use @JoinColumn with @OneToMany without mappedBy 如果你不需要@ManyToOne关联,您可以使用@JoinColumn@OneToMany没有mappedBy

    @Entity
    public class FatRabbitCarrot {

        @Id
        @GeneratedValue
        private Long id;

        @OnToMany
        @JoinColumn(name = "fatRabbitCarrot_id")
        private List<NutToffee> toffies;

    }

    @Entity
    public class NutToffee {

        @Id
        @GeneratedValue
        private Long id;


    }

https://stackoverflow.com/a/37542849/3405171 https://stackoverflow.com/a/37542849/3405171

暂无
暂无

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

相关问题 org.hibernate.MappingException:外键必须具有与引用的主键相同的列数 - org.hibernate.MappingException: Foreign key must have same number of columns as the 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 org.hibernate.MappingException:外键(FK12A711396456CA10 :)必须具有与引用的主键相同的列数 - org.hibernate.MappingException: Foreign key (FK12A711396456CA10:) must have same number of columns as the referenced primary key JPA映射注释错误org.hibernate.MappingException:外键必须具有与引用的主键相同的列数 - JPA mapping annotation error org.hibernate.MappingException: Foreign key must have same number of columns as the referenced primary key 外键必须与引用的主键具有相同的数字列 - Foreign key must have the same number columns as the referenced primary key Hibernate问题:外键必须与引用的主键具有相同数量的列 - Hibernate Issue : Foreign key must have same number of columns as referenced primary key Hibernate 外键必须与多对多上引用的主键具有相同的列数 - Hibernate Foreign key must have same number of columns as the referenced primary key on many-to-many 外键必须与引用的主键错误具有相同的列数,但没有具有复合键的实体 - 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
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM