티스토리 뷰

공부

[JPA] CreationTimestamp & UpdateTimestamp

승가비 2022. 8. 10. 02:30
728x90
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;

import javax.persistence.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Entity
@Table(name = "table", uniqueConstraints = {
        @UniqueConstraint(
                name = "table__email",
                columnNames = {"email"}
        )
}
)
public class Response {
    private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
    private String email;

    private Long requestCount = 1L;

    public Long getId() {
        return id;
    }

    public String getCreatedDateTime() {
        return createdDateTime.format(formatter);
    }

    public String getUpdatedDateTime() {
        return updatedDateTime.format(formatter);
    }

    @CreationTimestamp
    private LocalDateTime createdDateTime;
    @UpdateTimestamp
    private LocalDateTime updatedDateTime;

    public Response(long id, String name, String email, long requestCount, LocalDateTime createdDateTime, LocalDateTime updatedDateTime) {
        this.id = id;
        this.name = name;
        this.email = email;
        this.requestCount = requestCount;
        this.createdDateTime = createdDateTime;
        this.updatedDateTime = updatedDateTime;
    }

    public Response() {

    }

    public String getName() {
        return name;
    }

    public String getEmail() {
        return email;
    }

    public Long getRequestCount() {
        return requestCount;
    }

    public Response(Request request) {
        this.name = request.getName();
        this.email = request.getEmail();
    }

    public boolean equals(Object obj) {
        if (obj == this) {
            return true;
        }

        if (!(obj instanceof Response)) {
            return false;
        }
        Response r = (Response) obj;

        return id.equals(r.id) &&
                name.equals(r.getName()) &&
                email.equals(r.getEmail()) &&
                requestCount.equals(requestCount) &&
                createdDateTime.equals(createdDateTime) &&
                updatedDateTime.equals(updatedDateTime);
    }
}

https://stackoverflow.com/questions/811845/setting-a-jpa-timestamp-column-to-be-generated-by-the-database

 

Setting a JPA timestamp column to be generated by the database?

In my SQL Server 2000 database, I have a timestamp (in function not in data type) column of type DATETIME named lastTouched set to getdate() as its default value/binding. I am using the Netbeans 6.5

stackoverflow.com

 

728x90

'공부' 카테고리의 다른 글

[JPA] @Query update immediately  (0) 2022.08.10
[pandas] for index, row in rche_df.iterrows():  (0) 2022.08.10
[pandas] DataFrame  (0) 2022.08.10
[spring] MockMvc  (0) 2022.08.10
[pandas] df.columns = df.columns.str.lower() - lowercase column  (0) 2022.08.10
댓글