5

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

Person:身份证,姓名,姓氏

Vote: ID, Vote (Boolean), VoterID (This is 实际上FK_PersonID), PersonID (This is 实际上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" 映射)

4

2 回答 2

10

@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布尔权的含义来表示赞成/反对票。

于 2012-08-17T09:03:11.753 回答
1

异常的原因是下面的代码,并且您从 Vote 实体与 Person 实体有两次关系。为什么你需要有两次关系?

 @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; 
} 
于 2012-08-17T09:03:42.677 回答