====== JPA Date Time ====== ===== Joda Time ===== * [[https://gist.github.com/unizune/7872436|Spring Data JPA & Joda-Time]] * MySQL에 millisecond 단위가 없는데, 데이터를 넣을 때 ms를 넣으면 무조건 올림이 돼버리는 현상 발생. * Date 필드의 길이를 6으로 지정해서 애초에 잘라버리면 될 듯(''columnDefinition = "datetime(6)"''). * Jodatime 설정 [[http://www.springbyexample.org/examples/spring-data-jpa-jpa-config.html|2. JPA Configuration]] 1 * http://jadira.sourceforge.net/ ===== 생성/수정 시간 (creation datetime, update datetime) ===== ==== Hibernate 의 CreationTimestamp, UpdateTimestamp ==== * [[java:hibernate:valuegenerationtype|Hibernate @ValueGenerationType]] 을 이용하여 자동으로 생성/수정시간 생성 * [[https://thoughts-on-java.org/persist-creation-update-timestamps-hibernate/|How to persist creation and update timestamps with Hibernate]] * [[java:hibernate|Hibernate]]의 [[https://docs.jboss.org/hibernate/orm/current/javadocs/org/hibernate/annotations/CreationTimestamp.html|@CreationTimestamp]], [[https://docs.jboss.org/hibernate/orm/current/javadocs/org/hibernate/annotations/UpdateTimestamp.html|@UpdateTimestamp]]를 사용하는 것이 좋겠다. * [[https://hibernate.atlassian.net/browse/HHH-11096|[HHH-11096] @CreationTimestamp doesn't works with @Column(nullable=false) - Hibernate JIRA]] ''nullable=false''일때 오류 발생하는 버그 있음. 5.4에서 해결된 듯. @Column @CreationTimestamp private LocalDateTime createDateTime; @Column @UpdateTimestamp private LocalDateTime updateDateTime; ==== DB DEFAULT 사용시 ==== * [[database:mysql|MySQL]]에서 DEFAULT 값 사용시 @Generated(GenerationTime.INSERT) @Column private LocalDateTime createDateTime; @Generated(GenerationTime.ALWAYS) @Column private LocalDateTime updateDateTime; created_at DATETIME(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), modified_at DATETIME(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), * 해당 필드에 ''insertable=false, updatable=false'' 설정을 해준다. * [[java:hibernate:generated|Hibernate @Generated]] 로 필드를 지정해야한다. * [[https://www.logicbig.com/tutorials/java-ee-tutorial/jpa/refreshing.html|JPA + Hibernate - Refreshing an Entity Instance]] ===== Java 8 DateTime ===== * [[http://www.thoughts-on-java.org/persist-localdate-localdatetime-jpa/|How to persist LocalDate and LocalDateTime with JPA]] * [[https://github.com/marschall/threeten-jpa|ThreeTen JPA converter]] * https://bitbucket.org/montanajava/jpaattributeconverters * [[http://jadira.sourceforge.net/usertype-userguide.html|Jadira Java 8 DateTime/JodaTime Hibernate usertype]] ===== Java 8 Year Month ===== * [[https://vladmihalcea.com/java-yearmonth-jpa-hibernate/|How to map the Java YearMonth type with JPA and Hibernate]] * [[https://vladmihalcea.com/java-time-year-month-jpa-hibernate/|How to map java.time.Year and java.time.Month with JPA and Hibernate]] * [[https://thoughts-on-java.org/hibernate-tips-how-to-map-java-time-year-with-jpa-and-hibernate/|Hibernate Tips: How to Map java.time.Year with JPA and Hibernate]] * ''YearMonth'', ''Year'', ''Month'' 는 ''AttributeConverter'' 만들어야 함. ===== 참조 ===== * [[https://www.baeldung.com/hibernate-date-time|Hibernate – Mapping Date and Time | Baeldung]] * [[https://vladmihalcea.com/how-to-store-date-time-and-timestamps-in-utc-time-zone-with-jdbc-and-hibernate/|How to store date, time, and timestamps in UTC time zone with JDBC and Hibernate - Vlad Mihalcea]]