I have property in Enum:
@Basic
@Column(name = "payment_status", columnDefinition = "varchar(32) default 'ENTERED'", nullable = false)
@Enumerated(EnumType.STRING)
private PaymentStatus paymentStatus;
I want to get the default value for a field from enum
I have error:
org.hibernate.PropertyValueException: not-null property references a null or transient value
The field cannot be null
The error is when I want to create an object and save in the database without entering this field (PaymentStatus)
EDIT:
@Basic
@ColumnDefault(value = "ENTERED")
@Column(name = "payment_status", nullable = false)
@Enumerated(EnumType.STRING)
private PaymentStatus paymentStatus = PaymentStatus.ENTERED;
Why is it not working?
default 'ENTERED'
tells the database to use value 'ENTERED'
if the column is not included in the INSERT
statement. Since the column is in the class, JPA will always include it in the INSERT
statement.
To make JPA fill in the default value, simply assign it with an initializer, so it has that value until replaced by you (calling setter method), or replaced from database (when reading from there).
private PaymentStatus paymentStatus = PaymentStatus.ENTERED;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With