JPA 생성과 수정시 날짜시간 자동삽입

Hibernate generate timestamp on create and update 

Hibernate generate timestamp on create and update 

Hi, 

I am using hibernate/JPA annotations. I need to have two fields creation time and modified time in the table which will be updated by hibernate. I tried using @generated, but its not updating the fields. Any other simple approach to update the fields automatically. 
 

1
2
3
4
5
6
7
8
9
@Generated(GenerationTime.ALWAYS)
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "MODIFIED_DATE", updatable = false, insertable = false)
private Date modifiedDate;
 
@Generated(GenerationTime.INSERT)
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "CREATED_DATE", insertable = false)
private Date createdDate;

 

Otherwise

 

The following works for me: 
 

1
2
3
4
5
6
7
8
9
...
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "MODIFIED_DATE", insertable = false)
    private Date modifiedDate;
 
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "CREATED_DATE", updatable = false)
    private Date createdDate;
...



I am using Hibernate/JPA too. I've skipped the @Generated annotation, though. 

Here is the test code: 
 

1
2
3
4
5
6
7
8
9
10
...
        TestObj test = new TestObj();
        test.setValue("value");
        em.persist(test);
        em.flush();
        em.detach(test);
        test.setValue("value1");
        em.merge(test);
        em.flush();
...



...and the output: 

1
2
3
Hibernate: insert into TestObj (CREATED_DATE, value) values (?, ?)
Hibernate: select testobj0_.id as id4_0_, testobj0_.CREATED_DATE as CREATED2_4_0_, testobj0_.MODIFIED_DATE as MODIFIED3_4_0_, testobj0_.value as value4_0_ from TestObj testobj0_ where testobj0_.id=?
Hibernate: update TestObj set MODIFIED_DATE=?, value=? where id=?
 

[출처] https://coderanch.com/t/493984/databases/Hibernate-generate-timestamp-create-update

 

 

참고

 

경축! 아무것도 안하여 에스천사게임즈가 새로운 모습으로 재오픈 하였습니다.
어린이용이며, 설치가 필요없는 브라우저 게임입니다.
https://s1004games.com

What is the use of the @Temporal annotation in Hibernate?

This annotation must be specified for persistent fields or properties of type java.util.Date and java.util.Calendar. It may only be specified for fields or properties of these types.

The Temporal annotation may be used in conjunction with the Basic annotation, the Idannotation, or the ElementCollection annotation (when the element collection value is of such a temporal type.

In plain Java APIs, the temporal precision of time is not defined. When dealing with temporal data, you might want to describe the expected precision in database. Temporal data can have DATE, TIME, or TIMESTAMP precision (i.e., the actual date, only the time, or both). Use the @Temporalannotation to fine tune that.

The temporal data is the data related to time. For example, in a content management system, the creation-date and last-updated date of an article are temporal data. In some cases, temporal data needs precision and you want to store precise date/time or both (TIMESTAMP) in database table.

The temporal precision is not specified in core Java APIs. @Temporal is a JPA annotation that converts back and forth between timestamp and java.util.Date. It also converts time-stamp into time. For example, in the snippet below, @Temporal(TemporalType.DATE) drops the time value and only preserves the date.

@Temporal(TemporalType.DATE)
private java.util.Date creationDate;

As per javadocs,

Annotation to declare an appropriate {@code TemporalType} on query method parameters. Note that this annotation can only be used on parameters of type {@link Date} with default TemporalType.DATE

[Information above collected from various sources]

 

[출처] https://stackoverflow.com/questions/25333711/what-is-the-use-of-the-temporal-annotation-in-hibernate

 
본 웹사이트는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
번호 제목 글쓴이 날짜 조회 수
264 SpringBoot JPA 예제(1:N, 양방향) 졸리운_곰 2018.12.31 36
263 SpringBoot JPA 예제(결합 인덱스) 졸리운_곰 2018.12.31 63
262 SpringBoot JPA 예제(@ManyToOne, 단방향) 졸리운_곰 2018.12.31 165
261 SpringBoot JPA 예제(@OneToMany, 단방향) 졸리운_곰 2018.12.31 61
260 JPA OneToOne? 자바 jpa join 졸리운_곰 2018.12.31 34
259 Spring Data JPA 연관관계 매핑하는 방법 졸리운_곰 2018.12.31 38
258 Spring MVC 주요 애노테이션(Annotation)정리 file 졸리운_곰 2018.12.29 95
257 스프링/스프링부트 애노테이션(Annotation) 정리 졸리운_곰 2018.12.29 113
256 Spring Framework - annotation 정리 #1 졸리운_곰 2018.12.29 141
255 [Spring] Annotation 정리 졸리운_곰 2018.12.29 65
» JPA 생성과 수정시 날짜시간 자동삽입 Hibernate generate timestamp on create and update 졸리운_곰 2018.12.13 1888
253 JPA로 insert / update시 날짜시간 자동으로 설정 How to create an auto-generated Date/timestamp field in a Play! / JPA? 졸리운_곰 2018.12.13 796
252 Spring Data repository with empty IN clause. 졸리운_곰 2018.11.16 74
251 Spring Data JPA Tutorial: Introduction to Query Methods 졸리운_곰 2018.11.16 75
250 JPA OrderColumn 에서의 정렬 order by 졸리운_곰 2018.11.16 587
249 Jpa @elementcollection 의 데이터 정렬 @orderby : JPA - Using @OrderBy Annotation file 졸리운_곰 2018.11.16 723
248 순수 Java Application 코드로 Restful api 호출 졸리운_곰 2018.10.10 244
247 Springboot 에서 Querydsl 사용하기 졸리운_곰 2018.09.18 159
246 Springboot 에서 DATA-JPA(Hibernate) 사용하기[3] - JOIN file 졸리운_곰 2018.09.18 77
245 Springboot 에서 DATA-JPA(Hibernate) 사용하기[2] - Entity, Repository, CRUD file 졸리운_곰 2018.09.18 87
대표 김성준 주소 : 경기 용인 분당수지 U타워 등록번호 : 142-07-27414
통신판매업 신고 : 제2012-용인수지-0185호 출판업 신고 : 수지구청 제 123호 개인정보보호최고책임자 : 김성준 sjkim70@stechstar.com
대표전화 : 010-4589-2193 [fax] 02-6280-1294 COPYRIGHT(C) stechstar.com ALL RIGHTS RESERVED