- 전체
- JAVA 일반
- JAVA 수학
- JAVA 그래픽
- JAVA 자료구조
- JAVA 인공지능
- JAVA 인터넷
- Java Framework
- Java GUI (AWT,SWING,SWT,JFACE)
- SWT and RCP (web RAP/RWT)[eclipse], EMF
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
참고
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 Id
annotation, 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 @Temporal
annotation 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
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.