NubMaster
NubMaster

Reputation: 51

@Column(nullable=false) validating the null check at application level

From my understanding nullable = false(let say for column customerid) is only useful for creating schemas using hibernate and it should not do any kind of validation before persisting. My database column has no such constraint(it can take null values), while persisting the entity with customerid null, getting this error

Caused by: org.hibernate.PropertyValueException: not-null property references a null or transient value :b
    at org.hibernate.engine.internal.Nullability.checkNullability(Nullability.java:111) ~[hibernate-core-5.4.27.Final.jar:5.4.27.Final]
    at org.hibernate.engine.internal.Nullability.checkNullability(Nullability.java:55) ~[hibernate-core-5.4.27.Final.jar:5.4.27.Final]
    at 

This error started keep coming after i update my spring boot version to 2.4.2

Entity class

public class FaceIndexResponse extends AuditEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    @Column(name="customer_id",nullable = false)
    private String customerId;
    @Column(name="application_id")
    private String appid;}

Service class where i was trying to save the above entity

public IndexFacesResult handle(FaceIndexModel model) throws IOException{
    FaceIndexResponse response=new FaceIndexResponse();
    response.setAppid(model.getApplicationId);
    faceIndexResponseJpa.save(response);
 }


                                   Table "public.face_index_response"
     Column     |            Type             | Collation | Nullable |             Default              
----------------+-----------------------------+-----------+----------+----------------------------------
 id             | integer                     |           | not null | generated by default as identity
 customer_id    | character varying(64)       |           |          | 
 application_id | character varying(64)       |           |          | 

Upvotes: 5

Views: 2674

Answers (2)

Jaap
Jaap

Reputation: 724

This is due to a change in Spring Boot 2.3.x:

https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.3-Release-Notes#validation-starter-no-longer-included-in-web-starters

As of #19550, Web and WebFlux starters do not depend on the validation starter by default any more. If your application is using validation features, for example you find that javax.validation.* imports are not being resolved, you’ll need to add the starter yourself.

As of 2.3.0, the Validation starter is no longer included in the Web starters. That means that Bean Validation is no longer on the classpath, causing a change in Hibernate's behaviour. This is indicated in the Hibernate docs here:

https://docs.jboss.org/hibernate/orm/current/userguide/html_single/Hibernate_User_Guide.html#_bean_validation_options

hibernate.check_nullability (e.g. true or false) Enable nullability checking. Raises an exception if a property marked as not-null is null.

Default to false if Bean Validation is present in the classpath and Hibernate Annotations is used, true otherwise.

So, the sudden validation taking place can be resolved in two ways:

  • By setting spring.jpa.properties.hibernate.check_nullability to false as indicated in PDiddly's answer
  • Or by adding the spring-boot-starter-validation dependency to your project:
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-validation</artifactId>
    </dependency>

(or the individual dependencies for bean validation if you prefer so)

Upvotes: 5

PDiddly
PDiddly

Reputation: 39

I can't comment above (rep isn't high enough), and there's not enough to know that this will answer the question, but Take a look here: https://www.baeldung.com/hibernate-notnull-vs-nullable and https://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/appendices/Configurations.html If you have this set, Hibernate will check null before the DB.

spring.jpa.properties.hibernate.check_nullability

So I'm thinking you have that property set.

Upvotes: 2

Related Questions