我正在做一个简单的投票系统。我有2张 table :Person
:ID,名称,姓氏Vote
:ID,投票( boolean 值),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;
}
错误消息
最佳答案
您对选民和个人使用相同的@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
boolean 值的含义,以表示赞成/反对表决。关于java - org.hibernate.MappingException : Repeated column in mapping for entity,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12002638/