Inheritance with abstract classes in Hibernate -
i have following situation, article can have several types of content (e.g. text, images, sourcecode, ...). therefore designed simple class structure:
this source of abstract articlecontent class:
@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 ); }
the getcontent()
, setcontent()
methods marked abstract, because return content displayed (e.g. plain text, <img src="..." />
, ...).
i started implementation of textarticlecontent class. class stores content in string:
@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; }
}
this error output receive:
caused by: org.hibernate.mappingexception: repeated column in mapping entity: com.something.model.textarticlecontent column: content (should mapped insert="false" update="false")
though error messages gives me advice should (should mapped insert="false" update="false"
), honestly, started using hibernate have no idea how handle it.
update: solution solution problem needed change @column
annotation of getcontent()
method. correct annotation looks this:
@column( name = "content", columndefinition = "text", nullable = false, insertable = false, updatable = false ) public abstract string getcontent();
i had add insertable , updatable, means tip in exception not correct.
furthermore, needed change inheritancestrategy abstract articlecontent class. correct @inheritance
annotation is:
@inheritance( strategy = inheritancetype.single_table )
for cosmetic reasons, @table
annotation in textarticlecontent class can replaced with:
@discriminatorvalue( "text_article_content" )
this changes discriminator value in article_contents table text_article_content.
try change in articlecontent
@column( name = "content", columndefinition = "text", nullable = false ) public abstract string getcontent();
to
@column( name = "content", columndefinition = "text", nullable = false, insertable = false, updatable = false ) public abstract string getcontent();
update.
changed insertable, updatable.
Comments
Post a Comment