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

 
본 웹사이트는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
번호 제목 글쓴이 날짜 조회 수
289 Blockchain Implementation With Java Code file 졸리운_곰 2019.06.16 164
288 Java 코드로 이해하는 블록체인(Blockchain) 졸리운_곰 2019.06.16 180
287 MessageBox Example : Dialog « SWT JFace Eclipse « Java file 졸리운_곰 2019.06.16 108
286 Add Table Selection Listener and Get Selected TableItem : Table Event « SWT « Java Tutorial file 졸리운_곰 2019.06.16 159
285 Building and delivering a table editor with SWT/JFace file 졸리운_곰 2019.06.16 180
284 Demonstrates CellEditors : Table « SWT JFace Eclipse « Java file 졸리운_곰 2019.06.16 85
283 Demonstrates TableViewers : Table « SWT JFace Eclipse « Java file 졸리운_곰 2019.06.16 129
282 Java and JMX - Building Manageable Systems secret 졸리운_곰 2019.05.26 0
281 Single_Sourcing_RAP_RCP_en.pdf file 졸리운_곰 2019.05.15 95
280 Rich client platform 설명 및 배우기 참고 졸리운_곰 2019.05.15 155
279 Rich Ajax Platform, Part 1: 소개 file 졸리운_곰 2019.05.15 196
278 또 하나의 크로스 플랫폼: Eclipse RAP file 졸리운_곰 2019.05.15 196
277 Eclipse 4 RCP 튜토리얼(완료) file 졸리운_곰 2019.05.14 794
276 [JPA] 쿼리메서드 : 쿼리 연습 조회(findBy..) , 페이징처리 졸리운_곰 2019.03.24 1670
275 스프링 데이터 JPA 레퍼런스 번역 file 졸리운_곰 2019.03.24 1046
274 JPA 개념, class05 JPA 환경설정 졸리운_곰 2019.03.24 110
273 [자바코드] 고유값인 UUID, GUID 생성하기 졸리운_곰 2019.02.27 281
272 [JPA] 복합키 졸리운_곰 2019.02.26 98
271 Spring Batch Multithreading Example file 졸리운_곰 2019.01.31 126
270 Spring batch를 Parallel로 돌려보자 졸리운_곰 2019.01.31 158
대표 김성준 주소 : 경기 용인 분당수지 U타워 등록번호 : 142-07-27414
통신판매업 신고 : 제2012-용인수지-0185호 출판업 신고 : 수지구청 제 123호 개인정보보호최고책임자 : 김성준 sjkim70@stechstar.com
대표전화 : 010-4589-2193 [fax] 02-6280-1294 COPYRIGHT(C) stechstar.com ALL RIGHTS RESERVED