Hibernate中抽象类的继承

Hibernate中抽象类的继承,hibernate,inheritance,annotations,Hibernate,Inheritance,Annotations,我有以下情况,一篇文章可以有几种类型的内容(例如文本、图像、源代码等)。因此,我设计了这个简单的类结构: 这是摘要ArticleContent类的来源: @Entity @Table( name = "ARTICLE_CONTENTS" ) @Inheritance( strategy = InheritanceType.TABLE_PER_CLASS ) public abstract class ArticleContent extends AbstractEntity { priva

我有以下情况,一篇文章可以有几种类型的内容(例如文本、图像、源代码等)。因此,我设计了这个简单的类结构:

这是摘要ArticleContent类的来源:

@Entity
@Table( name = "ARTICLE_CONTENTS" )
@Inheritance( strategy = InheritanceType.TABLE_PER_CLASS )
public abstract class ArticleContent extends AbstractEntity {

  private Article article;

  @ManyToOne
  @JoinColumn( name = "ARTICLE_ID", nullable = false, updatable = false )
  public Article getArticle() {
    return article;
  }

  public void setArticle( Article article ) {
    this.article = article;
  }

  @Column( name = "CONTENT", columnDefinition = "TEXT", nullable = false )
  public abstract String getContent();

  public abstract void setContent( String content );

}
getContent()
setContent()
方法标记为抽象,因为它们将返回实际显示的内容(例如纯文本,
,…)

我从实现TextArticleContent类开始。此类仅将内容存储在字符串中:

@Entity
@Table( name = "TEXT_ARTICLE_CONTENTS" )
@AttributeOverrides( { @AttributeOverride( name = "content", column = @Column( name = "CONTENT" ) ) } )
public class TextArticleContent extends ArticleContent {

  private String content;

  @Override
  public String getContent() {
    return content;
  }

  @Override
  public void setContent( String content ) {
    this.content = content;
  }
}

这是我收到的错误输出:

Caused by: org.hibernate.MappingException:
Repeated column in mapping for entity: com.something.model.TextArticleContent column: 
  CONTENT (should be mapped with insert="false" update="false")
虽然错误消息给了我应该做什么的建议(
应该用insert=“false”update=“false”
)进行映射,但老实说,由于我刚刚开始使用Hibernate,我不知道如何处理它

更新:解决方案 这个问题的解决方案是,我需要更改
getContent()
方法的
@Column
注释。正确的注释如下所示:

@Column( name = "CONTENT", columnDefinition = "TEXT", nullable = false, insertable = false, updatable = false )
public abstract String getContent();
我不得不添加可插入的可更新的,这基本上意味着异常中的提示并不完全正确

此外,我还需要更改抽象ArticleContent类的继承策略。 正确的继承注释是:

@Inheritance( strategy = InheritanceType.SINGLE_TABLE )
出于美观的原因,现在可以将TextArticleContent类中的
@Table
注释替换为:

@DiscriminatorValue( "TEXT_ARTICLE_CONTENT" )

这会将文章内容表中的鉴别器值更改为文本文章内容

尝试更改文章内容

    @Column( name = "CONTENT", columnDefinition = "TEXT", nullable = false )
  public abstract String getContent();

更新


已更改为可插入、可更新。

我无法使用插入和更新,必须将其更改为可插入和可更新。谢谢你的提示!但是我检索到了以下异常:无法将标识列键生成与映射用于:com.something.model.TextArticleContent。解决方案:我不得不将InheritanceFosterategy更改为InheritanceType.SINGLE_TABLE。谢谢!我根据您的回答编辑了我的问题以帮助他人:)
        @Column( name = "CONTENT", columnDefinition = "TEXT", nullable = false,
insertable = false, updatable = false )
      public abstract String getContent();