[hibernate-commits] Hibernate SVN: r20749 - in core/trunk: core/src/main/java/org/hibernate/cfg and 3 other directories.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Wed Sep 29 06:45:09 EDT 2010


Author: hardy.ferentschik
Date: 2010-09-29 06:45:08 -0400 (Wed, 29 Sep 2010)
New Revision: 20749

Added:
   core/trunk/core/src/main/java/org/hibernate/annotations/DiscriminatorOptions.java
   core/trunk/testsuite/src/test/java/org/hibernate/test/annotations/inheritance/discriminatoroptions/
   core/trunk/testsuite/src/test/java/org/hibernate/test/annotations/inheritance/discriminatoroptions/BaseClass.java
   core/trunk/testsuite/src/test/java/org/hibernate/test/annotations/inheritance/discriminatoroptions/DiscriminatorOptionsTest.java
   core/trunk/testsuite/src/test/java/org/hibernate/test/annotations/inheritance/discriminatoroptions/SubClass.java
Modified:
   core/trunk/core/src/main/java/org/hibernate/annotations/ForceDiscriminator.java
   core/trunk/core/src/main/java/org/hibernate/cfg/AnnotationBinder.java
   core/trunk/core/src/main/java/org/hibernate/cfg/annotations/EntityBinder.java
Log:
HHH-5190 - Introduced new annotation @DiscriminatorOptions and deprecated @ForceDiscriminator

Copied: core/trunk/core/src/main/java/org/hibernate/annotations/DiscriminatorOptions.java (from rev 20741, core/trunk/core/src/main/java/org/hibernate/annotations/DiscriminatorFormula.java)
===================================================================
--- core/trunk/core/src/main/java/org/hibernate/annotations/DiscriminatorOptions.java	                        (rev 0)
+++ core/trunk/core/src/main/java/org/hibernate/annotations/DiscriminatorOptions.java	2010-09-29 10:45:08 UTC (rev 20749)
@@ -0,0 +1,56 @@
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2010, Red Hat Inc. or third-party contributors as
+ * indicated by the @author tags or express copyright attribution
+ * statements applied by the authors.  All third-party contributions are
+ * distributed under license by Red Hat Inc.
+ *
+ * This copyrighted material is made available to anyone wishing to use, modify,
+ * copy, or redistribute it subject to the terms and conditions of the GNU
+ * Lesser General Public License, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this distribution; if not, write to:
+ * Free Software Foundation, Inc.
+ * 51 Franklin Street, Fifth Floor
+ * Boston, MA  02110-1301  USA
+ */
+// $Id:$
+package org.hibernate.annotations;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+
+import static java.lang.annotation.ElementType.TYPE;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+
+/**
+ * Optional annotation to express Hibernate specific discrimintor properties.
+ *
+ * @author Hardy Ferentschik
+ */
+ at Target({ TYPE })
+ at Retention(RUNTIME)
+public @interface DiscriminatorOptions {
+	/**
+	 * "Forces" Hibernate to specify the allowed discriminator values, even when retrieving all instances of the root class.
+	 *
+	 * @return {@code true} in case the discriminator value should be forces, {@code false} otherwise. Default is {@code false}.
+	 */
+	boolean force() default false;
+
+	/**
+	 * Set this to {@code false}, if your discriminator column is also part of a mapped composite identifier.
+	 * It tells Hibernate not to include the column in SQL INSERTs.
+	 *
+	 * @return {@code true} in case the discriminator value should be included in inserts, {@code false} otherwise.
+	 *         Default is {@code true}.
+	 */
+	boolean insert() default true;
+}


Property changes on: core/trunk/core/src/main/java/org/hibernate/annotations/DiscriminatorOptions.java
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Modified: core/trunk/core/src/main/java/org/hibernate/annotations/ForceDiscriminator.java
===================================================================
--- core/trunk/core/src/main/java/org/hibernate/annotations/ForceDiscriminator.java	2010-09-29 09:44:15 UTC (rev 20748)
+++ core/trunk/core/src/main/java/org/hibernate/annotations/ForceDiscriminator.java	2010-09-29 10:45:08 UTC (rev 20749)
@@ -23,16 +23,19 @@
  */
 package org.hibernate.annotations;
 
