We already know that when a new persistent instance is created through new, the instance is in a transient state, in order to convert the transient objectPersistence status, Hibernate session provides two methods:
Serializable save (Object OBJ) |
Changes the OBJ object to a persistent state, and the properties of this object are saved to the database. |
Void persist (Object OBJ) |
Changes the OBJ object to a persistent state, and the properties of this object are saved to the database. |
Therefore, to change a transient object to a persistent state, you can see the following code snippet:
News n = new news (); N. settitle ("unknown: ranokin"); N. setcontent ("xxxxxxxxxxxxxx"); Session. Save (N );
When we change a transient instance to a persistent state, Hibernate will generate an insert statement at the underlying layer, which inserts the corresponding data of the instance into the data table.
① If the logo attribute of news is generated, that is to say, if the primary key generator is specified, Hibernate will automatically generate the logo attribute value when executing the Save method, and assign the logo attribute value to the news object.
② If the news identity attribute is of the assigned type or a joint primary key, the identity attribute value should be manually assigned to the news object before calling save.
Note:
Hibernate provides a persist () method that is almost similar to the SAVE () function. On the one hand, it aims to take care of the usage habits of JPA. On the other hand, there is another difference between SAVE and persist: UseSave ()When the method saves the Persistent object, this method returns the identifier property value of the Persistent object, that is, the primary key value of the corresponding record;Persist ()When the method saves the Persistent Object, there is no return value. Because the Save method needs to return the identifier property value of the Persistent object immediately, the program runs save ( ) Method will immediately Insert the data corresponding to the persistent object into the database. The persist method ensures that it is not immediately converted into an insert statement when it is called outside a transaction. This function is very useful, especially when we encapsulate a long session flow, persist is especially important. |
Load () and get ():
You can also load a persistent instance by load (). This type of load loads the persistent instance based on the persistence class's identifier property value. The essence is to load a new record from the data table based on the primary key. News n = session. Load (news. Class, new INTEGER (PK); PK is the identifier attribute of the persistent instance to be loaded. If no matching database records exist, the load () method may throw a hibernateexception exception. If we specify delayed loading in the class ing file, load () method returns an uninitialized proxy object. This proxy object does not load data records. hibernate accesses the database only when the program calls a method of this proxy object. If you want to create an association pointing to another object in an object and do not want to load all associated objects while loading the object from the database, this method of delayed loading is very useful. Similar to the load () method, the get () method is used to load persistent instances based on the primary key, but the get () method immediately accesses the database, if no corresponding record exists, the get () method returns NULL instead of returning a proxy object. |
Once the persistent instance is loaded, the object is in the persistent state and the modifications made to the persistent instance in the Code, such as: N. settitle ("New title"); this modification will be saved to the database, and the modification to the title will be mapped to a specific column of a specific row that modifies the data table.
The modifications made by the program to the persistent instance will be automatically saved to the database before the session is flushed. No other methods need to be called by the program (the update method is not required) to persist the changes. That is to say, the simplest way to modify an object is to load the object when the session is open and then directly modify it.
For a persistent object that has been persisted but is now out of session management, we call it in the unmanaged state. After modifying the status of the unmanaged object, the program should use the new session to save the changes. Hibernate providesUpdate (), merge ()AndUpdateorsave ()To save the changes.
News n = firstsession. load (News. class, new INTEGER (PK); firstsession. close (); N. settitle ("New title"); Session secondsession = hibernatesessionfactory. getsession (); secondsession. update (N );
When we use another session to save this modification, the unmanaged object will return to session management again, and will return to the persistent state again.
When you need to useUpdate ()To save the modifications made by the program to the persistent object, if it is unclear whether the object has been persisted, the program can choose to useUpdateorsave ()Method. This method automatically determines whether the object has been persistent. If it has been persistent, use the update method. Otherwise, the SAVE operation will be used.
Merge ()The method can also save the modifications made by the program to the database, but the biggest difference between the merge method and the update method is:The merge () method does not persistently specify objects.For example, when we execute session. after the update (a) code is executed, object a becomes persistent and the session is executed. after Merge (a) code, object A is still not in the persistent state, and object A is still not associated with the session.