gpt4 book ai didi

java - Hibernate映射异常: Foreign key must have same number of columns as the referenced primary key

转载 作者:行者123 更新时间:2023-12-02 03:19:25 25 4
gpt4 key购买 nike

我有这个实体,名为 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;
}
}

现在这对我来说似乎是一个有效的类(class)。但看起来并非如此。我们使用 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 问题。无论数据库状态如何,我总是会收到此错误。我对连接该实体的 setter/getter 的各种参数进行了测试,但没有成功。如果我删除新的 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 之间建立关联和NutToffee使用连接表。您将获得额外的fatRabbitCarrot_id NutToffee 中的列表。

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

    @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

关于java - Hibernate映射异常: Foreign key must have same number of columns as the referenced primary key,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56937114/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com