I have this hierarchy for my Entities:
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;
}
........
}
The inheritance type is SINGLE_TABLE
.
LayerItem
has a property `enabled' of boolean type.
@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;
}
}
}
The problem is that when I want to persist a TypicalItem
(taht does not have the enabled
property) I face this error:
org.hibernate.PropertyAccessException: Null value was assigned to a property of primitive type setter of my.app.entity.LayerItem.enabled
I've set a default value (false) for it, but it has no effect.
a primitve boolean
can not be null
. So you have to change it to a Boolean
object or make it not nullable:
@Column(nullable = true)
private Boolean enabled = false;
Or
@Column(nullable = false)
private boolean enabled = false;