I have a column in DB with default value as sysdate. I’m looking for a way to get that default value inserted while I’m not giving anything to corresponding property on app side.
By the way, I’m using annotation-based configuration.
Any advice?
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.
The reason why the date column gets a
nullvalue when inserting, even though it is defined asdefault SYSDATEdbms-side, is that thedefaultvalue for a column is only used if the column is not given a value in the query. That means it must not appear in theINSERT INTOsentence, even if it has been givennull.If you want to use the
default SYSDATEon the DBMS side, you should configure the@Columnwithinsertable=falsein order to get the column out of your SQLINSERTs.Take into account that this approach will always ignore the value you provide to the property in your app when creating the entity. If you really want to be able to provide the date sometimes, maybe you should consider using a DB trigger to set the value instead of a default value.
There’s an alternative to using the
default SYSDATEdefinition of the DBMS. You could use the@PrePersistand@PreUpdateannotations to assign the current date to the property, prior to save/update, if and only if it has not been assigned yet:This closely related question provides different approaches: Creation timestamp and last update timestamp with Hibernate and MySQL.