hibernate mysql 时间_使用Hibernate和MySQL创建时间戳和最后更新时间戳
慕斯卡3215842
利用本文中的資源以及從不同來源獲得的左右信息,我提出了這個優(yōu)雅的解決方案,創(chuàng)建了以下抽象類import java.util.Date;import javax.persistence.Column;import javax.persistence.MappedSuperclass;import javax.persistence.PrePersist;import javax.persistence.PreUpdate;import javax.persistence.Temporal;import javax.persistence.TemporalType;@MappedSuperclasspublic abstract class AbstractTimestampEntity {? ? @Temporal(TemporalType.TIMESTAMP)? ? @Column(name = "created", nullable = false)? ? private Date created;? ? @Temporal(TemporalType.TIMESTAMP)? ? @Column(name = "updated", nullable = false)? ? private Date updated;? ? @PrePersist? ? protected void onCreate() {? ? updated = created = new Date();? ? }? ? @PreUpdate? ? protected void onUpdate() {? ? updated = new Date();? ? }}并讓您的所有實體對其進(jìn)行擴(kuò)展,例如:@Entity@Table(name = "campaign")public class Campaign extends AbstractTimestampEntity implements Serializable {...}
總結(jié)
以上是生活随笔為你收集整理的hibernate mysql 时间_使用Hibernate和MySQL创建时间戳和最后更新时间戳的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux要学git吗,git学习一 基
- 下一篇: mysql count distinct