I have an entity class where I want to set the default value for a column. I tried this:
@Column(name = "column", columnDefinition = "double")
@Value("#{new Double('${env.var:-0.25}')}")
@NonNull
private Double column;
Now, we cannot use @Value to set a value in the entity class. I saw some stackoverflow answers like this:
Setting default values for columns in JPA
where you set a default in the column definition, but there is no way to default and inject a env value like @Value does. Is there anything I can do to achieve this?
Thanks
Because @Value
works in spring managed bean, Declared only @Entity
might not works what you want.
How about using like this?
@Component
class SomeConfig {
@Value("${SomeVal}")
public String val;
}
@Entity
class YourEntity {
@Column(name = "column", columnDefinition = "double")
@NonNull
private Double column = SomeConfig.val;
}
Although the constraints of the DB do not change, but programmatically, the default values can be determined through external settings!