Notes on using hibernate

When using hibernate, sometimes you will encounter some strange problems, most of which are caused by insufficient understanding of hibernate or ignoring some of its important features. Here is a summary of the problems encountered, look at them often, and avoid multiple jumps. a pit.

 

1. The database is obviously updated using sql, but after querying the database, it is found that the data to be updated has not changed

Scenes:

1. The method is a transaction operation;

2. First query a piece of data through hibernate, and update one of the properties, but the saved method is not called;

3. Execute an update operation through the sql statement: update table_xxx set field_xxx=yyy where ..., this operation is not suitable for using hql representation;

4. After the transaction is executed, it is found that the attributes in step 2 have been updated to the database, but the attributes in step 3 have not changed;

 

Reason: The update in step 3 was successful, but it was overwritten by the data in step 2 at the end of the transaction;

Solution: When executing 3, flush it, and force operation 2 to refresh the cache to the database first, to ensure the order of sql execution, and to avoid subsequent data being overwritten;

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326094284&siteId=291194637