[hibernate-commits] Hibernate SVN: r11265 - 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
Fri Mar 9 11:28:58 EST 2007


Author: epbernard
Date: 2007-03-09 11:28:58 -0500 (Fri, 09 Mar 2007)
New Revision: 11265

Added:
   branches/Branch_3_2/HibernateExt/annotations/src/java/org/hibernate/annotations/Persister.java
   branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations/persister/
   branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations/persister/Card.java
   branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations/persister/CollectionPersister.java
   branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations/persister/Deck.java
   branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations/persister/EntityPersister.java
   branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations/persister/PersisterTest.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/annotations/CollectionBinder.java
   branches/Branch_3_2/HibernateExt/annotations/src/java/org/hibernate/cfg/annotations/EntityBinder.java
   branches/Branch_3_2/HibernateExt/annotations/src/java/org/hibernate/cfg/annotations/PropertyBinder.java
Log:
ANN-567 Persister for collections (Shawn Clowater)

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-09 04:24:33 UTC (rev 11264)
+++ branches/Branch_3_2/HibernateExt/annotations/doc/reference/en/modules/entity.xml	2007-03-09 16:28:58 UTC (rev 11265)
@@ -2268,10 +2268,6 @@
           </listitem>
 
           <listitem>
-             persister: allow the overriding of the default persister implementation 
-          </listitem>
-
-          <listitem>
              optimisticLock: optimistic locking strategy (OptimisticLockType.VERSION, OptimisticLockType.NONE, OptimisticLockType.DIRTY or OptimisticLockType.ALL) 
           </listitem>
         </itemizedlist></para>
@@ -2362,6 +2358,14 @@
       entity may not be updated or deleted by the application. This allows
       Hibernate to make some minor performance optimizations.</para>
 
+      <para><literal>@Persister</literal> lets you define your own custom
+      persistence strategy. You may, for example, specify your own subclass of
+      <classname>org.hibernate.persister.EntityPersister</classname> or you
+      might even provide a completely new implementation of the interface
+      <literal>org.hibernate.persister.ClassPersister</literal> that
+      implements persistence via, for example, stored procedure calls,
+      serialization to flat files or LDAP.</para>
+
       <para><programlisting>@Entity
 @BatchSize(size=5)
 @org.hibernate.annotations.Entity(
@@ -2371,6 +2375,7 @@
         polymorphism = PolymorphismType.EXPLICIT)
 @Where(clause="1=1")
 @org.hibernate.annotations.Table(name="Forest", indexes = { @Index(name="idx", columnNames = { "name", "length" } ) } )
