Hibernate: Nullable columns and primitive fields do not mix

A null in the database cannot be assigned to an int, and the failure happens at load time.

Code
@Entity
class Order {
    int quantity;        // column is nullable -> exception on load
    Integer discount;    // correct for a nullable column
}
Output
org.hibernate.PropertyAccessException:
Could not set field value [null] value by reflection : [Order.quantity]
Advertisement

Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.

Published 2026-08-11