Put the three-state transition diagram here to facilitate the function of the analysis method:
1.Session save() method
Session is the most important interface among all Hibernate interfaces, providing methods for saving, updating, querying and deleting data.
Session's save() method can convert temporary or free states to persistent states. For example, save a Customer object:
SessionFactory sessionFactory;Configuration configuration = new Configuration().configure();sessionFactory = configuration.buildSessionFactory();Session session = sessionFactory.openSession();Transaction tr = session.beginTransaction();//1. Create Customer object Customer c1 = new Customer();c1.setId(new long(1));c1.setName("test");c1.setEmail("[email protected]");c1.setPassword("123456");c1.setAddress("Paradise");//2. Call the save() method of Session to persist the Customer object session.save(c1);tr.commit();session.close();The Save() method mainly does the following three things:
(1) Put the temporary Customer object created by new into the cache to make it persist.
(2) At the same time, according to the OID generator set in the object relationship mapping file, that is, the primary key generation method generates a unique OID for the object.
<!--Set the primary key-><idname="id"column="ID"type="long"><!--Primary key generation method-><generatorclass="increment"/></id>
There is a question in this place. When creating an object, whether the setId() method generates a primary key for the persistence of the object. In fact, it is not. setId does not generate the primary key as we set, but generates the primary key based on the primary key generation method configured in the object relationship mapping file. It can be run several times more, and the primary key grows by itself. You can see multiple pieces of data from the database. The primary key starts from 1, so you can know that setId()
The primary key is not set, otherwise the database primary key uniqueness verification will not be possible.
We can also manually set the primary key value. We must overload the save() method, use the overload method save(c1, newLong(1)), and set it manually each time.
(3) Plan to execute the insert statement. Note that the insert statement is not executed immediately, and the insert statement will only be executed when the Session cleanses the cache. tr.commit() transaction commit.
2.Session's update() method
Turn free-state objects into persistent objects. For example:
SessionFactory sessionFactory; Configuration configuration = new Configuration().configure(); sessionFactory = configuration.buildSessionFactory(); Session session1 = sessionFactory.openSession(); Transaction tr1 = session1.beginTransaction(); //1. Create Customer object Customer c1 = new Customer(); c1.setId(new Long(1)); c1.setName("test"); c1.setEmail("[email protected]"); c1.setPassword("123456"); c1.setAddress("Paradise"); //2. Call Session save() method to persist the Customer object session1.save(c1); tr1.commit(); session1.close(); //3. Update the free-state object, find changes, and execute the update statement Session session2 = sessionFactory.openSession(); Transaction tr2 = session2.beginTransaction(); c1.setAddress("test update"); session2.update(c1); tr2.commit(); session2.close();The update() of Session should do the following:
(1) Add the free Customer object to the Session cache to become a persistent object.
(2) Execute the update statement. Just like saving() executes the insert statement, it does not execute the update statement immediately. When the cache is cleared, the Customer object is assembled into an update statement.
Then execute again.
Note that even if Customer has not changed, by default, the Update statement will be assembled when the cache is cleared. If it needs to be set to change, the object relationship mapping file needs to be set.
3.Session saveOrUpdate() method
The saveOrUpdate() method contains the functions of the save() and update() methods, and different methods are called according to the state of the passed parameter. Passing in a temporary object to call the save() method, if passing in a free object to call it
update() method. Returns the incoming persistent object. Therefore, every time we only need to pass in the object, saveOrUpdate() method, automatically judge the state of the passed object state, and dynamically call the processing method.
How does this method determine the state of the incoming object? When any of the following conditions are met, it is a temporary state:
(1) The OID of the java object is null, which means that the object is not instantiated otherwise. Even if it is instantiated, it is an object that has been deleted and becomes a temporary state after the free state is deleted. In this case, the object is temporary state.
(2) If the java object has version control and the version number is null, it means that there is no version number of the object.
(3) Customize the interceptor, call isUnsaved() and return the value is true.
4. Session's load() and get() methods
Both methods are based on OID, loading a persistent object from the database. The persistent object is placed in the Session cache, and the persistent object can be operated according to different needs.
The difference between the two:
When there is no corresponding record in the OID in the database, load() throws an exception and get() returns null.
5. Session's delete() method
delete() as the name implies is used to delete records corresponding to java objects from the database.
delete() If a persistent object is passed in, assemble a delete statement and execute the delete; if a free state object is passed in, hibernate first associates the free state to the session, becomes a persistent state, and then generates the delete statement.
Perform deletion.
All are executed only when the session cache is cleared.
The above executions are all an object, corresponding to a record.
You can use session.delete("fromCustomerwhere..."); to delete multiple data after adding conditions.
SessionFactory sessionFactory; Configuration configuration = new Configuration().configure(); sessionFactory = configuration.buildSessionFactory(); Session session1 = sessionFactory.openSession(); Transaction tr1 = session1.beginTransaction(); //1. Create Customer object Customer c1 = new Customer(); c1.setId(new Long(1)); c1.setName("test"); c1.setEmail("[email protected]"); c1.setPassword("123456"); c1.setAddress("Paradise"); //2. Call the save() method of Session to persist the Customer object session1.save(c1); tr1.commit(); session1.close(); //3. Associate the free state object to the session, clear the cache after persistence, and execute the delete statement; put it in the persistent state to delete it directly; Session session2 = sessionFactory.openSession(); Transaction tr2 = session2.beginTransaction(); session2.delete(c1); tr2.commit(); session2.close(); 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=?
Summarize
The above is all the detailed explanation of the Session Add, Deletion, Modification and Search Operation Code in Hibernate, I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!