[hibernate-commits] Hibernate SVN: r14344 - in annotations/trunk: src/java/org/hibernate/annotations and 2 other directories.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Wed Feb 20 12:14:31 EST 2008


Author: epbernard
Date: 2008-02-20 12:14:31 -0500 (Wed, 20 Feb 2008)
New Revision: 14344

Added:
   annotations/trunk/src/java/org/hibernate/annotations/GenericGenerators.java
   annotations/trunk/src/test/org/hibernate/test/annotations/id/Monkey.java
Modified:
   annotations/trunk/doc/reference/en/modules/entity.xml
   annotations/trunk/src/java/org/hibernate/cfg/AnnotationBinder.java
   annotations/trunk/src/test/org/hibernate/test/annotations/id/IdTest.java
   annotations/trunk/src/test/org/hibernate/test/annotations/id/package-info.java
Log:
ANN-442 add @GenericGenerators (Paul Cowan)

Modified: annotations/trunk/doc/reference/en/modules/entity.xml
===================================================================
--- annotations/trunk/doc/reference/en/modules/entity.xml	2008-02-20 13:46:43 UTC (rev 14343)
+++ annotations/trunk/doc/reference/en/modules/entity.xml	2008-02-20 17:14:31 UTC (rev 14344)
@@ -2394,10 +2394,11 @@
 public class Carrot extends Vegetable { ... }</programlisting></para>
     </sect2>
 
-    <sect2 id="entity-hibspec-identifier" label="Identifier" revision="1">
+    <sect2 id="entity-hibspec-identifier" label="Identifier" revision="2">
       <title>Identifier</title>
 
       <para><literal><literal>@org.hibernate.annotations.GenericGenerator</literal>
+      and <literal>@org.hibernate.annotations.GenericGenerators</literal>
       allows you to define an Hibernate specific id
       generator.</literal></para>
 
@@ -2420,16 +2421,23 @@
       some parameters through the <literal>parameters</literal>
       attribute.</para>
 
-      <para>Contrary to its standard counterpart,
-      <literal>@GenericGenerator</literal> can be used in package level
-      annotations, making it an application level generator (just like if it
-      were in a JPA XML file).</para>
+      <para>Contrary to their standard counterpart,
+      <literal>@GenericGenerator</literal> and <literal>@GenericGenerators</literal>
+      can be used in package level annotations, making them application level generators
+      (just like if they were in a JPA XML file).</para>
 
-      <programlisting>@GenericGenerator(name="hibseq", strategy = "seqhilo",
-    parameters = {
-        @Parameter(name="max_lo", value = "5"),
-        @Parameter(name="sequence", value="heybabyhey")
-    }
+      <programlisting>@GenericGenerators(
+    {
+    @GenericGenerator(
+        name="hibseq",
+        strategy = "seqhilo",
+        parameters = {
+            @Parameter(name="max_lo", value = "5"),
+            @Parameter(name="sequence", value="heybabyhey")
+        }
+     ),
+     @GenericGenerator(...)
+     }
 )
 package org.hibernate.test.model</programlisting>
     </sect2>

Added: annotations/trunk/src/java/org/hibernate/annotations/GenericGenerators.java
===================================================================
--- annotations/trunk/src/java/org/hibernate/annotations/GenericGenerators.java	                        (rev 0)
+++ annotations/trunk/src/java/org/hibernate/annotations/GenericGenerators.java	2008-02-20 17:14:31 UTC (rev 14344)
@@ -0,0 +1,21 @@
+//$
+package org.hibernate.annotations;
+
+import static java.lang.annotation.ElementType.PACKAGE;
+import static java.lang.annotation.ElementType.TYPE;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+
+/**
+ * Array of generic generator definitions
+ *
+ * @author Paul Cowan
+ */
+ at Target({PACKAGE, TYPE})
+ at Retention(RUNTIME)
+public @interface GenericGenerators {
+	GenericGenerator[] value();
+}
+

Modified: annotations/trunk/src/java/org/hibernate/cfg/AnnotationBinder.java
===================================================================
--- annotations/trunk/src/java/org/hibernate/cfg/AnnotationBinder.java	2008-02-20 13:46:43 UTC (rev 14343)
+++ annotations/trunk/src/java/org/hibernate/cfg/AnnotationBinder.java	2008-02-20 17:14:31 UTC (rev 14344)
@@ -96,6 +96,7 @@
 import org.hibernate.annotations.TypeDef;
 import org.hibernate.annotations.TypeDefs;
 import org.hibernate.annotations.Where;
+import org.hibernate.annotations.GenericGenerators;
 import org.hibernate.annotations.common.reflection.ReflectionManager;
 import org.hibernate.annotations.common.reflection.XAnnotatedElement;
 import org.hibernate.annotations.common.reflection.XClass;
@@ -233,17 +234,31 @@
 			mappings.addGenerator( idGen );
 
 		}
