Save () in Hibernate session, update (), delete (), saveorupdate () fine-grained analysis

Source: Internet
Author: User

Hibernate must obtain the session instance before working on the database, which is equivalent to JDBC having to obtain the connection instance before the database operation, the session is the basis of hibernate operation, It is not designed for thread safety (Thread-safe), and a session is used by a thread.

    • Open session
session instances are obtained by sessionfactory, for example:
Config uration config = new Configuration (). Configure ();
sessionfactory sessionfactory = Config.buildsessionfactory ();
session Session = Sessionfactory.opensession ();
Transaction tx = Session.begintransaction ();
....
Tx.commit ();
Session.close ();

Open session will not get connection immediately, but in the end really need to connect to the database for updates or queries to get connection, if there is set user user = new user ();
User.setname ("Momor");
user.setage (New Integer (26));

Session session = Sessionfactory.opensession ();
Transaction tx = Session.begintransaction ();
Session.save (user);
Tx.commit ();
Session.close ();

Session session = Sessionfactory.opensession ();
Transaction tx = Session.begintransaction ();
User user = (user) Session.get (user.class, New Integer (1));
tx.commit ();
Session.close ();

If the matching data is not found, the Get () method returns NULL, and the load () method throws Objectnotfoundexception, and in the advanced application, the load () method can return the proxy object. When necessary, the database is actually queried for the corresponding data and the cache is fully utilized.

In Hibernate 3, the Find () method is canceled, and you must use query or criteria to search for data.


    • Delete data
take a look at using the session delete data, you can delete data using delete ():
Session session = Sessionfactory.opensession ();
Transaction tx = Session.begintransaction ();
User user = (user) Session.get (user.class, New Integer (1));
Session.delete (user);
Tx.commit ();
Session.close ();
    • Update information
When you take the data in the same session and encapsulate it as persistence object, then update the state of the object, and then transaction commit, the status updates on the object will be reflected in the repository, and you do not need to do any additional updates.

If you open a session, take the data from the table to the user interface, and then close the session, when the user finishes working on the interface and presses the storage, then you re-open a session, using update () method to update the data in the object to the corresponding table:

Session session = Sessionfactory.opensession ();
Transaction tx = Session.begintransaction ();
User user = (user) Session.get (user.class, New Integer (2));
Tx.commit ();
Session.close ();
....
User.setage (New Integer (27));
Session = Sessionfactory.opensession ();
tx= session.begintransaction ();
Session.update (user);
Tx.commit ();
Session.close ();


The session provides a saveorupdate () method that provides a uniform operating interface for storing or updating data, and by defining a mapping file, setting the < id> tag Unsaved-value to determine what the new value is required, What is an existing value must be updated:
<id name= "id" column= "id" type= "java.lang.Integer" unsaved-value= "null" >
<generator class= "native"/>
</id>

The values that Unsaved-value can set include:
    • Any: Always store
    • None: Always update
    • Save when Null:id is null (preset)
    • Valid:id is null or stored when a value is specified

Once this is set, you can use the Saveorupdate () method of the session to replace the update () method.


    • Merge status
For The example here:
Session session = Sessionfactory.opensession ();
Transaction tx = Session.begintransaction ();
User User1 = (user) Session.get (user.class, New Integer (2));
tx.commit ();
session.close ();
....
user1.setage (new Integer);

session = Sessionfactory.opensession ();
tx= session.begintransaction ();
User user2 = (User) Session.get (User.class, New Integer (2)); Same ID
User1 = = User2 to False
Session.update (user1); Throw out Nonuniqueobjectexception
Tx.commit ();
session.close ();


Before updating (), you will first obtain another data from the database, and then try to update User1, because the current persistence context is loaded with a data, and has the same ID of the user instance, the same persistence The context does not have two object instances of the same ID, so the nonuniqueobjectexception is dropped.

For this example, you should use merge () instead of update ():
Session session = Sessionfactory.opensession ();
Transaction tx = Session.begintransaction ();
User User1 = (user) Session.get (user.class, New Integer (2));
tx.commit ();
session.close ();
....
user1.setage (new Integer);

session = Sessionfactory.opensession ();
tx= session.begintransaction ();
User user2 = (User) session.get (User.class, New Integer (2));//Same ID
User User3 = (user) session.merge (user1);
//User1 = = User2 to False
//User2 = = User3 true
tx.commit ();
session.close ();


Merge () merges the state of the User1 with the User2 and returns an instance of the object that was originally referenced by User2.

Save () in Hibernate session, update (), delete (), saveorupdate () fine-grained analysis

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.