我有一个名为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的两个外键。
至于我的测试。我相信这是一个休眠问题。无论数据库状态如何,我总是会收到此错误。我在连接该实体的吸气剂上使用了各种参数进行了测试,但是没有成功。如果我同时删除了新的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