should be mapped with insert="false" update="false"
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.
JeyJ
Updated on December 08, 2022Comments
-
JeyJ over 1 year
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.
-
JeyJ over 5 yearsbut both my subclass and my parent class should insert that value, in case it is the subclass the value should be true and in case it is the parent it should be false.