So i´ve searched around and found several answers on how to set a default value in JPA, for example: here
But none of the answers are working for me.
I have the following entity:
@Data
@Entity
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Project {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@Column(unique = true)
private String name;
private Integer budget;
private String description;
private Boolean isArchived;
private LocalDate archivedDate;
@NotNull
@ManyToOne
private Customer customer;
}
I would like to add a new field date
of type LocalDate
and set its default value to LocalDate.now()
so every existing project inside the table gets LocalDate.now
as its value for the date
property.
So i tried doing it with the recommended way:
@Column(name="date")
private LocalDate date = LocalDate.now();
But it does not work, the values are set to null in the database instead of to the default value i tried to define. If i check the columndefinition inside the database, theres also no defaultvalue set. If i add the new column manually with SQL and set a default value there, everythings working as expected, i just cant get it with JPA.
I also tried playing around with columnDefinition
but couldnt get anything woring there aswell.