+ at Persister(impl=MyEntityPersister.class)
 public class Forest { ... }</programlisting><programlisting>@Entity
 @Inheritance(
     strategy=InheritanceType.JOINED
@@ -2923,6 +2928,14 @@
             <listitem>
                the collection immutability using @Immutable: if set specifies that the elements of the collection never change (a minor performance optimization in some cases) 
             </listitem>
+
+            <listitem>
+              <para>a custom collection persister (ie the persistence strategy
+              used) using <literal>@Persister</literal>: the class must
+              implement
+              <classname>org.hibernate.persister.collectionCollectionPersister</classname>
+              </para>
+            </listitem>
           </itemizedlist></para>
 
         <para>You can also declare a sort comparator. Use the

Added: branches/Branch_3_2/HibernateExt/annotations/src/java/org/hibernate/annotations/Persister.java
===================================================================
--- branches/Branch_3_2/HibernateExt/annotations/src/java/org/hibernate/annotations/Persister.java	                        (rev 0)
+++ branches/Branch_3_2/HibernateExt/annotations/src/java/org/hibernate/annotations/Persister.java	2007-03-09 16:28:58 UTC (rev 11265)
@@ -0,0 +1,16 @@
+//$Id: $
+package org.hibernate.annotations;
+
+import java.lang.annotation.*;
+
+/**
+ * Specify a custom persister.
+ *
+ * @author Shawn Clowater
+ */
+ at java.lang.annotation.Target({ElementType.TYPE, ElementType.METHOD, ElementType.FIELD})
+ at Retention( RetentionPolicy.RUNTIME )
+public @interface Persister {
+	/** Custom persister */
+	Class impl();
+}

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-09 04:24:33 UTC (rev 11264)
+++ branches/Branch_3_2/HibernateExt/annotations/src/java/org/hibernate/cfg/annotations/CollectionBinder.java	2007-03-09 16:28:58 UTC (rev 11265)
@@ -47,6 +47,7 @@
 import org.hibernate.annotations.Loader;
 import org.hibernate.annotations.Immutable;
 import org.hibernate.annotations.OptimisticLock;
+import org.hibernate.annotations.Persister;
 import org.hibernate.cfg.AnnotatedClassType;
 import org.hibernate.cfg.AnnotationBinder;
 import org.hibernate.cfg.BinderHelper;
@@ -310,6 +311,8 @@
 		collection.setMutable( ! property.isAnnotationPresent( Immutable.class ) );
 		OptimisticLock lockAnn = property.getAnnotation( OptimisticLock.class );
 		if (lockAnn != null) collection.setOptimisticLocked( ! lockAnn.excluded() );
+		Persister persisterAnn = property.getAnnotation( Persister.class );
+		if ( persisterAnn != null ) collection.setCollectionPersisterClass( persisterAnn.impl() );		
 
 		// set ordering
 		if ( orderBy != null ) collection.setOrderBy( orderBy );

Modified: branches/Branch_3_2/HibernateExt/annotations/src/java/org/hibernate/cfg/annotations/EntityBinder.java
===================================================================
--- branches/Branch_3_2/HibernateExt/annotations/src/java/org/hibernate/cfg/annotations/EntityBinder.java	2007-03-09 04:24:33 UTC (rev 11264)
+++ branches/Branch_3_2/HibernateExt/annotations/src/java/org/hibernate/cfg/annotations/EntityBinder.java	2007-03-09 16:28:58 UTC (rev 11265)
@@ -39,6 +39,7 @@
 import org.hibernate.annotations.Tuplizer;
 import org.hibernate.annotations.Immutable;
 import org.hibernate.annotations.FetchMode;
+import org.hibernate.annotations.Persister;
 import org.hibernate.cache.CacheFactory;
 import org.hibernate.cfg.AnnotationBinder;
 import org.hibernate.cfg.BinderHelper;
@@ -79,7 +80,6 @@
 	private boolean dynamicUpdate;
 	private boolean explicitHibernateEntityAnnotation;
 	private OptimisticLockType optimisticLockType;
-	private String persister;
 	private PolymorphismType polymorphismType;
 	private boolean selectBeforeUpdate;
 	private int batchSize;
@@ -123,17 +123,16 @@
 			dynamicInsert = hibAnn.dynamicInsert();
 			dynamicUpdate = hibAnn.dynamicUpdate();
 			optimisticLockType = hibAnn.optimisticLock();
-			persister = hibAnn.persister();
 			selectBeforeUpdate = hibAnn.selectBeforeUpdate();
 			polymorphismType = hibAnn.polymorphism();
 			explicitHibernateEntityAnnotation = true;
+			//persister handled in bind
 		}
 		else {
 			//default values when the annotation is not there
 			dynamicInsert = false;
 			dynamicUpdate = false;
 			optimisticLockType = OptimisticLockType.VERSION;
-			persister = "";
 			polymorphismType = PolymorphismType.IMPLICIT;
 			selectBeforeUpdate = false;
 		}
@@ -200,14 +199,27 @@
 		}
 		persistentClass.setOptimisticLockMode( getVersioning( optimisticLockType ) );
 		persistentClass.setSelectBeforeUpdate( selectBeforeUpdate );
