Hibernate add, delete, modify and query operation code

Hibernate deletes data

To delete a piece of data in the user table, you need to have the primary key ID value of the user table. First, query the corresponding object from the database according to the ID value. There are two methods: the get method of session and the load method of session.

Session get method: calling this method will return an object object. Then we cast it. Useruser = (User)session. get(User.class,” 402881e5441c035e01441c0360510003”); When we pass the ID value and find no corresponding result in the data, the get method will return a null value.

Difference: when the get method is loaded, it will immediately issue SQL statements to query, while when the load method is executed, it does not immediately issue SQL statements to query, generate a proxy user, and do not generate a real user. When we really use this user, we will load the real user. Load() supports deferred loading, while get() does not. Get returns a null object when the loaded object does not exist, and load() throws an objectnotfoundexception exception when the loaded object does not exist.

Session load method: this method is also called to return an object object, and then forced conversion is performed.

Then we load the object corresponding to the user table ID through get or load, and then call the delete method of session to delete the object and delete a record in the table. The code is as follows.

The first deletion method.

The second deletion method is to manually construct the detached object and then delete it.

The code is shown below.

Hibernate data query operation

For general query, the code is as follows.

For paging query, the code is as follows.

Hibernate updates data

Manually construct the detached object and call the update () method of the session. The code is as follows.

Load the object and call the update () method of the session to update the object when it is in the persistent state. The code is as follows.

summary

The above is the hibernate addition, deletion, modification and query operation code introduced by Xiaobian. I hope it will help you. If you have any questions, please leave me a message, and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

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
分享
二维码
< <上一篇
下一篇>>