Skip to content
Advertisement

should be mapped with insert=”false” update=”false”

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.

Advertisement

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.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement