[英]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. 据我了解,当我们具有OneToMany关系时,经常使用insertable = false和updatble = false。 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.
将insertabl = false,updtable = false添加到在Question类中响应的列时,不会出现错误。 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. 当需要映射鉴别符列时,必须使用
insert="false" update="false"
映射它,因为只有Hibernate才能管理该列。 If you don't map the column, Hibernate considers it declared once, for inner purposes. 如果不映射该列,则出于内部目的,Hibernate会将其视为已声明一次。 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" . 这是因为
@DiscriminatorValue
已经映射了SP
sponsered
列,该值应始终等于“ 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 . 如果您两次将
sponsered
列映射为update / insert,则Hibernate会感到困惑,因为它不知道应将哪些值用于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. 但是在将
sponsered
列更改为只读模式(即insertabl=false
, updtable=false
)之后,hibernate知道应该使用哪些值进行更新/插入,因为只有唯一的事实来源。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.