简体   繁体   中英

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 its just inheritance. When adding the insertabl=false,updtable=false to the column sponsered in the Question class the error doesnt appear. I wanted to understand why..

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 (ie insertabl=false , updtable=false ) , hibernate knows what values should it use for update / insert as there is only single source of truth.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM