From d50a66bc207fd8593012c8331641790f26fa2f74 Mon Sep 17 00:00:00 2001 From: Steve Ebersole Date: Mon, 23 Jan 2012 12:11:46 -0600 Subject: [PATCH] HHH-6970 - Expand notion of "natural id mutability" to ternary value --- .../org/hibernate/NaturalIdMutability.java | 65 ----------------- .../org/hibernate/annotations/NaturalId.java | 23 +------ .../java/org/hibernate/cfg/BinderHelper.java | 1 - .../java/org/hibernate/cfg/HbmBinder.java | 20 +++--- .../cfg/annotations/PropertyBinder.java | 11 +-- .../DefaultFlushEntityEventListener.java | 69 ++++++++++++------- .../java/org/hibernate/mapping/Property.java | 28 ++------ .../tuple/entity/EntityMetamodel.java | 3 +- .../hibernate/test/jpa/naturalid/Group.java | 3 +- 9 files changed, 70 insertions(+), 153 deletions(-) delete mode 100644 hibernate-core/src/main/java/org/hibernate/NaturalIdMutability.java diff --git a/hibernate-core/src/main/java/org/hibernate/NaturalIdMutability.java b/hibernate-core/src/main/java/org/hibernate/NaturalIdMutability.java deleted file mode 100644 index eb721aafdf..0000000000 --- a/hibernate-core/src/main/java/org/hibernate/NaturalIdMutability.java +++ /dev/null @@ -1,65 +0,0 @@ -/* - * 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; - -/** - * Possible values regarding the mutability of a natural id. - * - * @author Steve Ebersole - * - * @see org.hibernate.annotations.NaturalId - */ -public enum NaturalIdMutability { - /** - * The natural id is mutable. Hibernate will write changes in the natural id value when flushing updates to the - * the entity to the database. Also, it will invalidate any caching when such a change is detected. - */ - MUTABLE, - - /** - * The natural id is immutable. Hibernate will ignore any changes in the natural id value when flushing updates - * to the entity to the database. Additionally Hibernate will not check with the database to check if the - * natural id values change there. Essentially the user is assuring Hibernate that the values will not change. - */ - IMMUTABLE, - - /** - * The natural id is immutable. Hibernate will ignore any changes in the natural id value when flushing updates - * to the entity to the database. However, Hibernate will check with the database to check if the natural - * id values change there. This will ensure caching gets invalidated if the natural id value is changed in the - * database (outside of this Hibernate SessionFactory). - * - * Note however that frequently changing natural ids are really not natural ids and should really not be mapped - * as such. The overhead of maintaining caching of natural ids in these cases is far greater than the benefit - * from such caching. In such cases, a database index is a much better solution. - */ - IMMUTABLE_CHECKED, - - /** - * @deprecated Added in deprecated form solely to allow seamless working until the deprecated attribute - * {@link org.hibernate.annotations.NaturalId#mutable()} can be removed. - */ - @Deprecated - UNSPECIFIED -} diff --git a/hibernate-core/src/main/java/org/hibernate/annotations/NaturalId.java b/hibernate-core/src/main/java/org/hibernate/annotations/NaturalId.java index ed39c1460b..f9993565b7 100644 --- a/hibernate-core/src/main/java/org/hibernate/annotations/NaturalId.java +++ b/hibernate-core/src/main/java/org/hibernate/annotations/NaturalId.java @@ -26,8 +26,6 @@ package org.hibernate.annotations; import java.lang.annotation.Retention; import java.lang.annotation.Target; -import org.hibernate.NaturalIdMutability; - import static java.lang.annotation.ElementType.FIELD; import static java.lang.annotation.ElementType.METHOD; import static java.lang.annotation.RetentionPolicy.RUNTIME; @@ -36,31 +34,14 @@ import static java.lang.annotation.RetentionPolicy.RUNTIME; * This specifies that a property is part of the natural id of the entity. * * @author Nicol�s Lichtmaier - * @author Steve Ebersole */ @Target( { METHOD, FIELD } ) @Retention( RUNTIME ) public @interface NaturalId { /** - * @deprecated Use {@link #mutability()} instead. For {@code mutable == false} (the default) use - * {@link NaturalIdMutability#IMMUTABLE_CHECKED}; for {@code mutable == true} use - * {@link NaturalIdMutability#MUTABLE}. + * Is this natural id mutable (or immutable)? * - * Note however the difference between {@link NaturalIdMutability#IMMUTABLE_CHECKED} which mimics the old behavior - * of {@code mutable == false} and the new behavior available via {@link NaturalIdMutability#IMMUTABLE} + * @return {@code true} indicates the natural id is mutable; {@code false} (the default) that it is immutable. */ - @Deprecated - @SuppressWarnings( {"JavaDoc"}) boolean mutable() default false; - - /** - * The mutability behavior of this natural id. - * - * Note: the current default value is the {@link NaturalIdMutability#UNSPECIFIED} value which was added - * in deprecated form until the deprecated {@link #mutable()} attribute here can be removed. This lets existing - * applications continue to work seamlessly using their existing natural id annotations. - * - * @return The mutability behavior. - */ - NaturalIdMutability mutability() default NaturalIdMutability.UNSPECIFIED; } diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/BinderHelper.java b/hibernate-core/src/main/java/org/hibernate/cfg/BinderHelper.java index 86db2eadea..7009459a62 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/BinderHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/BinderHelper.java @@ -104,7 +104,6 @@ public class BinderHelper { clone.setName( property.getName() ); clone.setNodeName( property.getNodeName() ); clone.setNaturalIdentifier( property.isNaturalIdentifier() ); - clone.setNaturalIdMutability( property.getNaturalIdMutability() ); clone.setOptimisticLocked( property.isOptimisticLocked() ); clone.setOptional( property.isOptional() ); clone.setPersistentClass( property.getPersistentClass() ); diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/HbmBinder.java b/hibernate-core/src/main/java/org/hibernate/cfg/HbmBinder.java index a1c0cd7d6a..825fcb42e5 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/HbmBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/HbmBinder.java @@ -40,7 +40,6 @@ import org.hibernate.EntityMode; import org.hibernate.FetchMode; import org.hibernate.FlushMode; import org.hibernate.MappingException; -import org.hibernate.NaturalIdMutability; import org.hibernate.engine.internal.Versioning; import org.hibernate.engine.spi.ExecuteUpdateResultCheckStyle; import org.hibernate.engine.spi.FilterDefinition; @@ -2222,19 +2221,24 @@ public final class HbmBinder { } if ( value != null ) { - Property property = createProperty( value, propertyName, persistentClass - .getClassName(), subnode, mappings, inheritedMetas ); + final Property property = createProperty( + value, + propertyName, + persistentClass.getClassName(), + subnode, + mappings, + inheritedMetas + ); if ( !mutable ) { property.setUpdateable(false); } if ( naturalId ) { - property.setNaturalIdentifier(true); - property.setNaturalIdMutability( - mutable ? NaturalIdMutability.MUTABLE : NaturalIdMutability.IMMUTABLE_CHECKED - ); + property.setNaturalIdentifier( true ); } persistentClass.addProperty( property ); - if ( uniqueKey!=null ) uniqueKey.addColumns( property.getColumnIterator() ); + if ( uniqueKey!=null ) { + uniqueKey.addColumns( property.getColumnIterator() ); + } } } diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/annotations/PropertyBinder.java b/hibernate-core/src/main/java/org/hibernate/cfg/annotations/PropertyBinder.java index cfdd6874b9..7969766e06 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/annotations/PropertyBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/annotations/PropertyBinder.java @@ -30,7 +30,6 @@ import javax.persistence.Id; import org.jboss.logging.Logger; import org.hibernate.AnnotationException; -import org.hibernate.NaturalIdMutability; import org.hibernate.annotations.Generated; import org.hibernate.annotations.GenerationTime; import org.hibernate.annotations.Immutable; @@ -286,20 +285,14 @@ public class PropertyBinder { } NaturalId naturalId = property != null ? property.getAnnotation( NaturalId.class ) : null; if ( naturalId != null ) { - NaturalIdMutability mutability = naturalId.mutability(); - if ( mutability == NaturalIdMutability.UNSPECIFIED ) { - mutability = naturalId.mutable() - ? NaturalIdMutability.MUTABLE - : NaturalIdMutability.IMMUTABLE_CHECKED; - } - if ( mutability != NaturalIdMutability.MUTABLE ) { + if ( ! naturalId.mutable() ) { updatable = false; } prop.setNaturalIdentifier( true ); - prop.setNaturalIdMutability( mutability ); } prop.setInsertable( insertable ); prop.setUpdateable( updatable ); + OptimisticLock lockAnn = property != null ? property.getAnnotation( OptimisticLock.class ) : null; diff --git a/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultFlushEntityEventListener.java b/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultFlushEntityEventListener.java index d033d6c184..d64d456266 100755 --- a/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultFlushEntityEventListener.java +++ b/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultFlushEntityEventListener.java @@ -96,34 +96,57 @@ public class DefaultFlushEntityEventListener implements FlushEntityEventListener Object[] loaded, SessionImplementor session) { if ( persister.hasNaturalIdentifier() && entry.getStatus() != Status.READ_ONLY ) { - Object[] snapshot = null; - Type[] types = persister.getPropertyTypes(); - int[] props = persister.getNaturalIdentifierProperties(); - boolean[] updateable = persister.getPropertyUpdateability(); - for ( int i=0; i