Detailed explanation of session addition, deletion, modification and query operation code in Hibernate

Put the three state transition diagram here to facilitate the function of the analysis method:

1. Save() method of session

Session is the most important of all hibernate interfaces. It provides methods for saving, updating, querying and deleting data.

The save () method of session can convert the temporary state or free state into persistent state. For example, save a customer object:

The save () method mainly does the following three things:

(1) Put the temporary customer object created by new into the cache to make it persistent.

(2) At the same time, a unique oid is generated for the object according to the oid generator set in the object relationship mapping file, that is, the primary key generation method.

There is a question here about whether the setid () method generates a primary key for the object persistence when creating an object. In fact, it is not. Setid does not generate a primary key as we set, but generates a primary key by the primary key generation method configured in the object relationship mapping file. It can be run several times. The primary key grows. Multiple pieces of data can be seen from the database. The primary key starts from 1, So you can know setid()

The primary key is not set, otherwise the database primary key uniqueness verification cannot pass.

We can also set the primary key value manually. We must overload the save () method and use the overload method save (C1, NEWLONG (1)) to set it manually each time.

(3) Plan to execute the insert statement. Note that the insert statement is not executed immediately. The insert statement will be executed only when the session cleans up the cache. Tr.commit() transaction commit.

2. Session update() method

Makes a free state object persistent. For example:

The update() of the session should do the following:

(1) Add the free customer object to the session cache to make it a persistent object.

(2) Execute the update statement. Like the insert statement executed by save(), the update statement is not executed immediately. When the cache is empty, the customer object needs to be assembled into an update statement,

Then execute.

Note that even if the customer is not changed, by default, the update statement will be assembled when the cache is cleared. If the update statement needs to be changed to execute, the object relationship mapping file needs to be set.

3. Saveorupdate() method of session

The saveOrUpdate () method contains the functions of the save () and update () methods. Different methods are called according to the status of the incoming parameters. The save() method is called when a temporary object is passed in. If a free object is passed in, the save() method is called

Update() method. The incoming persistent object returns. Therefore, each time we just need to pass in the object, we can use the saveOrUpdate () method to automatically judge the state of the incoming object and dynamically call the processing method.

How does this method determine the status of the incoming object? When any of the following conditions is met, it is a temporary state:

(1) The oid of the Java object is null, indicating that the object is not instantiated. Even if it is instantiated, it is also an object that becomes temporary after the free state is deleted. In this case, the object is temporary.

(2) If the Java object has version control, the version number is null, that is, there is no version number of the object.

(3) Customize the interceptor, call isunsaved() and the return value is true.

4. Load () and get () methods of session

Both methods load a persistent object from the database according to oid. The persistent object is placed in the session cache and can be operated according to different needs.

Differences between the two:

When there is no corresponding record of oid in the database, load() throws an exception and get() returns null.

5. Delete() method of session

Delete () is used to delete records corresponding to Java objects from the database as the name suggests.

Delete() if a persistent object is passed in, assemble a delete statement to execute the deletion; If a free state object is passed in, hibernate first associates the free state with the session to become a persistent state, and then generates a delete statement,

Perform deletion.

Both are executed only when the session cache is empty.

The above execution is an object, corresponding to a record.

You can use session delete("fromCustomerwhere...."); Conditions are added to delete multiple pieces of data.

Execution result, console output:

Hibernate: select max(ID) from CUSTOMERS Hibernate: insert into CUSTOMERS (NAME,EMAIL,PASSWORD,PHONE,ADDRESS,SEX,IS_MARRIED,DESCRIPTION,IMAGE,BIRTHDAY,REGISTERED_TIME,ID) values (?,?,?) Hibernate: delete from CUSTOMERS where ID=?

summary

The above is all about the detailed explanation of the operation code of session addition, deletion, modification and query in hibernate. I hope it will be helpful to you. Interested friends can continue to refer to other related topics on this site. If there are deficiencies, please leave a message to point out. Thank you for your support!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>