-import java.lang.annotation.Target;
 import java.lang.annotation.ElementType;
 import java.lang.annotation.Retention;
 import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
 
 /**
  * ForceDiscriminator flag
  * To be placed at the root entity near @DiscriminatorColumn or @DiscriminatorFormula
  *
  * @author Serg Prasolov
+ * @deprecated use {@link org.hibernate.annotations.DiscriminatorOptions} instead
  */
- at Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME)
-public @interface ForceDiscriminator {}
+ at Target({ ElementType.TYPE })
+ at Retention(RetentionPolicy.RUNTIME)
+public @interface ForceDiscriminator {
+}

Modified: core/trunk/core/src/main/java/org/hibernate/cfg/AnnotationBinder.java
===================================================================
--- core/trunk/core/src/main/java/org/hibernate/cfg/AnnotationBinder.java	2010-09-29 09:44:15 UTC (rev 20748)
+++ core/trunk/core/src/main/java/org/hibernate/cfg/AnnotationBinder.java	2010-09-29 10:45:08 UTC (rev 20749)
@@ -97,6 +97,7 @@
 import org.hibernate.annotations.CollectionId;
 import org.hibernate.annotations.CollectionOfElements;
 import org.hibernate.annotations.Columns;
+import org.hibernate.annotations.DiscriminatorOptions;
 import org.hibernate.annotations.Fetch;
 import org.hibernate.annotations.FetchProfile;
 import org.hibernate.annotations.FetchProfiles;
@@ -104,6 +105,7 @@
 import org.hibernate.annotations.FilterDef;
 import org.hibernate.annotations.FilterDefs;
 import org.hibernate.annotations.Filters;
+import org.hibernate.annotations.ForceDiscriminator;
 import org.hibernate.annotations.ForeignKey;
 import org.hibernate.annotations.Formula;
 import org.hibernate.annotations.GenericGenerator;
@@ -533,6 +535,16 @@
 				clazzToProcess, inheritanceStatePerClass, mappings, inheritanceState
 		);
 
+		PersistentClass persistentClass = makePersistentClass( inheritanceState, superEntity );
+		Entity entityAnn = clazzToProcess.getAnnotation( Entity.class );
+		org.hibernate.annotations.Entity hibEntityAnn = clazzToProcess.getAnnotation(
+				org.hibernate.annotations.Entity.class
+		);
+		EntityBinder entityBinder = new EntityBinder(
+				entityAnn, hibEntityAnn, clazzToProcess, persistentClass, mappings
+		);
+		entityBinder.setInheritanceState( inheritanceState );
+
 		bindQueries( clazzToProcess, mappings );
 		bindFilterDefs( clazzToProcess, mappings );
 		bindTypeDefs( clazzToProcess, mappings );
@@ -555,57 +567,17 @@
 				clazzToProcess, mappings, inheritanceState, superEntity
 		);
 		Ejb3DiscriminatorColumn discriminatorColumn = null;
