From db347c9de7deec7a173d476f0d7a5d263b55840c Mon Sep 17 00:00:00 2001 From: Steve Ebersole Date: Tue, 22 May 2012 16:13:21 -0500 Subject: [PATCH] HHH-7074 - the replacement annotations of @Entity are not working --- .../cfg/annotations/EntityBinder.java | 55 +++++++++--- .../test/annotations/entity/Forest2.java | 84 +++++++++++++++++++ ...NewCustomEntityMappingAnnotationsTest.java | 58 +++++++++++++ 3 files changed, 184 insertions(+), 13 deletions(-) create mode 100644 hibernate-core/src/matrix/java/org/hibernate/test/annotations/entity/Forest2.java create mode 100644 hibernate-core/src/matrix/java/org/hibernate/test/annotations/entity/NewCustomEntityMappingAnnotationsTest.java diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/annotations/EntityBinder.java b/hibernate-core/src/main/java/org/hibernate/cfg/annotations/EntityBinder.java index 659546d298..012e6b912c 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/annotations/EntityBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/annotations/EntityBinder.java @@ -44,12 +44,16 @@ import org.hibernate.MappingException; import org.hibernate.annotations.BatchSize; import org.hibernate.annotations.Cache; import org.hibernate.annotations.CacheConcurrencyStrategy; +import org.hibernate.annotations.DynamicInsert; +import org.hibernate.annotations.DynamicUpdate; import org.hibernate.annotations.FetchMode; import org.hibernate.annotations.Immutable; import org.hibernate.annotations.Loader; import org.hibernate.annotations.NaturalIdCache; import org.hibernate.annotations.OptimisticLockType; +import org.hibernate.annotations.OptimisticLocking; import org.hibernate.annotations.Persister; +import org.hibernate.annotations.Polymorphism; import org.hibernate.annotations.PolymorphismType; import org.hibernate.annotations.Proxy; import org.hibernate.annotations.RowId; @@ -57,6 +61,7 @@ import org.hibernate.annotations.SQLDelete; import org.hibernate.annotations.SQLDeleteAll; import org.hibernate.annotations.SQLInsert; import org.hibernate.annotations.SQLUpdate; +import org.hibernate.annotations.SelectBeforeUpdate; import org.hibernate.annotations.Subselect; import org.hibernate.annotations.Synchronize; import org.hibernate.annotations.Tables; @@ -153,24 +158,48 @@ public class EntityBinder { bindHibernateAnnotation( hibAnn ); } + @SuppressWarnings("SimplifiableConditionalExpression") private void bindHibernateAnnotation(org.hibernate.annotations.Entity hibAnn) { + { + final DynamicInsert dynamicInsertAnn = annotatedClass.getAnnotation( DynamicInsert.class ); + this.dynamicInsert = dynamicInsertAnn == null + ? ( hibAnn == null ? false : hibAnn.dynamicInsert() ) + : dynamicInsertAnn.value(); + } + + { + final DynamicUpdate dynamicUpdateAnn = annotatedClass.getAnnotation( DynamicUpdate.class ); + this.dynamicUpdate = dynamicUpdateAnn == null + ? ( hibAnn == null ? false : hibAnn.dynamicUpdate() ) + : dynamicUpdateAnn.value(); + } + + { + final SelectBeforeUpdate selectBeforeUpdateAnn = annotatedClass.getAnnotation( SelectBeforeUpdate.class ); + this.selectBeforeUpdate = selectBeforeUpdateAnn == null + ? ( hibAnn == null ? false : hibAnn.selectBeforeUpdate() ) + : selectBeforeUpdateAnn.value(); + } + + { + final OptimisticLocking optimisticLockingAnn = annotatedClass.getAnnotation( OptimisticLocking.class ); + this.optimisticLockType = optimisticLockingAnn == null + ? ( hibAnn == null ? OptimisticLockType.VERSION : hibAnn.optimisticLock() ) + : optimisticLockingAnn.type(); + } + + { + final Polymorphism polymorphismAnn = annotatedClass.getAnnotation( Polymorphism.class ); + this.polymorphismType = polymorphismAnn == null + ? ( hibAnn == null ? PolymorphismType.IMPLICIT : hibAnn.polymorphism() ) + : polymorphismAnn.type(); + } + if ( hibAnn != null ) { - dynamicInsert = hibAnn.dynamicInsert(); - dynamicUpdate = hibAnn.dynamicUpdate(); - optimisticLockType = hibAnn.optimisticLock(); - selectBeforeUpdate = hibAnn.selectBeforeUpdate(); - polymorphismType = hibAnn.polymorphism(); + // used later in bind for logging explicitHibernateEntityAnnotation = true; //persister handled in bind } - else { - //default values when the annotation is not there - dynamicInsert = false; - dynamicUpdate = false; - optimisticLockType = OptimisticLockType.VERSION; - polymorphismType = PolymorphismType.IMPLICIT; - selectBeforeUpdate = false; - } } private void bindEjb3Annotation(Entity ejb3Ann) { diff --git a/hibernate-core/src/matrix/java/org/hibernate/test/annotations/entity/Forest2.java b/hibernate-core/src/matrix/java/org/hibernate/test/annotations/entity/Forest2.java new file mode 100644 index 0000000000..52e9fe5921 --- /dev/null +++ b/hibernate-core/src/matrix/java/org/hibernate/test/annotations/entity/Forest2.java @@ -0,0 +1,84 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2012, 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 + */ +package org.hibernate.test.annotations.entity; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; + +import org.hibernate.annotations.DynamicInsert; +import org.hibernate.annotations.DynamicUpdate; +import org.hibernate.annotations.OptimisticLock; +import org.hibernate.annotations.OptimisticLockType; +import org.hibernate.annotations.OptimisticLocking; +import org.hibernate.annotations.Polymorphism; +import org.hibernate.annotations.PolymorphismType; +import org.hibernate.annotations.SelectBeforeUpdate; +import org.hibernate.annotations.Type; + +/** + * Mapping following lines of {@link Forest}, but using the replacements for the now deprecated + * {@link org.hibernate.annotations.Entity} annotation. + * + * @author Steve Ebersole + */ +@Entity +@DynamicInsert +@DynamicUpdate +@SelectBeforeUpdate +@OptimisticLocking( type = OptimisticLockType.ALL ) +@Polymorphism( type = PolymorphismType.EXPLICIT ) +public class Forest2 { + private Integer id; + private String name; + private String longDescription; + + @Id + @GeneratedValue + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @OptimisticLock(excluded=true) + @Type(type = "text") + public String getLongDescription() { + return longDescription; + } + + public void setLongDescription(String longDescription) { + this.longDescription = longDescription; + } +} diff --git a/hibernate-core/src/matrix/java/org/hibernate/test/annotations/entity/NewCustomEntityMappingAnnotationsTest.java b/hibernate-core/src/matrix/java/org/hibernate/test/annotations/entity/NewCustomEntityMappingAnnotationsTest.java new file mode 100644 index 0000000000..64d6c831e4 --- /dev/null +++ b/hibernate-core/src/matrix/java/org/hibernate/test/annotations/entity/NewCustomEntityMappingAnnotationsTest.java @@ -0,0 +1,58 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2012, 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 + */ +package org.hibernate.test.annotations.entity; + +import org.hibernate.mapping.RootClass; + +import org.junit.Test; + +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; + +import static org.junit.Assert.assertEquals; + +/** + * @author Steve Ebersole + */ +public class NewCustomEntityMappingAnnotationsTest extends BaseCoreFunctionalTestCase { + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { Forest.class, Forest2.class }; + } + + @Override + protected String[] getAnnotatedPackages() { + return new String[] { Forest.class.getPackage().getName() }; + } + + @Test + public void testSameMappingValues() { + RootClass forest = (RootClass) configuration().getClassMapping( Forest.class.getName() ); + RootClass forest2 = (RootClass) configuration().getClassMapping( Forest2.class.getName() ); + assertEquals( forest.useDynamicInsert(), forest2.useDynamicInsert() ); + assertEquals( forest.useDynamicUpdate(), forest2.useDynamicUpdate() ); + assertEquals( forest.hasSelectBeforeUpdate(), forest2.hasSelectBeforeUpdate() ); + assertEquals( forest.getOptimisticLockMode(), forest2.getOptimisticLockMode() ); + assertEquals( forest.isExplicitPolymorphism(), forest2.isExplicitPolymorphism() ); + } +}