Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am doing a simple Poll system. I have 2 tables:

Person: ID, Name, Surname

Vote: ID, Vote (Boolean), VoterID (This is actually FK_PersonID), PersonID (This is actually FK_PersonID as well).

I need to be able to identify who cast the vote as well as who the vote was for - using the people stored in the Person table for both of these needs. The table Person contains user details of people that can "Vote" as well as be "Voted for". People are allowed to decide whether they want to vote for themselves or not.

I've mapped out my tables in my domain objects like this:

Person

    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;
    }

Vote

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;
}

Error message

Caused by: org.hibernate.MappingException: Repeated column in mapping for entity: web.poll.domain.Vote column: personID (should be mapped with insert="false" update="false")

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
281 views
Welcome To Ask or Share your Answers For Others

1 Answer

You use the same @JoinColumn for voter and person. Change to @JoinColumn("personID") for associated person and @JoinColumn("voterID") for associated voter and all should be fine.

As a side note because you tagged this with domain-driven-design... Your vote class would be more DDD style if implemented like this:

@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;
}

Just an example, don't know if I got the meaning of your vote boolean right to indicate an up/down vote.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...