1

I have an Hibernate entity which has a java.util.Date property. Instead of setting the property value to new Date() on my entity and then saving with Hibernate, I would like to make the database set the date through a SYSDATE() function. I want this because I can't rely on the date set by the client, because it is a swing app. I can only trust on the time taken from the database.

I have thought of some options, but both seem quite poor:

  • Ask the database for the current time, set it on my object and then save. (expensive)
  • Create an Hibernate insert statement and pass the parameters manually. (subverts the purpose of Hibernate)

1 Answer 1

1

Hibernate allows you to have DB generated columns, check the Generated properties Hibernate documentation.

You database should have a DEFAULT SYSDATE configuration.

Check this example for a complete solution.

Sign up to request clarification or add additional context in comments.

1 Comment

Generated properties are just a way to make Hibernate get the generated value for you. The DEFAULT SYSDATE made the trick I wanted. I used the insert="false" but disabled the generated="insert" because I don't need the generated value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.