Hibernate entry BLOG [3. Common session methods and three statuses of hibernate objects]

Source: Internet
Author: User

[Java]
Common Methods of Session in Hibernate
In Hibernate, we often use session objects to Operate Database CRUD. Next, let's analyze it.
Common Methods of session:
Void SessionTest (){
User user = new User ();
Session s = HibernateUtil. getSession ();
// Start the transaction
S. beginTransaction ();
// Clear Cache
S. clear ();
// Close the session
S. close ();
// Query the corresponding object with the Bureau ID
S. get (User. class, 5 );
// Delete an object
S. delete (user );
// Synchronize the session with the database
S. flush ();
// Lazy loading. When loading an object, the returned object will never be empty.
S. load (User. class, 4); www.2cto.com
// Update. The updated object is still hosted.
S. merge (user );
// Save and persist are both saved. Their difference is that
// Save if the transaction is not enabled, the insert operation is performed once. But it will return rollback.
// If persist does not enable the transaction, it will not insert at all. This is their fundamental difference.
S. persist (user );
S. save (user );
}
 
---------------------------------------------------------------------
 
Three statuses in the Hibernate object and the saveOrUpdate method:
The status of the JavaBean object for hibernate:
Instantaneous State: a new object is an instantaneous object.
Persistent Object: the data and objects in the database. It is related to session. When a persistent object changes, the objects in the database can correspond
Managed (free): the database contains data and other objects. But it has nothing to do with the session object. It changes but the database does not.
 
 
What is the difference between a hosted object and an instantaneous object?
1. There is no relationship between the instantaneous object and the database and the session, and the lifecycle is short.
2. Hosting is based on data in the database. But not in the session. Even if the managed object changes, hibernate will not be aware of it.
 
When the managed object needs to be updated, we need to call the update method. If the persistence object is modified, the related SQL statement will be run to change the database.
 
 
How does saveOrUpdate determine whether to save or update?
It is determined by ID and version. If the id is null or 0, the object is an instantaneous object. If the id is not empty, the update statement is called for the hosted object.
The objects updated by merge are managed objects. Non-Persistent Object. The above saveOrUpdate operation ends with a persistent object.
<! -- Id indicates the primary key, while name = id indicates the attributes in the class. That is, the attribute corresponding to the primary key.
Unsaved-value determines whether it is set as an instantaneous object. The following value is used by default,
If this parameter is not modified, the int value 0 string is null.
-->
<Id name = "id" unsaved-value = "-1">
<! -- Primary key generator -->
<Generator class = "native"/>
</Id>

Author: zhang6622056

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.