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)