English 中文(简体)
Last update timestamp with JPA
原标题:
  • 时间:2009-11-17 00:43:29
  •  标签:
  • java
  • jpa

I m playing around a bit with JPA(Eclipselink to be specific). The below entity have a timestamp that s supposed to reflect whenever that entity was last updated.

What are the strategies for making JPA update that timestamp automatically every time this entity is changed ?

How would I go about if I also want a creation timestamp, only set when the entity is first persisted, never allowed to be changed again ?

 @Entity
 public class Item implements Serializable {
     private static final long serialVersionUID = 1L;
     private Integer id;
     private String note;


    public Item () {
    }


    @Id
    @GeneratedValue(strategy=SEQUENCE)
    @Column(unique=true, nullable=false)
    public Integer getId() {
        return this.id;
    }

    public void setId(Integer id) {
        this.id = id;
    }


    @Column(length=255)
    @Column(nullable=false)
    public String getNote() {
        return this.note;
    }

    public void setNote(String note) {
        this.note = note;
    }

    @Column(nullable=false)
    public Timestamp getUpdated() {
        return this.updated;
    }

    public void setUpdated(Timestamp updated) {
        this.updated = updated;
    }


}
最佳回答

Use @PrePersist and @PreUpdate annotations and write your own event listener.

Take a look at this answer for details. It s tagged as Hibernate but is applicable to any JPA provider.

问题回答

if you are using mysql, I think you can do the following to disable the timestamps from being updated from entity

@Column(name = "lastUpdate", updatable= false, insertable=false)
@Temporal(TemporalType.TIMESTAMP)
private Date lastUpdate;

for oracle you time have to set the timestamps from the entity using @PreUpdate annotation as explained above.

Actually, surajz answer works. Consider a scenario where you are tracking 2 timestamps. You want to manually update them via the entity. You d need to set one to updateble=false (the one you do not want updated when the record is updated), and leave the other free for updates.

That s what worked for me.

In Entity class

@Column
@UpdateTimestamp
private Timestamp updatedOn

@Column
@CreationTimestamp
private Timestamp createdOn

it will update automatically when any change in entity

Supported attribute types

java.time.LocalDate (since Hibernate 5.2.3)
java.time.LocalDateTime (since Hibernate 5.2.3)
java.util.Date
java.util.Calendar
java.sql.Date
java.sql.Time
java.sql.Timestamp

Example :

@Entity
public class MyEntity {
 
    @Id
    @GeneratedValue
    private Long id;
 
    @CreationTimestamp
    private Timestamp createdOn;
 
    @UpdateTimestamp
    private Timestamp updatedOn;
 
}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签