사용자 도구

사이트 도구


java:jpa:datetime

차이

문서의 선택한 두 판 사이의 차이를 보여줍니다.

차이 보기로 링크

양쪽 이전 판 이전 판
다음 판
이전 판
java:jpa:datetime [2019/01/14 17:20]
kwon37xi
java:jpa:datetime [2021/10/07 21:59] (현재)
kwon37xi [DB DEFAULT 사용시]
줄 13: 줄 13:
 ===== 생성/수정 시간 (creation datetime, update datetime) ===== ===== 생성/수정 시간 (creation datetime, update datetime) =====
 ==== Hibernate 의 CreationTimestamp, UpdateTimestamp ==== ==== 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]]   * [[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]]를 사용하는 것이 좋겠다.   * [[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에서 해결된 듯.
 <code java> <code java>
 @Column @Column
줄 29: 줄 29:
 ==== DB DEFAULT 사용시 ==== ==== DB DEFAULT 사용시 ====
   * [[database:mysql|MySQL]]에서 DEFAULT 값 사용시   * [[database:mysql|MySQL]]에서 DEFAULT 값 사용시
 +
 +<code java>
 +@Generated(GenerationTime.INSERT) 
 +@Column
 +private LocalDateTime createDateTime;
 +
 +@Generated(GenerationTime.ALWAYS)  
 +@Column
 +private LocalDateTime updateDateTime;
 +</code>
 <code sql> <code sql>
 created_at      DATETIME(6)  NOT NULL DEFAULT CURRENT_TIMESTAMP(6), created_at      DATETIME(6)  NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
줄 34: 줄 44:
 </code> </code>
   * 해당 필드에 ''insertable=false, updatable=false'' 설정을 해준다.   * 해당 필드에 ''insertable=false, updatable=false'' 설정을 해준다.
-  * ''EntityManager.persist(), merge()'' 이후 다시 로드 하려면 ''EntityManager.refresh(entity)''를 사용다. 안그러면 엔티티 저장직후에 바로 사용하고자 할 때 생성/수정 시각 필드가 null이 된다.+  * [[java:hibernate:generated|Hibernate @Generated]] 로 드를 지정해야한다.
   * [[https://www.logicbig.com/tutorials/java-ee-tutorial/jpa/refreshing.html|JPA + Hibernate - Refreshing an Entity Instance]]   * [[https://www.logicbig.com/tutorials/java-ee-tutorial/jpa/refreshing.html|JPA + Hibernate - Refreshing an Entity Instance]]
  
줄 43: 줄 53:
   * https://bitbucket.org/montanajava/jpaattributeconverters   * https://bitbucket.org/montanajava/jpaattributeconverters
   * [[http://jadira.sourceforge.net/usertype-userguide.html|Jadira Java 8 DateTime/JodaTime Hibernate  usertype]]   * [[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://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]]   * [[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]]
java/jpa/datetime.1547454043.txt.gz · 마지막으로 수정됨: 2019/01/14 17:20 저자 kwon37xi