一尘不染

org.hibernate.MappingException:实体映射中的重复列

hibernate

我正在做一个简单的投票系统。我有2张桌子:

Person:ID,姓名,姓氏

Vote:ID,投票(布尔值),VoterID(实际上是FK_PersonID),PersonID(实际上FK_PersonID也是)。

我需要能够使用表中存储的人员来 满足* 这两个需求,从而确定 进行投票以及 谁进行投票
。该表包含可以“投票”以及“被投票”的用户的用户详细信息。人们可以决定是否要为自己投票。Person
*Person

我已经将表映射到我的domain对象中,如下所示:

    private Integer ID;
    private String name;
    private String surname;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "ID")
    public Integer getID() {
        return ID;
    }

    public void setID(Integer ID) {
        this.ID = ID;
    }

    @Column(name = "name")
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Column(name = "surname")
    public String getSurname() {
        return surname;
    }

    public void setSurname(String surname) {
        this.surname = surname;
    }

投票

private Integer ID;
private Person voter;
private Person person;
private Boolean vote;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ID")
public Integer getID() {
    return ID;
}

public void setID(Integer ID) {
    this.ID = ID;
}

@Column(name = "vote")
   public Boolean getVote() {
    return vote;
}

public void setVote(Boolean vote) {
    this.vote = vote;
}

@ManyToOne
@JoinColumn(name = "personID")
public Person getVoter() {
    return voter;
}

public void setVoter(Person voter) {
    this.voter = voter;
}

@ManyToOne
@JoinColumn(name = "personID")
public Person getPerson() {
    return person;
}

public void setPerson(Person person) {
    this.person = person;
}

错误信息

由以下原因引起:org.hibernate.MappingException:实体的映射中重复的列:web.poll.domain.Vote列:personID(应映射为insert
=“ false” update =“ false”)


阅读 241

收藏
2020-06-20

共1个答案

一尘不染

@JoinColumn对选民和个人使用相同的名称。更改@JoinColumn("personID")为关联人和@JoinColumn("voterID")关联选民,一切都很好。

附带说明一下,因为您用domain-driven-design… 标记了它。如果像这样实现,则投票类将具有更多DDD样式:

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ID")
private Integer ID;

@ManyToOne
@JoinColumn(name = "voterID")
private Person voter;

@ManyToOne
@JoinColumn(name = "votedForID")
private Person votedFor;

@Column(name = "vote")
private Boolean vote;

public void cast(Person voter, Person votedFor, boolean vote) {
    // Maybe assert that this vote has not already been casted
    this.voter = voter;
    this.votedFor = votedFor;
    this.vote = vote;
}

public Integer getID() {
    return ID;
}

public Boolean isUpVote() {
    return vote;
}

public Boolean isDownVote() {
    return !vote;
}

public Person getVoter() {
    return voter;
}

public Person getVotedFor() {
    return votedFor;
}

只是一个例子,不知道我是否了解您的vote布尔值的含义以表示赞成/反对表决。

2020-06-20