Modify a detached object
ManyProgramYou need to obtain the object in a transaction, send the object to the interface layer for operation, and save the modification in a new transaction. This method is used in highly concurrent access environments. Data with version information is usually used to ensure isolation between these "long" work units.
Hibernate providesSession. Update ()
OrSession. Merge ()
To support this model, you can re-associate the database with a single instance.
// In the first session
Cat = (Cat) firstsession. Load (cat. Class , Catid );
Cat potentialmate = New CAT ();
Firstsession. Save (potentialmate );
// In a higher layer of the application
Cat. setmate (potentialmate );
// Later, in a new session
Secondsession. Update (CAT ); // Update cat
Secondsession. Update (mate ); // Update mate
IfCatid
Persistent identifierCat
Has beenAnother session (secondsession)
When the application performs reattach, an exception is thrown.
If youMake sure that the current session does not contain a persistent instance with the same persistence identifier.Update ()
. If you want to merge your changes at any time without considering the session status, useMerge ()
.In other words, in a new session, the first call is usuallyUpdate ()
The detached object is first executed.
Like statement
Public Collection findowners (string lastname) Throws Dataaccessexception {
ReturnGethibernatetemplate (). Find ("From owner where owner. lastname like?", Lastname+ "%");
}
Gethibernatetemplate (). Find ( " From owner where owner. lastname like? " , Lastname + " % " );