Hibernate MappingException:外键必须与引用的主键具有相同的列数 [英] Hibernate MappingException: Foreign key must have same number of columns as the referenced primary key

查看:101
本文介绍了Hibernate MappingException:外键必须与引用的主键具有相同的列数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为FatRabbitCarrot的实体:

I have this entity, called 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.

然后,我尝试添加一个新实体,该实体具有上述类的外键.我们将此类称为NutToffee.FatRabbitCarrot与此新实体具有OneToMany关系,而实体本身应具有ManyToOne关系:

Then I tried to add a new entity, that has a foreign key to the class above. Let's call this class NutToffee. FatRabbitCarrot have a OneToMany relationship to this new entity, while the entity itself should have a ManyToOne relationship:

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

现在这对我来说似乎是一个有效的课程.但它看起来不是这样.我们正在使用Java 8,Hibernate JPA 2.1,Java EE 7和gradle构建我们要部署的工件.我们尝试将其部署在Wildfly 10应用程序服务器上,但出现以下错误:

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. We attempt to deploy it on a Wildfly 10 application server, but we get the following error:

[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}

据我了解,Hibernate找到了FatRabbitCarrot的复合主键?即使我们从未定义一个?似乎捡到了一个伪造的主键,在该主键中它使用了来自实体FatRabbitCarrot的两个外键.

From my understanding, Hibernate found a composite primary key for 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.

至于我的测试.我相信这是一个休眠问题.无论数据库状态如何,我总是会收到此错误.我在连接该实体的吸气剂上使用了各种参数进行了测试,但是没有成功.如果我同时删除了新的OneToMany和ManyToOne连接,则将部署工件.

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.

有人知道为什么Hibernate这样做吗?

Does anyone have any idea why Hibernate is doing this?

推荐答案

您使用的 @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;

}

这意味着您将使用联接表在 FatRabbitCarrot NutToffee 之间建立关联.这样,您在 NutToffee 表中将有一个附加的 fatRabbitCarrot_id 列.

This means that you will have association between FatRabbitCarrot and NutToffee using a join table. An you will have an additional fatRabbitCarrot_id column in the NutToffee table.

您需要使用 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;

    }

如果不需要 @ManyToOne 关联,则可以将 @JoinColumn @OneToMany 结合使用,而无需 mappedBy

if you don't need the @ManyToOne association, you can use @JoinColumn with @OneToMany without 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

这篇关于Hibernate MappingException:外键必须与引用的主键具有相同的列数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