繁体   English   中英

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

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

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

}

这意味着您将使用NutToffee表在FatRabbitCarrotNutToffee之间FatRabbitCarrot关联。 您将在NutToffee表中增加一个fatRabbitCarrot_id列。

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

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM