I am using spring with hibernate. my database is oracle. i want to update/save records with sysdate. we can insert/update sysdate using HQL. but i dont know how to insert sysdate using Criteria. please help me.
Thanks!
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
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.
There doesn’t seem to be a straightforward manner to use
sysdatein a HQL insert/update statement. You can use thecurrent_timestamp()function in HQL selects, but not in update/insert.The question is not clear regarding if you need to do this update in some isolate situations, or if this column will always have the database’s date in the moment of insertion/any update of the Entity.
In case it is isolated updates that you need: You could workaround this by defining a property in your entity that would always hold sysdate, by means of
@Formula(value="select sysdate from dual"). If this property was namedsysdate, you could achieve this by doingupdate MyEntity e set e.myDate = e.sysdate. Take into account that this approach introduces direct dependency to Oracle.In case the column is to be always updated with the database’s timestamp whenever an update on the Entity occurs, use the approach @UdoFholl pointed out in his comments, or use a trigger (if you have that level of control on the database) and annotate the field with
@Generated.