Modify a detached object

Source: Internet
Author: User
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

IfCatidPersistent identifierCatHas 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 +   " % " );

 

 

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.