-		if ( pckg.isAnnotationPresent( GenericGenerator.class ) ) {
-			GenericGenerator ann = pckg.getAnnotation( GenericGenerator.class );
-			IdGenerator idGen = buildIdGenerator( ann, mappings );
-			mappings.addGenerator( idGen );
-		}
+		bindGenericGenerators(pckg, mappings);
 		bindQueries( pckg, mappings );
 		bindFilterDefs( pckg, mappings );
 		bindTypeDefs( pckg, mappings );
 		BinderHelper.bindAnyMetaDefs( pckg, mappings );
 	}
 
+	private static void bindGenericGenerators(XAnnotatedElement annotatedElement, ExtendedMappings mappings) {
+		GenericGenerator defAnn = annotatedElement.getAnnotation( GenericGenerator.class );
+		GenericGenerators defsAnn = annotatedElement.getAnnotation( GenericGenerators.class );
+		if ( defAnn != null ) {
+			bindGenericGenerator( defAnn, mappings );
+		}
+		if ( defsAnn != null ) {
+			for (GenericGenerator def : defsAnn.value() ) {
+				bindGenericGenerator( def, mappings );
+			}
+		}
+	}
+
+	private static void bindGenericGenerator(GenericGenerator def, ExtendedMappings mappings) {
+		IdGenerator idGen = buildIdGenerator( def, mappings );
+		mappings.addGenerator( idGen );
+	}
+
 	private static void bindQueries(XAnnotatedElement annotatedElement, ExtendedMappings mappings) {
 		{
 			SqlResultSetMapping ann = annotatedElement.getAnnotation( SqlResultSetMapping.class );

Modified: annotations/trunk/src/test/org/hibernate/test/annotations/id/IdTest.java
===================================================================
--- annotations/trunk/src/test/org/hibernate/test/annotations/id/IdTest.java	2008-02-20 13:46:43 UTC (rev 14343)
+++ annotations/trunk/src/test/org/hibernate/test/annotations/id/IdTest.java	2008-02-20 17:14:31 UTC (rev 14344)
@@ -41,6 +41,21 @@
 
 	}
 
+	/*
+	 * Ensures that GenericGenerator annotations wrapped inside a GenericGenerators holder are
+	 * bound correctly
+	 */
+	public void testGenericGenerators() throws Exception {
+		Session s = openSession();
+		Transaction tx = s.beginTransaction();
+		Monkey monkey = new Monkey();
+		s.persist( monkey );
+		s.flush();
+		assertNotNull(monkey.getId());
+		tx.rollback();
+		s.close();
+	}
+
 	public void testTableGenerator() throws Exception {
 		Session s = openSession();
 		Transaction tx = s.beginTransaction();
@@ -264,7 +279,8 @@
 				SoundSystem.class,
 				Furniture.class,
 				GoalKeeper.class,
-				BreakDance.class
+				BreakDance.class,
+				Monkey.class
 		};
 	}
 

Added: annotations/trunk/src/test/org/hibernate/test/annotations/id/Monkey.java
===================================================================
--- annotations/trunk/src/test/org/hibernate/test/annotations/id/Monkey.java	                        (rev 0)
+++ annotations/trunk/src/test/org/hibernate/test/annotations/id/Monkey.java	2008-02-20 17:14:31 UTC (rev 14344)
@@ -0,0 +1,24 @@
+//$
+package org.hibernate.test.annotations.id;
+
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+
+/**
+ * @author Paul Cowan
+ */
+ at Entity
+public class Monkey {
+	private String id;
+
+	@Id
+	@GeneratedValue(generator = "system-uuid-2")
+	public String getId() {
+		return id;
+	}
+
+	public void setId(String id) {
+		this.id = id;
+	}
+}

Modified: annotations/trunk/src/test/org/hibernate/test/annotations/id/package-info.java
===================================================================
--- annotations/trunk/src/test/org/hibernate/test/annotations/id/package-info.java	2008-02-20 13:46:43 UTC (rev 14343)
+++ annotations/trunk/src/test/org/hibernate/test/annotations/id/package-info.java	2008-02-20 17:14:31 UTC (rev 14344)
@@ -3,7 +3,9 @@
  * Test package for metatata facilities
  * It contains an example of package level metadata
  */
+ at org.hibernate.annotations.GenericGenerator(name = "system-uuid", strategy = "uuid")
+ at org.hibernate.annotations.GenericGenerators(
+		@org.hibernate.annotations.GenericGenerator(name = "system-uuid-2", strategy = "uuid")
+)
+package org.hibernate.test.annotations.id;
 
- at org.hibernate.annotations.GenericGenerator(name = "system-uuid",
-		strategy = "uuid") package org.hibernate.test.annotations.id;
-




More information about the hibernate-commits mailing list