Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

org.hibernate.MappingException: Repeated column in mapping for entity

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")

like image 810
ThreaT Avatar asked Aug 17 '12 08:08

ThreaT


2 Answers

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.

like image 174
lost Avatar answered Sep 28 '22 18:09

lost


reason for the exception is the below piece of code, and you have relation to Person entity twice from Vote entity. Why do you need to have relationship twice?

 @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; 
} 
like image 29
Meiyappan Kannappa Avatar answered Sep 28 '22 18:09

Meiyappan Kannappa