spring-projects / spring-data-jpa

Simplifies the development of creating a JPA-based data access layer.
https://spring.io/projects/spring-data-jpa/
Apache License 2.0
2.98k stars 1.41k forks source link

Creation Timestamp updating in result but when I get from database nothing changes. #3405

Closed Lazizkhan1 closed 6 months ago

Lazizkhan1 commented 6 months ago

https://github.com/spring-projects/spring-data-jpa/assets/71133975/dd1ece88-f097-4012-9ee1-08a9483a09f2

I thought the result after updating shouldn't be changed into.


import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.SourceType;
import org.hibernate.annotations.UpdateTimestamp;

import java.math.BigDecimal;
import java.time.ZonedDateTime;
import java.util.UUID;

@Builder
@Data
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "additional_expenses")
@Entity
public class AdditionalExpenses {
    @Id
    @GeneratedValue(strategy = GenerationType.UUID)
    private UUID id;

    @Column(nullable = false)
    private UUID branchId;

    @Column(nullable = false, precision = 15, scale = 2)
    private BigDecimal amount;

    @Column(nullable = false)
    private String reason;

    @Temporal(TemporalType.TIMESTAMP)
    private ZonedDateTime expenseDate;

    @Column(updatable = false)
    @Temporal(TemporalType.TIMESTAMP)
    @CreationTimestamp(source = SourceType.VM)
    private ZonedDateTime createdAt;

    @Temporal(TemporalType.TIMESTAMP)
    @UpdateTimestamp(source = SourceType.VM)
    private ZonedDateTime updatedAt;

    @Temporal(TemporalType.TIMESTAMP)
    private ZonedDateTime deletedAt;
} 
schauder commented 6 months ago

A video is not a proper issue description. I'm not going to watch this over an over again until I'm sure I understand what you want.

In case you are assuming the created timestamp shouldn't get updated when you update it explicitely, you are wrong. Make it insert only if you want that behaviour.