From hibernate-commits at lists.jboss.org Thu Mar 8 23:24:33 2007 Content-Type: multipart/mixed; boundary="===============3961113038093189344==" MIME-Version: 1.0 From: hibernate-commits at lists.jboss.org To: hibernate-commits at lists.jboss.org Subject: [hibernate-commits] Hibernate SVN: r11264 - in branches/Branch_3_2/HibernateExt/annotations: src/java/org/hibernate/annotations and 3 other directories. Date: Thu, 08 Mar 2007 23:24:33 -0500 Message-ID: --===============3961113038093189344== Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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/anno= tations/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/en= tity.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/mod= ules/entity.xml =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- branches/Branch_3_2/HibernateExt/annotations/doc/reference/en/modules/e= ntity.xml 2007-03-08 01:42:10 UTC (rev 11263) +++ branches/Branch_3_2/HibernateExt/annotations/doc/reference/en/modules/e= ntity.xml 2007-03-09 04:24:33 UTC (rev 11264) @@ -2324,7 +2324,7 @@ = @org.hibernate.annotations.Table can also be - used to define the following elements of secondary tables: + used to define the following elements of secondary tables: = @@ -2349,7 +2349,7 @@ optional: If enabled (the default), Hibernate will insert a row only if the properties defined by th= is join are non-null and will always use an outer join to retrieve = the - properties. + properties. = @@ -2685,6 +2685,18 @@ = + + + Optimistic lock + + 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 + @OptimisticLock(excluded=3Dtrue). + + More formally, specifies that updates to this property do not + require acquisition of the optimistic lock. + = @@ -3414,7 +3426,7 @@ = - Custom SQL for CRUD operations + Custom SQL for CRUD operations = 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 =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- branches/Branch_3_2/HibernateExt/annotations/src/java/org/hibernate/ann= otations/OptimisticLock.java (rev 0) +++ branches/Branch_3_2/HibernateExt/annotations/src/java/org/hibernate/ann= otations/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 optim= istic lock srategy (default) + * + * @author Logi Ragnarsson + */ +(a)Target( {ElementType.METHOD, ElementType.FIELD} ) +(a)Retention( RetentionPolicy.RUNTIME ) +public @interface OptimisticLock { + + /** + * If true, the annotated property change will not trigger a version upgr= ade + */ + boolean excluded(); + +} Modified: branches/Branch_3_2/HibernateExt/annotations/src/java/org/hiberna= te/cfg/AnnotationBinder.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- 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/hiberna= te/cfg/annotations/CollectionBinder.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- 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 =3D property.getAnnotation( OptimisticLock.class = ); + if (lockAnn !=3D null) collection.setOptimisticLocked( ! lockAnn.exclude= d() ); = // set ordering if ( orderBy !=3D null ) collection.setOrderBy( orderBy ); Modified: branches/Branch_3_2/HibernateExt/annotations/src/java/org/hiberna= te/cfg/annotations/PropertyBinder.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- 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 =3D property.getAnnotation( OptimisticLock.class = ); + if ( lockAnn !=3D 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=3Dtrue incompat= ible 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/hiberna= te/test/annotations/various/Conductor.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/tes= t/annotations/various/Conductor.java 2007-03-08 01:42:10 UTC (rev 11263) +++ branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/tes= t/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 =3D "cond_name") @Index(name =3D "cond_name") + @OptimisticLock(excluded =3D true) private String name; = + @Version + private Long version; + + + public Long getVersion() { + return version; + } + + public void setVersion(Long version) { + this.version =3D version; + } + public Integer getId() { return id; } Added: branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/= test/annotations/various/VersionTest.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/tes= t/annotations/various/VersionTest.java (rev 0) +++ branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/tes= t/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 =3D new Conductor(); + c.setName( "Bob" ); + Session s =3D openSession( ); + s.getTransaction().begin(); + s.persist( c ); + s.flush(); + + s.clear(); + + c =3D (Conductor) s.get( Conductor.class, c.getId() ); + Long version =3D c.getVersion(); + c.setName( "Don" ); + s.flush(); + + s.clear(); + + c =3D (Conductor) s.get( Conductor.class, c.getId() ); + assertEquals( version, c.getVersion() ); + + s.getTransaction().rollback(); + s.close(); + } + + protected Class[] getMappings() { + return new Class[] { + Conductor.class + }; + } +} --===============3961113038093189344==--