具有布尔类型 属性 的休眠继承映射 single_table

hibernate inheritance mapping single_table with a property of boolean type

我的实体有这个层次结构:

TreeItem
LayerItem extends TreeItem
TypicalItem extends TreeItem

树项:

@Entity
@Table(name = "tree_item")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(
    name = "item_type",
    discriminatorType = DiscriminatorType.STRING
)
@DiscriminatorValue("tree_item")
public class TreeItem implements Serializable {

   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   @Basic(optional = false)
   private Long id;

   public Long getId() {
     return id;
   }

   public void setId(Long id) {
     this.id = id;
   }
   ........
}

继承类型为SINGLE_TABLE
LayerItem 有一个布尔类型的 属性 `enabled'。

图层项目:

 @Entity
@DiscriminatorValue("layer_item")
public class LayerItem extends TreeItem {

   @Column(nullable = true)
   private boolean enabled = false;

   @Column
   private String position;

   @PrePersist
   public void prePersist() {
       if (JUtil.isEmpty(this.enabled)) //We set default value in case if the value is not set yet.
       {
          this.enabled = false;
       }
   }
}

问题是,当我想坚持 TypicalItem(taht 没有 enabled 属性)时,我遇到了这个错误:

org.hibernate.PropertyAccessException: Null value was assigned to a property of primitive type setter of my.app.entity.LayerItem.enabled

我给它设置了一个默认值(false),但是没有效果。

原语boolean不能null。因此,您必须将其更改为 Boolean 对象或使其不可为空:

   @Column(nullable = true)
   private Boolean enabled = false;

或者

   @Column(nullable = false)
   private boolean enabled = false;