Hibernate to check the deletion of individual records _hibernate

Source: Internet
Author: User

(Hibernate learning Notes series from "Crazy Java" hibernate video learning) Save users
Session.save (person); Session.persist (person); Query users
Load (class Theclass,serializable ID) Get (class Theclass,serializable ID) modifies the user
Session.update (person) session.merger (person); Save or modify a user
Session.saveorupdate (person) Merge Delete user
Session.delete (person) increased

There are two ways to add records, save () and persist ();
and persist: insist; Remain Stubborn continues to exist, so the name of the persist () is more obvious.
Save () returns an ID
and persist () has no return value
* * If the added object specifies an ID field, persist an exception occurs. And save ignores the setting of the ID field * *
Check
Query for a record based on ID, with two types of load () and get () methods

Load (Class theclass,serializable ID)

Theclass refers to the persisted class to query, Serializable ID represents the first few records in the table

Get (Class theclass,serializable ID)

Get immediate query, and load is deferred query (query when used)
Load returns a proxy for the persisted class, waiting for the database to be queried by using a field other than the ID of the instance
Objectnotfoundexception exception occurs when the record for the Load method query ID does not exist
The return record is null when the record for the Get method query does not exist
The *.hbm.xml file can use the class label's lazy property to set the Load method to query immediately, default to True, and to represent deferred queries

Change
method is update (), merge (), Saveorupdate ()

First you need to understand the three states of objects in hibernate

Update () and merge (), Saveorupdate () have two different ways

First query out a record, then update the data record in the execution update

You need a transaction when you change, so create a transaction object with the session. The time of the
change, when the session is closed

yourself new an object, then set the field to be updated, other fields will be set to the original value (otherwise it will be empty), and then set the value of the ID, this ID must be the ID in the database, otherwise there will be an exception

Here we can also see that the unique identity field in the database should be an object, not a base type, because the base type will have a default value, so if you forget the SetID (ID), you will modify the Id=0 record

Using Session.merge (Object o)
The merge () method returns the corresponding persisted object of O. If O is itself a persisted object, the return value and O point to the same object; If O is a temporary state (a new object), then the return value is a persisted object and is different from O.
For merge
If the given ID does not exist, it is new, even if the ID does not exist;
If ID exists, it is modified.

Using Saveorupdate ()
Adds or modifies an operation based on whether the object passed has an ID.
Have ID is to modify
No ID is added
If the ID of the passed object does not exist in the database, an error is deleted

Method: Delete ()
Gets or load the record to be deleted, and then calls delete (Object O);
Or new object, set the ID, and then call Delete (), if the ID doesn't exist throw an exception

The above operations are for a record of additions and deletions to check

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.