Session
Manage the task units of a database, that is, managing the addition, deletion, modification, and query operations in the database and committing transactions.
Method crud: Save (), delete (), load (), get (), update (), saveorupdate (), clear (). Flush ()
Session. begintransaction ();
Session. Save (Object OBJ );
Session. gettransaction (). Commit ();
Session. Close ();
Get ()AndLoad ()Difference
L when searching, it will give priority to the session cache.
L when the corresponding record does not exist in the search, the load method does not return an error when it is not found, and the get method returns an error when it is not found.
L load returns a proxy object. When the object content is actually used, an SQL statement is initiated. Get directly initiates an SQL statement to retrieve it from the database without delay.
Update ()Method
1 is used to update the detached object. After the update is complete, the object becomes persistent.
2. An error will be reported when you update the transient object. You can update the transient object of your Set ID (provided that the ID exists in the Database.
3. The persistence object will be updated as long as different fields are set.
4. Update partially changed fields (three methods)
L XML sets the property tag update = true | false attribute,
Annotation sets the @ column (updatable = false) attribute. This method is less useful and less flexible than Z.
L set the dynamic-update = "true" attribute of the class label in XML,
Yes in the same session, but not across sessions. the cross-session implementation method can use the Merge () of the session (). the merge method first loads data from the database, compares the obtained data with those in the database, and then updates the changed fields.
Jpa1.0 annotation does not have corresponding attributes, Hibernate
L use hql (ejbql) (recommended)
Clear ()Method:
The load () and get () methods give priority to the session cache. Calling the clear () method will forcibly clear the session cache and will not deal with the database.
Flush ()Method:
The flush () method forces synchronization from memory (session cache) to database. Synchronization is performed only when the session transaction is committed by default.