-		String discrimValue = null;
 		if ( InheritanceType.SINGLE_TABLE.equals( inheritanceState.getType() ) ) {
-			javax.persistence.DiscriminatorColumn discAnn = clazzToProcess.getAnnotation(
-					javax.persistence.DiscriminatorColumn.class
+			discriminatorColumn = processDiscriminatorProperties(
+					clazzToProcess, mappings, inheritanceState, entityBinder
 			);
-			DiscriminatorType discriminatorType = discAnn != null ?
-					discAnn.discriminatorType() :
-					DiscriminatorType.STRING;
-
-			org.hibernate.annotations.DiscriminatorFormula discFormulaAnn = clazzToProcess.getAnnotation(
-					org.hibernate.annotations.DiscriminatorFormula.class
-			);
-			if ( !inheritanceState.hasParents() ) {
-				discriminatorColumn = Ejb3DiscriminatorColumn.buildDiscriminatorColumn(
-						discriminatorType, discAnn, discFormulaAnn, mappings
-				);
-			}
-			if ( discAnn != null && inheritanceState.hasParents() ) {
-				log.warn(
-						"Discriminator column has to be defined in the root entity, it will be ignored in subclass: {}",
-						clazzToProcess.getName()
-				);
-			}
-
-			discrimValue = clazzToProcess.isAnnotationPresent( DiscriminatorValue.class ) ?
-					clazzToProcess.getAnnotation( DiscriminatorValue.class ).value() :
-					null;
 		}
 
-		PersistentClass persistentClass = makePersistentClass( inheritanceState, superEntity );
+		entityBinder.setProxy( clazzToProcess.getAnnotation( Proxy.class ) );
+		entityBinder.setBatchSize( clazzToProcess.getAnnotation( BatchSize.class ) );
+		entityBinder.setWhere( clazzToProcess.getAnnotation( Where.class ) );
+	    entityBinder.setCache( determineCacheSettings( clazzToProcess, mappings ) );
 
-		Proxy proxyAnn = clazzToProcess.getAnnotation( Proxy.class );
-		BatchSize sizeAnn = clazzToProcess.getAnnotation( BatchSize.class );
-		Where whereAnn = clazzToProcess.getAnnotation( Where.class );
-		Entity entityAnn = clazzToProcess.getAnnotation( Entity.class );
-		org.hibernate.annotations.Entity hibEntityAnn = clazzToProcess.getAnnotation(
-				org.hibernate.annotations.Entity.class
-		);
-
-		Cache cacheAnn = determineCacheSettings( clazzToProcess, mappings );
-
-		EntityBinder entityBinder = new EntityBinder(
-				entityAnn, hibEntityAnn, clazzToProcess, persistentClass, mappings
-		);
-		entityBinder.setDiscriminatorValue( discrimValue );
-		entityBinder.setBatchSize( sizeAnn );
-		entityBinder.setProxy( proxyAnn );
-		entityBinder.setWhere( whereAnn );
-		entityBinder.setCache( cacheAnn );
-		entityBinder.setInheritanceState( inheritanceState );
-
 		//Filters are not allowed on subclasses
 		if ( !inheritanceState.hasParents() ) {
 			bindFilters( clazzToProcess, entityBinder, mappings );
@@ -763,6 +735,50 @@
 
 	}
 
+	// parse everything discriminator column relevant in case of single table inheritance
+	private static Ejb3DiscriminatorColumn processDiscriminatorProperties(XClass clazzToProcess, Mappings mappings, InheritanceState inheritanceState, EntityBinder entityBinder) {
+		Ejb3DiscriminatorColumn discriminatorColumn = null;
+		javax.persistence.DiscriminatorColumn discAnn = clazzToProcess.getAnnotation(
+				javax.persistence.DiscriminatorColumn.class
+		);
+		DiscriminatorType discriminatorType = discAnn != null ?
+				discAnn.discriminatorType() :
+				DiscriminatorType.STRING;
+
+		org.hibernate.annotations.DiscriminatorFormula discFormulaAnn = clazzToProcess.getAnnotation(
+				org.hibernate.annotations.DiscriminatorFormula.class
+		);
+		if ( !inheritanceState.hasParents() ) {
+			discriminatorColumn = Ejb3DiscriminatorColumn.buildDiscriminatorColumn(
+					discriminatorType, discAnn, discFormulaAnn, mappings
+			);
+		}
+		if ( discAnn != null && inheritanceState.hasParents() ) {
+			log.warn(
+					"Discriminator column has to be defined in the root entity, it will be ignored in subclass: {}",
+					clazzToProcess.getName()
+			);
+		}
+
+		String discrimValue = clazzToProcess.isAnnotationPresent( DiscriminatorValue.class ) ?
+				clazzToProcess.getAnnotation( DiscriminatorValue.class ).value() :
+				null;
+		entityBinder.setDiscriminatorValue( discrimValue );
+
+		if ( clazzToProcess.isAnnotationPresent( ForceDiscriminator.class ) ) {
+			log.warn( "@ForceDiscriminator is deprecated use @DiscriminatorOptions instead." );
+			entityBinder.setForceDiscriminator( true );
+		}
+
+		DiscriminatorOptions discriminatorOptions = clazzToProcess.getAnnotation( DiscriminatorOptions.class );
+		if ( discriminatorOptions != null) {
+			entityBinder.setForceDiscriminator( discriminatorOptions.force() );
+			entityBinder.setInsertableDiscriminator( discriminatorOptions.insert() );
+		}
+
+		return discriminatorColumn;
+	}
+
 	private static void processIdPropertiesIfNotAlready(
 			Map<XClass, InheritanceState> inheritanceStatePerClass,
 			Mappings mappings,

Modified: core/trunk/core/src/main/java/org/hibernate/cfg/annotations/EntityBinder.java
===================================================================
--- core/trunk/core/src/main/java/org/hibernate/cfg/annotations/EntityBinder.java	2010-09-29 09:44:15 UTC (rev 20748)
+++ core/trunk/core/src/main/java/org/hibernate/cfg/annotations/EntityBinder.java	2010-09-29 10:45:08 UTC (rev 20749)
@@ -43,7 +43,6 @@
 import org.hibernate.annotations.Cache;
 import org.hibernate.annotations.CacheConcurrencyStrategy;
 import org.hibernate.annotations.FetchMode;
-import org.hibernate.annotations.ForceDiscriminator;
 import org.hibernate.annotations.Immutable;
 import org.hibernate.annotations.Loader;
 import org.hibernate.annotations.OptimisticLockType;
@@ -102,6 +101,8 @@
 	private Mappings mappings;
 	private Logger log = LoggerFactory.getLogger( EntityBinder.class );
 	private String discriminatorValue = "";
+	private Boolean forceDiscriminator;
+	private Boolean insertableDiscriminator;
 	private boolean dynamicInsert;
 	private boolean dynamicUpdate;
 	private boolean explicitHibernateEntityAnnotation;
@@ -182,6 +183,14 @@
 		this.discriminatorValue = discriminatorValue;
 	}
 
+	public void setForceDiscriminator(boolean forceDiscriminator) {
+		this.forceDiscriminator = forceDiscriminator;
+	}
+
+	public void setInsertableDiscriminator(boolean insertableDiscriminator) {
+		this.insertableDiscriminator = insertableDiscriminator;
+	}
+
 	public void bindEntity() {
 		persistentClass.setAbstract( annotatedClass.isAbstract() );
 		persistentClass.setClassName( annotatedClass.getName() );
@@ -219,7 +228,12 @@
 				rootClass.setCacheRegionName( cacheRegion );
 				rootClass.setLazyPropertiesCacheable( cacheLazyProperty );
 			}
-			rootClass.setForceDiscriminator( annotatedClass.isAnnotationPresent( ForceDiscriminator.class ) );
+			if(forceDiscriminator != null) {
+				rootClass.setForceDiscriminator( forceDiscriminator );
+			}
+			if( insertableDiscriminator != null) {
+				rootClass.setDiscriminatorInsertable( insertableDiscriminator );
+			}
 		}
 		else {
 			if ( explicitHibernateEntityAnnotation ) {

Added: core/trunk/testsuite/src/test/java/org/hibernate/test/annotations/inheritance/discriminatoroptions/BaseClass.java
===================================================================
--- core/trunk/testsuite/src/test/java/org/hibernate/test/annotations/inheritance/discriminatoroptions/BaseClass.java	                        (rev 0)
+++ core/trunk/testsuite/src/test/java/org/hibernate/test/annotations/inheritance/discriminatoroptions/BaseClass.java	2010-09-29 10:45:08 UTC (rev 20749)
@@ -0,0 +1,47 @@
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2010, Red Hat Inc. or third-party contributors as
+ * indicated by the @author tags or express copyright attribution
+ * statements applied by the authors.  All third-party contributions are
+ * distributed under license by Red Hat Inc.
+ *
+ * This copyrighted material is made available to anyone wishing to use, modify,
+ * copy, or redistribute it subject to the terms and conditions of the GNU
+ * Lesser General Public License, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this distribution; if not, write to:
+ * Free Software Foundation, Inc.
+ * 51 Franklin Street, Fifth Floor
+ * Boston, MA  02110-1301  USA
+ */
+
+// $Id:$
+package org.hibernate.test.annotations.inheritance.discriminatoroptions;
+
+import javax.persistence.DiscriminatorValue;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+
+import org.hibernate.annotations.DiscriminatorOptions;
+
+/**
+ * @author Hardy Ferentschik
+ */
+ at Entity
+ at DiscriminatorValue("B")
+ at DiscriminatorOptions(force = true, insert = false)
+public class BaseClass {
+	@Id
+	@GeneratedValue
+	private long id;
+}
+
+


Property changes on: core/trunk/testsuite/src/test/java/org/hibernate/test/annotations/inheritance/discriminatoroptions/BaseClass.java
___________________________________________________________________
Name: svn:keywords
   + Id

Copied: core/trunk/testsuite/src/test/java/org/hibernate/test/annotations/inheritance/discriminatoroptions/DiscriminatorOptionsTest.java (from rev 20741, core/trunk/testsuite/src/test/java/org/hibernate/test/annotations/various/TimestampTest.java)
===================================================================
--- core/trunk/testsuite/src/test/java/org/hibernate/test/annotations/inheritance/discriminatoroptions/DiscriminatorOptionsTest.java	                        (rev 0)
+++ core/trunk/testsuite/src/test/java/org/hibernate/test/annotations/inheritance/discriminatoroptions/DiscriminatorOptionsTest.java	2010-09-29 10:45:08 UTC (rev 20749)
@@ -0,0 +1,56 @@
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2010, Red Hat Inc. or third-party contributors as
+ * indicated by the @author tags or express copyright attribution
+ * statements applied by the authors.  All third-party contributions are
+ * distributed under license by Red Hat Inc.
+ *
+ * This copyrighted material is made available to anyone wishing to use, modify,
+ * copy, or redistribute it subject to the terms and conditions of the GNU
+ * Lesser General Public License, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this distribution; if not, write to:
+ * Free Software Foundation, Inc.
+ * 51 Franklin Street, Fifth Floor
+ * Boston, MA  02110-1301  USA
+ */
+
+//$Id$
+package org.hibernate.test.annotations.inheritance.discriminatoroptions;
+
+import org.hibernate.mapping.PersistentClass;
+import org.hibernate.mapping.RootClass;
+import org.hibernate.test.annotations.TestCase;
+
+/**
+ * Test for the @DiscriminatorOptions annotations.
+ *
+ * @author Hardy Ferentschik
+ */
+public class DiscriminatorOptionsTest extends TestCase {
+
+	public void testNonDefaultOptions() throws Exception {
+		buildConfiguration();
+
+		PersistentClass persistentClass = cfg.getClassMapping( BaseClass.class.getName() );
+		assertNotNull( persistentClass );
+		assertTrue( persistentClass instanceof RootClass );
+
+		RootClass root = ( RootClass ) persistentClass;
+		assertTrue( "Discriminator should be forced", root.isForceDiscriminator() );
+		assertFalse( "Discriminator should not be insertable", root.isDiscriminatorInsertable() );
+	}
+
+	protected Class[] getAnnotatedClasses() {
+		return new Class[] {
+				BaseClass.class, SubClass.class
+		};
+	}
+}

Added: core/trunk/testsuite/src/test/java/org/hibernate/test/annotations/inheritance/discriminatoroptions/SubClass.java
===================================================================
--- core/trunk/testsuite/src/test/java/org/hibernate/test/annotations/inheritance/discriminatoroptions/SubClass.java	                        (rev 0)
+++ core/trunk/testsuite/src/test/java/org/hibernate/test/annotations/inheritance/discriminatoroptions/SubClass.java	2010-09-29 10:45:08 UTC (rev 20749)
@@ -0,0 +1,42 @@
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2010, Red Hat Inc. or third-party contributors as
+ * indicated by the @author tags or express copyright attribution
+ * statements applied by the authors.  All third-party contributions are
+ * distributed under license by Red Hat Inc.
+ *
+ * This copyrighted material is made available to anyone wishing to use, modify,
+ * copy, or redistribute it subject to the terms and conditions of the GNU
+ * Lesser General Public License, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this distribution; if not, write to:
+ * Free Software Foundation, Inc.
+ * 51 Franklin Street, Fifth Floor
+ * Boston, MA  02110-1301  USA
+ */
+
+// $Id:$
+package org.hibernate.test.annotations.inheritance.discriminatoroptions;
+
+import javax.persistence.DiscriminatorValue;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+
+/**
+ * @author Hardy Ferentschik
+ */
+ at Entity
+ at DiscriminatorValue("B")
+public class SubClass extends BaseClass {
+
+}
+
+


Property changes on: core/trunk/testsuite/src/test/java/org/hibernate/test/annotations/inheritance/discriminatoroptions/SubClass.java
___________________________________________________________________
Name: svn:keywords
   + Id



More information about the hibernate-commits mailing list