-		if ( !BinderHelper.isDefault( persister ) ) {
-			try {
-				persistentClass.setEntityPersisterClass( ReflectHelper.classForName( persister ) );
+
+		//set persister if needed
+		//@Persister has precedence over @Entity.persister
+		Persister persisterAnn = annotatedClass.getAnnotation( Persister.class );
+		Class persister = null;
+		if ( persisterAnn!= null ) {
+			persister = persisterAnn.impl();
+		}
+		else {
+			org.hibernate.annotations.Entity entityAnn = annotatedClass.getAnnotation( org.hibernate.annotations.Entity.class );
+			if (entityAnn != null && !BinderHelper.isDefault( entityAnn.persister() ) ) {
+				try {
+					persister = ReflectHelper.classForName( entityAnn.persister() );
+				}
+				catch (ClassNotFoundException cnfe) {
+					throw new AnnotationException( "Could not find persister class: " + persister );
+				}
 			}
-			catch (ClassNotFoundException cnfe) {
-				throw new AnnotationException( "Could not find persister class: " + persister );
-			}
 		}
+		if ( persister != null ) persistentClass.setEntityPersisterClass(persister);
+
 		persistentClass.setBatchSize( batchSize );
 
 		//SQL overriding

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-09 04:24:33 UTC (rev 11264)
+++ branches/Branch_3_2/HibernateExt/annotations/src/java/org/hibernate/cfg/annotations/PropertyBinder.java	2007-03-09 16:28:58 UTC (rev 11265)
@@ -155,7 +155,9 @@
 		}
 		prop.setInsertable( insertable );
 		prop.setUpdateable( updatable );
-		OptimisticLock lockAnn = property.getAnnotation( OptimisticLock.class );
+		OptimisticLock lockAnn = property != null ?
+				property.getAnnotation( OptimisticLock.class ) :
+				null;
 		if ( lockAnn != null) {
 			prop.setOptimisticLocked( ! lockAnn.excluded() );
 			//TODO this should go to the core as a mapping validation checking

Added: branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations/persister/Card.java
===================================================================
--- branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations/persister/Card.java	                        (rev 0)
+++ branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations/persister/Card.java	2007-03-09 16:28:58 UTC (rev 11265)
@@ -0,0 +1,21 @@
+package org.hibernate.test.annotations.persister;
+
+import java.io.Serializable;
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.JoinColumn;
+import javax.persistence.ManyToOne;
+
+/**
+ * @author Shawn Clowater
+ */
+ at Entity
+ at org.hibernate.annotations.Entity( persister = "org.hibernate.persister.entity.SingleTableEntityPersister" )
+public class Card implements Serializable {
+	@Id
+	public Integer id;
+
+	@ManyToOne()
+	@JoinColumn()
+	public Deck deck;
+}
\ No newline at end of file

Added: branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations/persister/CollectionPersister.java
===================================================================
--- branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations/persister/CollectionPersister.java	                        (rev 0)
+++ branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations/persister/CollectionPersister.java	2007-03-09 16:28:58 UTC (rev 11265)
@@ -0,0 +1,19 @@
+package org.hibernate.test.annotations.persister;
+
+import org.hibernate.MappingException;
+import org.hibernate.cache.CacheConcurrencyStrategy;
+import org.hibernate.cache.CacheException;
+import org.hibernate.cfg.Configuration;
+import org.hibernate.engine.SessionFactoryImplementor;
+import org.hibernate.mapping.Collection;
+import org.hibernate.persister.collection.OneToManyPersister;
+
+/**
+ * @author Shawn Clowater
+ */
+public class CollectionPersister extends OneToManyPersister {
+	public CollectionPersister(Collection collection, CacheConcurrencyStrategy cache, Configuration cfg,
+							   SessionFactoryImplementor factory) throws MappingException, CacheException {
+		super( collection, cache, cfg, factory );
+	}
+}
\ No newline at end of file

Added: branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations/persister/Deck.java
===================================================================
--- branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations/persister/Deck.java	                        (rev 0)
+++ branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations/persister/Deck.java	2007-03-09 16:28:58 UTC (rev 11265)
@@ -0,0 +1,25 @@
+package org.hibernate.test.annotations.persister;
+
+import java.io.Serializable;
+import java.util.Set;
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.OneToMany;
+
+import org.hibernate.annotations.Persister;
+
+
+/**
+ * @author Shawn Clowater
+ */
+ at Entity
+ at org.hibernate.annotations.Entity( persister = "org.hibernate.persister.entity.SingleTableEntityPersister" )
+ at Persister( impl = org.hibernate.test.annotations.persister.EntityPersister.class )
+public class Deck implements Serializable {
+	@Id
+	public Integer id;
+
+	@OneToMany( mappedBy = "deck" )
+	@Persister( impl = org.hibernate.test.annotations.persister.CollectionPersister.class )
+	public Set<Card> cards;
+}
\ No newline at end of file

Added: branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations/persister/EntityPersister.java
===================================================================
--- branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations/persister/EntityPersister.java	                        (rev 0)
+++ branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations/persister/EntityPersister.java	2007-03-09 16:28:58 UTC (rev 11265)
@@ -0,0 +1,18 @@
+package org.hibernate.test.annotations.persister;
+
+import org.hibernate.HibernateException;
+import org.hibernate.cache.CacheConcurrencyStrategy;
+import org.hibernate.engine.Mapping;
+import org.hibernate.engine.SessionFactoryImplementor;
+import org.hibernate.mapping.PersistentClass;
+import org.hibernate.persister.entity.SingleTableEntityPersister;
+
+/**
+ * @author Shawn Clowater
+ */
+public class EntityPersister extends SingleTableEntityPersister {
+	public EntityPersister(PersistentClass persistentClass, CacheConcurrencyStrategy cache,
+						   SessionFactoryImplementor factory, Mapping cfg) throws HibernateException {
+		super( persistentClass, cache, factory, cfg );
+	}
+}
\ No newline at end of file

Added: branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations/persister/PersisterTest.java
===================================================================
--- branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations/persister/PersisterTest.java	                        (rev 0)
+++ branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations/persister/PersisterTest.java	2007-03-09 16:28:58 UTC (rev 11265)
@@ -0,0 +1,47 @@
+package org.hibernate.test.annotations.persister;
+
+import org.hibernate.mapping.Collection;
+import org.hibernate.mapping.PersistentClass;
+import org.hibernate.persister.entity.SingleTableEntityPersister;
+import org.hibernate.test.annotations.TestCase;
+
+/**
+ * @author Shawn Clowater
+ */
+public class PersisterTest extends TestCase {
+	public PersisterTest(String x) {
+		super( x );
+	}
+
+	public void testEntityEntityPersisterAndPersisterSpecified() throws Exception {
+		//checks to see that the persister specified with the @Persister annotation takes precedence if a @Entity.persister() is also specified		
+		PersistentClass persistentClass = (PersistentClass) getCfg().getClassMapping( Deck.class.getName() );
+		assertEquals( "Incorrect Persister class for " + persistentClass.getMappedClass(), EntityPersister.class,
+				persistentClass.getEntityPersisterClass() );
+	}
+
+	public void testEntityEntityPersisterSpecified() throws Exception {
+		//tests the persister specified with an @Entity.persister()		
+		PersistentClass persistentClass = (PersistentClass) getCfg().getClassMapping( Card.class.getName() );
+		assertEquals( "Incorrect Persister class for " + persistentClass.getMappedClass(),
+				SingleTableEntityPersister.class, persistentClass.getEntityPersisterClass() );
+	}
+
+	public void testCollectionPersisterSpecified() throws Exception {
+		//tests the persister specified by the @Persister annotation on a collection
+		Collection collection = (Collection) getCfg().getCollectionMapping( Deck.class.getName() + ".cards" );
+		assertEquals( "Incorrect Persister class for collection " + collection.getRole(), CollectionPersister.class,
+				collection.getCollectionPersisterClass() );
+	}
+
+	/**
+	 * @see org.hibernate.test.annotations.TestCase#getMappings()
+	 */
+	protected Class[] getMappings() {
+		return new Class[]{
+				Card.class,
+				Deck.class
+		};
+	}
+
+}
\ No newline at end of file




More information about the hibernate-commits mailing list