我有2个类,一个用@MappedSuperclass注释的Abstract类和一个扩展该类的子类。当我运行我的程序时,我收到以下错误:
Caused by: org.hibernate.MappingException: Repeated column in mapping for entity: models.Comment column: comment_id (should be mapped with insert="false" update="false")
但我正在查看我的代码,我没有看到任何重复的列名。这是我第一次使用Mapped supper类注释,我不确定我是否做错了/使用注释。以下是我的两个课程:
@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;
}
}
和
@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;
}
}
非常感谢任何帮助
答案 0 :(得分:2)
您定义了'id'属性两次 将注释从getId()方法从Comment类移到AbstractModel,并从Comment类中删除getId()方法。
当您使用proerty访问时,您需要为您的列设置所有setter和getter。缺少AbstractModel类中的setCreationDate。