'DB default column value null while saving with Spring Data JPA
I have a JPA entity with the following fields:
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "modified_by_user_id")
private User modifiedByUser;
@Convert(converter = LocalDatetimeAttributeConverter.class)
private LocalDateTime modifiedDatetime;
"modified_by_user_id" and "modified_datetime" columns have a default value in the DB. If I do a manual insert into, the columns will get their default values without being explicitly specified.
However, while saving the entity with Spring Data JPA, the default values is not being calculated and it shows null in the DB
Contributor contributor = new Contributor();
contributor.setName("a");
contributorRepository.save(contributor);
Why is not the default values being calculated when I save the entity with Spring Data JPA?
Solution 1:[1]
when you create contributor using
Contributor contributor = new Contributor();
it will have null as value of modifiedByUser and modifiedDatetime (unless constructor sets those fields) so persisting that object saves those nulls to db
maybe simply marking those fields not null in Entity and database will work
but I think better aproach is setting default values in aplication (for example in constructor) , instead database
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | lukwas |
