@CreationTimeStamp and @UpdateTimeStamp not working with LocalDate, only have null values

1.2k views Asked by At

I have the following code in my entity class

    @CreationTimestamp
    @Column(updatable = false)
    private LocalDate dateAdded;
    @JsonProperty("last_edited")
    @UpdateTimestamp
    @Column(updatable = false)
    private LocalDate lastEdited;

However, when it comes to posting to the database, I am only getting null values for the two columns.

enter image description here enter image description here

I am using Java 18 and spring-boot-starter-jpa

Edit: haven't figured out what's causing the problem... going to add the local date in the service instead for now...

        var sql = """
                INSERT INTO public.recipe ("name", "instructions", "date_added", "last_edited")
                VALUES (?, ?, ?, ?)
                """;
        LocalDateTime localDateTime = LocalDateTime.now();
        recipeDTO.setDateAdded(localDateTime);
        recipeDTO.setLastEdited(localDateTime);
        jdbcTemplate.update(
                sql,
                recipeDTO.getName(),
                recipeDTO.getInstructions(),
                localDateTime,
                localDateTime
        );
1

There are 1 answers

0
Christian Beikov On

You have to remove the updatable = false part as Hibernate will exclude assignments to these columns in update statements. So even if you do update the entity fields, the data will not be updated in the database.