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

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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: stackoverflow.com/questions/5257921/… and kruders.com/hibernate/…. 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?
You can use @transient annotation or use field access (annotations on fields instead of getters)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.