Today, the instructor explained in detail the Povo in hibernate.
The Povo that we first understood is to use the SAVE () method to persist OBJ to the database. if you want to update, set the attribute in OBJ to a new value, and execute Update (this is an incorrect concept), because Povo has not been touched yet, it was very powerful at first, and now I remember Povo in my mind. I thought this benefit could be easily used in the project. The incorrect understanding of the results gave me a lot of exceptions: a different object with the same Identifier value was already associated with the session; the above is one of the reasons, which is caused by incorrect understanding of Povo. Let's take a look at the following section. Code : CAT princess = new CAT (); // creates a cat object.
Princess. setid ("402881830c2cf0f3010c2cf0f8b40001"); // sets the existing ID of an object in a database
Princess. setname ("princess ");
Princess. setsex ('F ');
Princess. setweight (18.8f );
Cat princess2 = new CAT (); // creates a new cat object.
Princess2.setid ("402881830c2cf0f3010c2cf0f8b40001"); // set the object with the same ID
Princess2.setname ("princess ");
Princess2.setsex ('F ');
Princess2.setweight (19.8f );
Session session = hibernatesessionfactory. currentsession (); // The player is playing. Let's start with current a session
Transaction Tx = session. begintransaction (); // start transaction (transaction doesn't know exactly what it is, I only know it can write data in the cache into the database) // cat c = (CAT) session. load (cat. class, "402881830c2cf0f3010c2cf0f8b40001 ");
// C. setweight (optional 3f );
Session. saveorupdate (PRINCESS); // at this time, we use the session to save Princess. After this, the princess should be persistent and become a po.
Session. saveorupdate (princess2); // if I understood it at the beginning, princess2 should also be persistent (see the blue part above ), because princess2 only updates princess (this is a fatal error ).
TX. Commit ();
Hibernatesessionfactory. closesession (); the execution result is always so ironic. When a different object with the same Identifier value was already associated with the session encounters a problem, it is always the most exciting and helpless. let's take a look at what went wrong: First, let's talk about Povo, Po, and persistence object. Persistence objects refer to objects through Save (), update (), load (), after get ();, add the Persistent object in the Entity map object container of the session, that is, in my session. saveorupdate (PRINCESS);, the key and object have been added to the object container. because map uses different keys to add OBJ, and ID is the unique identifier in hibernate, it is best to use ID as the key. therefore, the session object container can only add one ID object. when session. saveorupdate (princess2); when the session is about to add princess2 to entitymap, because of the same ID, that is, the key, it reports an exception for a different object with the same identifier .. congratulations. that is to say, Po is an object that can be referenced in the object container of the session. When I want to update the Po, I can set a property value, as long as Tx. the COMMIT (); action can be updated (which surprised me ). besides save ();, PO can also be obtained through load, update, and get. however, there is a little difference between load and get (in version 3.0). Get (ID) directly obtains do (select operation is executed immediately), while load is different, it will mark the ID to be loaded first. When you want to use the exported object, it will execute. in the example above: cat c = (CAT) session. load (cat. class, "402881830c2cf0f3010c2cf0f8b40001"); // only mark
C. setweight (optional 3f); // The SELECT statement is actually executed at this time.