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.