【问题标题】:Repeated column in mapping for entity exception thrown with no repeated column映射中的重复列,用于抛出没有重复列的实体异常
【发布时间】:2015-04-16 02:56:44
【问题描述】:

我有 2 个类,一个用 @MappedSuperclass 注释的抽象类和一个扩展该类的子类。当我运行我的程序时,我收到以下错误:

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

但我正在查看我的代码,但没有看到任何重复的列名。这是我第一次使用映射的晚餐类注释,我不确定我是否做错了什么/使用了注释。这是我的两个课程:

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

对此的任何帮助将不胜感激

【问题讨论】:

    标签: java hibernate jpa


    【解决方案1】:

    您定义了两次“id”属性。
    将注释从 Comment 类的 getId() 方法移动到 AbstractModel 并从 Comment 类中删除 getId() 方法。

    当您使用属性访问时,您需要所有 setter 和 getter 用于您的列 setCreationDate 在 AbstractModel 类中丢失。

    【讨论】:

    • 嗯好吧。是的,我从来没有使用过映射超类或注释覆盖,所以我只使用我从其他堆栈溢出站点看到的示例,特别是:stackoverflow.com/questions/5257921/…kruders.com/hibernate/…。那么以后,如果我想重写一些从父类继承的方法,我是不是运气不好?
    • 你可以使用@transient注解或者使用字段访问(字段上的注解而不是getter)
    猜你喜欢
    • 2014-11-02
    • 1970-01-01
    • 2012-03-11
    • 2014-06-20
    • 1970-01-01
    • 2019-02-18
    • 1970-01-01
    • 2013-08-05
    相关资源
    最近更新 更多