应该用 insert="false" update="false" 映射

Jey*_*eyJ 4 java hibernate

我得到了接下来的两节课:

@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;

....}
Run Code Online (Sandbox Code Playgroud)

和一个子类:

@Entity
@DiscriminatorValue("true")

public class SP extends Question{

public SP(String q)
{
    super(q);
    this.sponsered=true;
}
Run Code Online (Sandbox Code Playgroud)

但是,我收到下一个错误:

Caused by: org.hibernate.MappingException: Repeated column in mapping for entity: SP column: is_sponsered 
Run Code Online (Sandbox Code Playgroud)

据我了解,当我们有 OneToMany 关系时,通常会使用 insertable=false 和 updatble=false 。在这种情况下,它只是继承。将 insertabl=false,updtable=false 添加到 Question 类中赞助的列时,不会出现错误。我想明白为什么。

小智 5

当您需要映射鉴别器列时,您必须将其映射,insert="false" update="false"因为只有 Hibernate 管理该列。如果您不映射该列,Hibernate 会将其视为一次声明,用于内部目的。如果你声明它,那是两次,因此是错误的。