[hibernate-commits] Hibernate SVN: r11264 - in branches/Branch_3_2/HibernateExt/annotations: src/java/org/hibernate/annotations and 3 other directories.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Thu Mar 8 23:24:33 EST 2007


Author: epbernard
Date: 2007-03-08 23:24:33 -0500 (Thu, 08 Mar 2007)
New Revision: 11264

Added:
   branches/Branch_3_2/HibernateExt/annotations/src/java/org/hibernate/annotations/OptimisticLock.java
   branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations/various/VersionTest.java
Modified:
   branches/Branch_3_2/HibernateExt/annotations/doc/reference/en/modules/entity.xml
   branches/Branch_3_2/HibernateExt/annotations/src/java/org/hibernate/cfg/AnnotationBinder.java
   branches/Branch_3_2/HibernateExt/annotations/src/java/org/hibernate/cfg/annotations/CollectionBinder.java
   branches/Branch_3_2/HibernateExt/annotations/src/java/org/hibernate/cfg/annotations/PropertyBinder.java
   branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations/various/Conductor.java
Log:
ANN-26 Support exclusion from optimistic locking scheme

Modified: branches/Branch_3_2/HibernateExt/annotations/doc/reference/en/modules/entity.xml
===================================================================
--- branches/Branch_3_2/HibernateExt/annotations/doc/reference/en/modules/entity.xml	2007-03-08 01:42:10 UTC (rev 11263)
+++ branches/Branch_3_2/HibernateExt/annotations/doc/reference/en/modules/entity.xml	2007-03-09 04:24:33 UTC (rev 11264)
@@ -2324,7 +2324,7 @@
       </note>
 
       <para><literal>@org.hibernate.annotations.Table</literal> can also be
-      used to define the following elements of secondary tables: </para>
+      used to define the following elements of secondary tables:</para>
 
       <itemizedlist>
         <listitem>
@@ -2349,7 +2349,7 @@
           <para><literal>optional</literal>: If enabled (the default),
           Hibernate will insert a row only if the properties defined by this
           join are non-null and will always use an outer join to retrieve the
-          properties. </para>
+          properties.</para>
         </listitem>
 
         <listitem>
@@ -2685,6 +2685,18 @@
 
         <para></para>
       </sect3>
+
+      <sect3>
+        <title>Optimistic lock</title>
+
+        <para>It is sometimes useful to avoid increasing the version number
+        even if a given property is dirty (particularly collections). You can
+        do that by annotating the property (or collection) with
+        <literal>@OptimisticLock(excluded=true)</literal>.</para>
+
+        <para>More formally, specifies that updates to this property do not
+        require acquisition of the optimistic lock.</para>
+      </sect3>
     </sect2>
 
     <sect2 id="entity-hibspec-inheritance" revision="2">
@@ -3414,7 +3426,7 @@
     </sect2>
 
     <sect2 id="entity-hibspec-customsql" revision="1">
-      <title> Custom SQL for CRUD operations</title>
+      <title>Custom SQL for CRUD operations</title>
 
       <para>Hibernate gives you the avility to override every single SQL
       statement generated. We have seen native SQL query usage already, but

Added: branches/Branch_3_2/HibernateExt/annotations/src/java/org/hibernate/annotations/OptimisticLock.java
===================================================================
--- branches/Branch_3_2/HibernateExt/annotations/src/java/org/hibernate/annotations/OptimisticLock.java	                        (rev 0)
+++ branches/Branch_3_2/HibernateExt/annotations/src/java/org/hibernate/annotations/OptimisticLock.java	2007-03-09 04:24:33 UTC (rev 11264)
@@ -0,0 +1,24 @@
+//$Id: $
+package org.hibernate.annotations;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Whether or not update entity's version on property's change
+ * If the annotation is not present, the property is involved in the optimistic lock srategy (default)
+ *
+ * @author Logi Ragnarsson
+ */
+ at Target( {ElementType.METHOD, ElementType.FIELD} )
+ at Retention( RetentionPolicy.RUNTIME )
+public @interface OptimisticLock {
+
+	/**
+	 * If true, the annotated property change will not trigger a version upgrade
+	 */
+	boolean excluded();
+
+}

Modified: branches/Branch_3_2/HibernateExt/annotations/src/java/org/hibernate/cfg/AnnotationBinder.java
===================================================================
--- branches/Branch_3_2/HibernateExt/annotations/src/java/org/hibernate/cfg/AnnotationBinder.java	2007-03-08 01:42:10 UTC (rev 11263)
+++ branches/Branch_3_2/HibernateExt/annotations/src/java/org/hibernate/cfg/AnnotationBinder.java	2007-03-09 04:24:33 UTC (rev 11264)
@@ -1201,7 +1201,7 @@
 						|| property.isAnnotationPresent( EmbeddedId.class ) ) ) {
 			if ( isIdentifierMapper ) {
 				throw new AnnotationException(
-						"@IdClass class should not have @Id nor @EmbeddedId proeperties"
+						"@IdClass class should not have @Id nor @EmbeddedId properties"
 				);
 			}
 			log.debug( inferredData.getPropertyName() + " is an id" );

Modified: branches/Branch_3_2/HibernateExt/annotations/src/java/org/hibernate/cfg/annotations/CollectionBinder.java
===================================================================
--- branches/Branch_3_2/HibernateExt/annotations/src/java/org/hibernate/cfg/annotations/CollectionBinder.java	2007-03-08 01:42:10 UTC (rev 11263)
+++ branches/Branch_3_2/HibernateExt/annotations/src/java/org/hibernate/cfg/annotations/CollectionBinder.java	2007-03-09 04:24:33 UTC (rev 11264)
@@ -46,6 +46,7 @@
 import org.hibernate.annotations.SQLDeleteAll;
 import org.hibernate.annotations.Loader;
 import org.hibernate.annotations.Immutable;
+import org.hibernate.annotations.OptimisticLock;
 import org.hibernate.cfg.AnnotatedClassType;
 import org.hibernate.cfg.AnnotationBinder;
 import org.hibernate.cfg.BinderHelper;
@@ -307,6 +308,8 @@
 		}
 
 		collection.setMutable( ! property.isAnnotationPresent( Immutable.class ) );
