When to use @Basic (optional = false) in JPA 2.0

I have inheritance with displaying a single table in JPA, Say Class A and B extends some abstract object, so I need to make columns from A and B nullified at the end of DB, but if someone tries to save A, then all fields of A should be not null, and I want to ensure its execution using code. Can I use the following code for this -

@Entity @DiscriminatorValue("1") public Class A extends SomeAbstractEntity{ @Basic(optional = false) private String nameOfA; } 

I read this answer @Basic (optional = false) vs @Column (nullable = false) in JPA and thought it might be achievable, but I wanted to know what the best way is.

+6
java hibernate
source share
1 answer

This is pretty funny, but it seems that in this case (with inheritance per table) @Basic(optional = false) does not apply Hibernate (although in other cases it works as expected).

If so, the only option to enforce this rule is to use the @NotNull constraint from the JSR-303 Bean Validation. JSR-303 seamlessly integrates with JPA 2.0, so restrictions are automatically checked when entities persist, see Hibernate Validator .

+2
source share

All Articles