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

2024-04-24

我有一个名为 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;
}
}

它有效。现在上面的类已经替换了字段名称,但结构与我们的存储库中的相同。

然后我尝试添加一个新实体,该实体具有上面类的外键。我们将这个类称为 NutToffee。 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;
}
}

现在这对我来说似乎是一门有效的课程。但看起来并非如此。我们使用 Java 8、Hibernate JPA 2.1、Java EE 7 和 gradle 来构建我们想要部署的工件。我们尝试将其部署在 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}

据我了解,Hibernate找到了FatRabbitCarrot的复合主键?即使我们从未定义过?它似乎选取了一个假主键,其中使用了来自实体 FatRabbitCarrot 的两个外键。

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

有谁知道 Hibernate 为什么这样做?


您正在使用@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 and NutToffee使用连接表。您将获得额外的fatRabbitCarrot_id栏目中的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 with @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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

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

随机推荐