+		OptimisticLock lockAnn = property.getAnnotation( OptimisticLock.class );
+		if (lockAnn != null) collection.setOptimisticLocked( ! lockAnn.excluded() );
 
 		// set ordering
 		if ( orderBy != null ) collection.setOrderBy( orderBy );

Modified: branches/Branch_3_2/HibernateExt/annotations/src/java/org/hibernate/cfg/annotations/PropertyBinder.java
===================================================================
--- branches/Branch_3_2/HibernateExt/annotations/src/java/org/hibernate/cfg/annotations/PropertyBinder.java	2007-03-08 01:42:10 UTC (rev 11263)
+++ branches/Branch_3_2/HibernateExt/annotations/src/java/org/hibernate/cfg/annotations/PropertyBinder.java	2007-03-09 04:24:33 UTC (rev 11264)
@@ -1,11 +1,15 @@
 //$Id$
 package org.hibernate.cfg.annotations;
 
+import javax.persistence.Id;
+import javax.persistence.EmbeddedId;
+
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.hibernate.AnnotationException;
 import org.hibernate.annotations.Generated;
 import org.hibernate.annotations.GenerationTime;
+import org.hibernate.annotations.OptimisticLock;
 import org.hibernate.cfg.Ejb3Column;
 import org.hibernate.cfg.ExtendedMappings;
 import org.hibernate.cfg.PropertyHolder;
@@ -151,6 +155,18 @@
 		}
 		prop.setInsertable( insertable );
 		prop.setUpdateable( updatable );
+		OptimisticLock lockAnn = property.getAnnotation( OptimisticLock.class );
+		if ( lockAnn != null) {
+			prop.setOptimisticLocked( ! lockAnn.excluded() );
+			//TODO this should go to the core as a mapping validation checking
+			if ( lockAnn.excluded() && (
+					property.isAnnotationPresent( javax.persistence.Version.class )
+					|| property.isAnnotationPresent( Id.class )
+					|| property.isAnnotationPresent( EmbeddedId.class ) ) ) {
+				throw new AnnotationException("@OptimisticLock.exclude=true incompatible with @Id, @EmbeddedId and @Version: "
+						+ StringHelper.qualify( holder.getPath(), name ) );
+			}
+		}
 		log.trace( "Cascading " + name + " with " + cascade );
 		return prop;
 	}

Modified: branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations/various/Conductor.java
===================================================================
--- branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations/various/Conductor.java	2007-03-08 01:42:10 UTC (rev 11263)
+++ branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations/various/Conductor.java	2007-03-09 04:24:33 UTC (rev 11264)
@@ -5,8 +5,10 @@
 import javax.persistence.Entity;
 import javax.persistence.GeneratedValue;
 import javax.persistence.Id;
+import javax.persistence.Version;
 
 import org.hibernate.annotations.Index;
+import org.hibernate.annotations.OptimisticLock;
 
 /**
  * @author Emmanuel Bernard
@@ -19,8 +21,21 @@
 
 	@Column(name = "cond_name")
 	@Index(name = "cond_name")
+	@OptimisticLock(excluded = true)
 	private String name;
 
+	@Version
+	private Long version;
+
+
+	public Long getVersion() {
+		return version;
+	}
+
+	public void setVersion(Long version) {
+		this.version = version;
+	}
+
 	public Integer getId() {
 		return id;
 	}

Added: branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations/various/VersionTest.java
===================================================================
--- branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations/various/VersionTest.java	                        (rev 0)
+++ branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations/various/VersionTest.java	2007-03-09 04:24:33 UTC (rev 11264)
@@ -0,0 +1,41 @@
+//$Id: $
+package org.hibernate.test.annotations.various;
+
+import org.hibernate.Session;
+import org.hibernate.test.annotations.TestCase;
+
+/**
+ * @author Emmanuel Bernard
+ */
+public class VersionTest extends TestCase {
+
+	public void testOptimisticLockDisabled() throws Exception {
+		Conductor c = new Conductor();
+		c.setName( "Bob" );
+		Session s = openSession( );
+		s.getTransaction().begin();
+		s.persist( c );
+		s.flush();
+
+		s.clear();
+
+		c = (Conductor) s.get( Conductor.class, c.getId() );
+		Long version = c.getVersion();
+		c.setName( "Don" );
+		s.flush();
+
+		s.clear();
+
+		c = (Conductor) s.get( Conductor.class, c.getId() );
+		assertEquals( version, c.getVersion() );
+
+		s.getTransaction().rollback();
+		s.close();
+	}
+
+	protected Class[] getMappings() {
+		return new Class[] {
+				Conductor.class
+		};
+	}
+}




More information about the hibernate-commits mailing list