Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The default entity from enum

Tags:

java

spring

jpa

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?

like image 562
DeveloperApps Avatar asked Sep 13 '25 05:09

DeveloperApps


1 Answers

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;
like image 127
Andreas Avatar answered Sep 15 '25 20:09

Andreas