Hibernate optimistic Lock (optimistic Locking)

Source: Internet
Author: User

1. Hibernate is implemented based on the data Versioning (version) recording mechanism. Adding a version ID to the data is typically done by adding a "version" field to the database table. When the data is read, the version number is read together, and then the version number is added one after the update. At this point, the version data of the submitted data is compared to the current version information of the database table corresponding to the record, and if the submitted version number is greater than the current version number of the database table, it is updated, otherwise it is considered to be outdated data.

2.

1 //$Id: Conductor.java 11282 2007-03-14 22:05:59z Epbernard $2  Packageorg.hibernate.test.annotations.various; 3   4 ImportJavax.persistence.Column; 5 Importjavax.persistence.Entity; 6 ImportJavax.persistence.GeneratedValue; 7 Importjavax.persistence.Id; 8 Importjavax.persistence.Version; 9   Ten ImportOrg.hibernate.annotations.Index;  One ImportOrg.hibernate.annotations.OptimisticLock;  A    -    - @Entity the  Public classConductor { - @Id - @GeneratedValue -     PrivateInteger ID;  +@Column (name = "Cond_name")   -@Index (name = "Cond_name")   +<span style= "Color:rgb (255, 0, 0); >//@OptimisticLock (excluded = true) </span> A    at     PrivateString name;  -@Column (name = "Cond_address")   -@Index (name = "Cond_address")   -<span style= "Color:rgb (255, 0, 0); > @OptimisticLock (excluded =true) </span> -    -     PrivateString address;  in @Version -     PrivateLong version;  to    + }   - address is the one I added. The Get Set method is also omitted. the Test: * Java Code $ //$Id: Indextest.java 11282 2007-03-14 22:05:59z Epbernard $Panax Notoginseng  Packageorg.hibernate.test.annotations.various;  -    the ...........................   +    A    the  Public classIndextestextendsTestCase { +     ...............   -    $@SuppressWarnings ("Unchecked")   $      Public voidTestversion ()throwsException { -            -Session session1=opensession ();  theSession session2=opensession ();  -Conductor stu1= (conductor) session1.createquery ("from conductor as a where a.name= ' Bob '"). Uniqueresult (); WuyiConductor stu2= (conductor) session2.createquery ("from conductor as a where a.name= ' Bob '"). Uniqueresult ();  the            -         //at this time, two version numbers are the same WuSystem.out.println ("v1=" +stu1.getversion () + "--v2=" +stu2.getversion ());  -            AboutTransaction tx1=session1.begintransaction ();  $Stu1.setname ("Session1");  - Tx1.commit ();  -         //at this time, the two version number is different, one of the version number is incremented -System.out.println ("v1=" +stu1.getversion () + "--v2=" +stu2.getversion ());  A            +Transaction tx2=session2.begintransaction ();  theStu2.setname ("Session2");  -            $ Tx2.rollback ();  the Session2.close ();  the Session1.close ();  the     }   the     ............   -}

Add the Testversion method.

Hibernate optimistic Lock (optimistic Locking)

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.