The status of the Hibernate Persistence object.

Source: Internet
Author: User

The status of the Hibernate Persistence object.
1. From the perspective of persistence,Hibernate divides objects into four states.:Persistence status,Temporary status,Free Status (off-pipe status),Delete state. The Session method can convert an object from one state to another.
2. temporary status (Transient):1). When the proxy primary key is used,OID is usually null 2). It is not in the Session cache 3). There is no corresponding record in the database
3. Persistence status(Also called "hosting")(Persist)
1). OID is not null 2). It is located in the Session cache 3). If there is a corresponding record in the database,The persistence object corresponds to the related records in the database. 4) The Session is stored inFlushCache Time,Will change according to the attributes of the Persistent Object,To synchronously update the database. 5) in the cache of the same Session instance,Each record in the database table only corresponds to a unique Persistent Object (the OID of the Session cache object cannot be repeated)
@ Test public void testOID(){News news2= (News)Session. get(News. class,1);System. out. println(News2);News news=New News();News. setAuthor("Oracle2");News. setContent("Oracle DB");News. setDate(New Date());News. setTitle("DB");News. setId(1);//Throw an exception: org. hibernate. NonUniqueObjectException session. update(News);}
4. Free Status(It is also called "off-pipe")(Detached): 1). OID is not null 2). It is no longer in Session cache 3). Generally, you need,Free objects are transformed from persistent objects.,Therefore, there may be records corresponding to the database.
5. Deletion Status(Removed)1). There is no record corresponding to its OID in the database. 2). It is no longer in the Session cache. 3). Generally,The application should no longer use the deleted object.
6. Status transition diagram
7. Differences between get and load: get and load an object from a data table,Differences between objects in persistent state after loading: get:Query the data table immediately. If no corresponding record exists,Returns null;Load:The data table is queried late until the first time this object is used.,Then, a proxy object is returned. If no corresponding record exists,This object is used.,The exception org. hibernate. ObjectNotFoundException is thrown.

8. update method:
@ Test public void testUpdate(){News news= (News)Session. get(News. class,1);System. out. println(News);Transaction. commit();Session. clear();Session=SessionFactory. openSession();Transaction=Session. beginTransaction();//At this time, the object is in the free status news. setAuthor("Oracle");//Update changes a free object to a persistent object.//Generally, you need to send an UPDATE statement.//If the object is a persistent object,You do not need to call update()Method session. update(News);}
Note: 1 ). application Scenario: when an object is uploaded from the page in the B \ S project and needs to be updated, update () method 2 is called ). if no record corresponding to the object exists in the data table, update is also executed.()Exception: org. hibernate. StaleObjectStateException
9. saveOrUpdate():Save or update
11. delete(): Delete the record corresponding to an object. 1). If you only want to delete the specified object, set the ID. 2) If no corresponding record exists in the data table, an exception is thrown: java. lang. IllegalArgumentException
12. Hibernate calls the native jdbc api:
Session. doWork(New Work() {@ Override public voidExecute(Connection connection)Throws SQLException{//Use the JDBC native API}});


What are the three persistence states of hibernate objects?

Instantaneous State: Simply put, you create an object in the program and it is not associated with the session.
Persistent state: the object is associated with the session, and the object is under hibernate framework management.
Free State: In the persistent state, the object and session are lost, such as session. close () or session. flush ()
But the data has been stored in the database.

Persistence of hibernate

Your understanding is reversed. Persistence, as its name implies, is to put the cached content into the database to make it persistent. for objects to be persisted, the lifecycle of an object is divided into three states: temporary state, persistent state, and free state.
Temporary State: a java object in the temporary state is called a temporary object. It has just been created with the new statement and has not been persisted and is not in the session cache.
Persistence: the object has been persisted and added to the session cache. A persistent java object is called a persistent object.
Free State: a java object that has been persisted but is not in the session cache.
Persistence object features:
Stored in the cache of a session case. It can be said that the persistent object is always associated with a session case.
. The persistence object corresponds to the relevant records in the database
When the Session clears the cache, it synchronously updates the database based on the attribute changes of the Persistent Object.
The save () method of the Session changes the temporary state to the persistent state.
The update (), saveOrUpdate (), and lock () Methods of the Session change the free state to the persistent state.

Related Article

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.