I got the next 2 classes :
@Entity@Table(name="questions")@Inheritance(strategy = InheritanceType.SINGLE_TABLE)@DiscriminatorColumn(name="is_sponsered")@SequenceGenerator(name="id_seq")public class Question {@Id@GeneratedValue(strategy = GenerationType.SEQUENCE, generator="id_seq")protected int id;@Column(name="is_sponsered",nullable=false)protected boolean sponsered=false;....}
and a subclass :
@Entity@DiscriminatorValue("true")public class SP extends Question{public SP(String q){super(q);this.sponsered=true;}
However, I'm getting the next error :
Caused by: org.hibernate.MappingException: Repeated column in mapping for entity: SP column: is_sponsered
From what I understood insertable=false and updatble=false is often used when we have a OneToMany relation. In this case it's just inheritance. When adding the insertabl=false,updtable=false to the column sponsored in the Question class the error doesn't appear. I wanted to understand why.
Best Answer
When you need to map the discriminator column, you'll have to map it with insert="false" update="false"
because it is only Hibernate that manages the column. If you don't map the column, Hibernate considers it declared once, for inner purposes. If you declare it, that's twice, hence the error.
This is because sponsered
columns in SP
is already mapped by @DiscriminatorValue
which should always equal to "true" .
If you map sponsered
columns twice for update/insert , Hibernate will get confused as it does not know which values should it use for update /insert . But after you change sponsered
column to read-only mode (i.e. insertabl=false
,updtable=false
) , hibernate knows what values should it use for update / insert as there is only single source of truth.