0

I have 2 classes, an Abstract class annotated with @MappedSuperclass and a child class which extends the class. When i run my program, i get the following error:

Caused by: org.hibernate.MappingException: Repeated column in mapping for entity: models.Comment column: comment_id (should be mapped with insert="false" update="false")

But I'm looking through my code and I don't see any repeated column names. This is my first time using the Mapped supper class annotation and I'm not sure if I am doing something wrong/using the annotation. Here are the two classes I have:

@MappedSuperclass
public abstract class AbstractModel {

    protected long id;

    protected Date creationDate = new Date();

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }
    @Temporal(TemporalType.TIMESTAMP)
    public Date getCreationDate() {
        return creationDate;
    } 
}

and

@Entity
@Table(name="comment")
@AttributeOverride(name = "id", column = @Column(name = "comment_id"))
public class Comment extends AbstractModel{

    private User user;

    private Post post;

    private String content;
/*
 * Getters and setters
 * 
 */
    @Override
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    public long getId(){
        return id;
    }
    @ManyToOne
    @JoinColumn(name="user_id")
    public User getUser() {
        return user;
    }
    public void setUser(User user) {
        this.user = user;
    }
    @ManyToOne
    @JoinColumn(name="post_id")
    public Post getPost() {
        return post;
    }
    public void setPost(Post post) {
        this.post = post;
    }
    public String getContent() {
        return content;
    }
    public void setContent(String content) {
        this.content = content;
    }
}

Any help with this would be greatly appreciated

Wonger
  • 285
  • 6
  • 18

1 Answers1

2

You defined 'id' property twice.
Move annotations from getId() method from Comment class to AbstractModel and remove getId() method from your Comment class.

When you use proerty access you need all setters and getters for your columns setCreationDate in AbstractModel class is missing.

Bartun
  • 549
  • 2
  • 12
  • hmmm okay. Ya I've never used mapped superclass or annotation override, so i just use examples I saw from other stack overflow sites, specifically: http://stackoverflow.com/questions/5257921/hibernate-how-override-an-attribute-from-mapped-super-class and http://kruders.com/hibernate/mappedsuperclass-override-column-identifier/. So in the future, what if I want to override some method that is inherited from the parent class, am I just out of luck? – Wonger Apr 16 '15 at 14:26
  • You can use @transient annotation or use field access (annotations on fields instead of getters) – Bartun Apr 16 '15 at 18:27