Hibernate set MySQL timestamp default tricks

Source: Internet
Author: User

First, to use the default values configured in the database, you must not let hibernate overwrite the default values, you need to configure the property insert= "false" update= "false", Tells hibernate not to perform an INSERT, update operation on this property, which will not overwrite the default values we configured in the database. In addition to this, we also need to configure Hibernate's operation on the table,dynamic-insert= "true" dynamic-update= "true",

To implement dynamic insertion of values into the database, insert,update only non-empty property values, and Null property values are not inserted into the database, which avoids null overwriting the default values set in the database table.

1 <classname= "Com.tea.lms.model.UserCourse"Table= "T_user_course"2 Catalog= "LMS"Lazy= "false"Dynamic-insert= "true"dynamic-update= "true">3         <!--This setting dynamic-insert= "true" dynamic-update= "true" is that hibernate inserts only non-null values when inserting data, and null values are not inserted into the database -4         <IDname= "id"type= "Long"> 5             <columnname= "id"Not-null= "true"length= " the"></column>6             <Generatorclass= "Native"></Generator>7         </ID>8         <Many-to-onename= "User"class= "Acc.model.User"Cascade= "Delete"9 Lazy= "false">Ten             <columnname= "user_id"></column> One         </Many-to-one> A         <Many-to-onename= "Course"class= "Com.tea.lms.model.Course" - Cascade= "Delete"Lazy= "false"> -             <columnname= "course_id"></column> the         </Many-to-one> -         <Many-to-onename= "Lastuseritem"class= "Com.tea.lms.model.UserItem" - Cascade= "All"Lazy= "false"> -             <columnname= "last_user_sco_id"></column> +         </Many-to-one> -         < Propertyname= "Regtime"Not-null= "true"type= "Timestamp" + Insert= "false"Update= "false"> <!--Java type, INSERT, update are set to False so hibernate does not overwrite default values - A             <columnname= "Reg_time"></column> at             <typename= "Timestamp"></type> <!--SQL Type - -         </ Property> -         < Propertyname= "Score"type= "int"> -             <columnname= "Score"length= "One"></column> -         </ Property> -         < Propertyname= "Pass"type= "int"> in             <columnname= "Pass"length= "One"></column> -         </ Property> to     </class>

Hibernate configuration is not finished, we also need to configure in MySQL, processing configuration TIMESTAMP column default value is current_timestamp not yet. Because the default values are inserted in the new version of MySQL, the default values are inserted only when the front-row constraint is not empty and no value is inserted. All we also need to configure the timestamp column constraint not NULL.

Completed.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.