Set creation and update time using Hibernate in Xml mappings

I am using Hibernate with Xml mappings . I have an object that has two creationDate and updateDate fields of type timestampthat must be filled with the current UTC time when the object is saved and updated. I am aware of the existence of annotations @PrePersistand @PreUpdate, but I do not know, how to use their equivalent in my Xml comparisons.

Again, I was wondering if Hibernate could somehow support the update and creation time.

thank

+5
source share
2 answers

@PrePersist @PreUpdate, , XML-.

Hibernate3 - , PreInsertEvent, PreUpdateEvent SaveOrUpdateEvent (. org.hibernate.event ), /.

interceptor, Session -scoped SessionFactory -scoped, createDate updateDate onSave(...), updateDate onFlushDirty(...).


: , , ( ) - .

generated timestamp strong > , creationDate updateDate, :

<class name="MyEntity" table="MY_ENTITY">
  <id .../>
  <timestamp name="createDate" generated="insert" ... />
  <timestamp name="updateDate" generated="always" ... />
  ...
</class>

. .

1

, timestamp generatead, . , , , timestamp , , , createDate updateDate ( , timestamp).

- , timestamp:

<class name="MyEntity" table="MY_ENTITY">
  <id .../>
  <property name="createDate" update="false" insert="false" generated="insert" ... />
  <property name="updateDate" update="false" insert="false" generated="always" ... />
  ...
</class>

updateDate. createDate current_timestamp . , , ...

2

1, updateDate (, , timestamp):

<class name="MyEntity" table="MY_ENTITY">
  <id .../>
  <timestamp name="updateDate" ... />
  <property name="createDate" update="false" insert="false" generated="insert" ... />
  ...
</class>

, 1 createDate, .

3

. ...

+9

Hibernate, -, , , <timestamp> . java.util.Date, new Date().

:

public class MyEntity {
  ...
  private Date updateDate;
  ...
}

<class name="MyEntity" table="MY_ENTITY">
  <id .../>
  <timestamp name="updateDate" access="field" column="UPDATE_DATE"/>
  ...
</class>

, timestamp id .

FYI timestamp.

+1

All Articles