Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 743535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T08:52:31+00:00 2026-05-14T08:52:31+00:00

I’m using Hibernate with Xml mappings. I have an entity that has two fields

  • 0

I’m using Hibernate with Xml mappings. I have an entity that has two fields creationDate and updateDate of type timestamp, that have to be filled with the current UTC time when the entity is persisted and updated.
I know about the existence of the @PrePersist and @PreUpdate annotations, but i don’t know how to use their equivalent in my Xml mappings.

Again, i was wondering if Hibernate somehow supports natively the update and creation time set.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T08:52:31+00:00Added an answer on May 14, 2026 at 8:52 am

    I know about the existence of the @PrePersist and @PreUpdate annotations, but i don’t know how to use their equivalent in my Xml mappings.

    The Hibernate3 event architecture provides something equivalent and you could register listeners for PreInsertEvent, PreUpdateEvent or SaveOrUpdateEvent (see the org.hibernate.event package for a full list) to set and update the create/update dates.

    Another approach would be to use an interceptor, either Session-scoped or SessionFactory-scoped and to set both createDate and updateDate in onSave(...), update the updateDate in onFlushDirty(...).


    Update: I’m leaving my original suggestions below but I think that the right approach (should have been my initial answer) is to use an interceptor or the event architecture.

    You could use the generated attribute of the timestamp to get creationDate and updateDate generated by the database on insert and on insert and update respectively:

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

    Refer to the section on generated properties for full details.

    Option 1

    It appears that timestamp doesn’t support generatead so my suggestion won’t work. Nevertheless, having read the documentation more carefully, my understanding is that timestamp is an alternative to versioning and I don’t think that it’s an appropriate choice for fields like createDate and updateDate (it may work for the later but that’s not what timestamp is for).

    So I would actually still use generated properties but with simple properties instead of 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>
    

    At the database level, this would require using a trigger for updateDate column. For the createDate column, using something like current_timestamp as default value would work nicely. But triggers are maybe not wanted…

    Option 2

    To avoid the trigger of the Option 1, a variation would be to use updateDate for versioning (and thus map it as timestamp):

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

    Same approach as Option 1 for createDate, use a default value at the database level.

    Option 3

    See the top of this answer…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have thousands of HTML files to process using Groovy/Java and I need to
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.