- 전체
- JAVA 일반
- JAVA 수학
- JAVA 그래픽
- JAVA 자료구조
- JAVA 인공지능
- JAVA 인터넷
- Java Framework
- Java GUI (AWT,SWING,SWT,JFACE)
- SWT and RCP (web RAP/RWT)[eclipse], EMF
JAVA 자료구조 JPA로 insert / update시 날짜시간 자동으로 설정 How to create an auto-generated Date/timestamp field in a Play! / JPA?
2018.12.13 13:59
JPA로 insert / update시 날짜시간 자동으로 설정
How to create an auto-generated Date/timestamp field in a Play! / JPA?
I'd like to add a Date/DateTime/Timestamp field on an entity object, that will be automatically created when the entity is created/persisted and set to "now", never to be updated again.
Other use cases included fields that were always updated to contain the last modification date of the entity.
I used to achieve such requirements in the mysql schema.
What's the best way to do this in Play! / JPA?
There is a code snippet that you can adapt to achieve what your want. Take a look:
// Timestampable.java
package models;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Version;
import play.db.ebean.Model;
@MappedSuperclass
public class Timestampable extends Model {
@Id
@GeneratedValue
public Long id;
@Column(name = "created_at")
public Date createdAt;
@Column(name = "updated_at")
public Date updatedAt;
@Version
public int version;
@Override
public void save() {
createdAt();
super.save();
}
@PrePersist
void createdAt() {
this.createdAt = this.updatedAt = new Date();
}
@PreUpdate
void updatedAt() {
this.updatedAt = new Date();
}
[출처] https://stackoverflow.com/questions/8202154/how-to-create-an-auto-generated-date-timestamp-field-in-a-play-jpa
본 웹사이트는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.