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

 
본 웹사이트는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
대표 김성준 주소 : 경기 용인 분당수지 U타워 등록번호 : 142-07-27414
통신판매업 신고 : 제2012-용인수지-0185호 출판업 신고 : 수지구청 제 123호 개인정보보호최고책임자 : 김성준 sjkim70@stechstar.com
대표전화 : 010-4589-2193 [fax] 02-6280-1294 COPYRIGHT(C) stechstar.com ALL RIGHTS RESERVED