Insert and update statements
In the initialization phase, Hibernate predefines the following SQL statements for all persistence classes based on the ing information of the ing file:
- Insert statement. For example, the insert statement of the person class is as follows:
Insert into person (ID, name, age, address) values (?,?,?,?)
- Update statement. For example, the update statement of the person class is as follows:
Update person set id = ?, Name = ?, Age = ?, Address =?
- Delete statement. For example, the delete statement of the person class is as follows:
Delete from person where id =?
- The SELECT statement of the persistence class instance is retrieved Based on the oId. For example, the SELECT statement of the person class is as follows:
Select ID, name, age, address from person where id =?
The question mark in the preceding SQL statement represents the parameters in JDBC preparedstatement. These SQL statements are stored in the cache of sessionfactory. When session save (), update (), and delete () are executed () when using the load () method, the corresponding predefined SQL statement is found in the cache, and then the specific parameter value is bound to the SQL statement.
By default, a predefined SQL statement contains all the fields of the table. In addition, Hibernate allows you to control the memory of the insert and update statements in the ing file. For example:
<Property name = "addrsss" update = "false" column = "address"/>
AboveCodeSet the update attribute of the <property> element to false, which indicates that the update statement does not contain the address field.
Ing Property |
Function |
<Property> insert attribute of an element |
If the value is false, this field is not included in the insert statement, indicating that this field cannot be inserted. The default value is true. |
<Property> Update attribute of an element |
If the value is false, the update statement does not contain this field, indicating that this field can never be updated. The default value is true. |
<Class> mutable attribute of an element |
If the value is false, the update attribute of all <property> elements is false, indicating that the entire instance cannot be updated. The default value is true. |
<Property> dynamic-insert attribute of an element |
If it is true, an insert statement is generated dynamically when an object is saved. The insert statement is included only when the value of this field cannot be null. The default value is false. |
<Property> dynamic-update attribute of an element |
If it is true, an update statement is generated dynamically when an object is updated. The update statement is included only when the value of this field is not null. The default value is false. |
<Class> dynamic-insert attribute of an element |
True is equivalent to true in the dynamic-insert attribute of all <property> elements, indicating that an insert statement is generated dynamically when an object is saved, the insert statement only contains all fields whose values are not null. The default value is false. |
<Class> dynamic-update attribute of an element |
If this parameter is set to true, the dynamic-update attribute of all <property> elements is set to true, indicating that an update statement is generated dynamically when an object is saved, the update statement only contains all fields whose values are not null. The default value is false. |