From 029badbba6863bbc8d36cab7086589462bd97313 Mon Sep 17 00:00:00 2001 From: Strong Liu Date: Fri, 11 Jan 2013 02:06:04 +0800 Subject: [PATCH] HHH-7916 @NotFound.IGNORE --- .../org/hibernate/cfg/ToOneFkSecondPass.java | 2 +- .../hibernate/metamodel/internal/Binder.java | 7 +++++-- .../internal/HibernateTypeHelper.java | 2 +- .../annotations/ToOneAttributeSourceImpl.java | 5 +++++ .../hbm/AbstractToOneAttributeSourceImpl.java | 5 +++++ .../hbm/ManyToOneAttributeSourceImpl.java | 5 +++++ .../AbstractAttributeBindingContainer.java | 2 ++ ...actCompositeAttributeBindingContainer.java | 2 ++ ...ctSingularAssociationAttributeBinding.java | 20 ++++++++++++++++++- .../binding/AttributeBindingContainer.java | 1 + .../binding/CompositeAttributeBinding.java | 2 ++ .../binding/ManyToOneAttributeBinding.java | 3 +++ .../spi/binding/OneToOneAttributeBinding.java | 2 +- .../SingularAssociationAttributeBinding.java | 2 ++ .../spi/source/ToOneAttributeSource.java | 1 + 15 files changed, 55 insertions(+), 6 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/ToOneFkSecondPass.java b/hibernate-core/src/main/java/org/hibernate/cfg/ToOneFkSecondPass.java index d35d22c0e2..f5c6d36820 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/ToOneFkSecondPass.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/ToOneFkSecondPass.java @@ -121,7 +121,7 @@ public class ToOneFkSecondPass extends FkSecondPass { if ( !manyToOne.isIgnoreNotFound() ) manyToOne.createPropertyRefConstraints( persistentClasses ); } else if ( value instanceof OneToOne ) { - ( (OneToOne) value ).createForeignKey(); + value.createForeignKey(); } else { throw new AssertionFailure( "FkSecondPass for a wrong value type: " + value.getClass().getName() ); diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/Binder.java b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/Binder.java index af909ae6c3..b92824b9f2 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/Binder.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/Binder.java @@ -1099,6 +1099,7 @@ public class Binder { propertyAccessorName( attributeSource ), attributeSource.isIncludedInOptimisticLocking(), attributeSource.isLazy(), + attributeSource.isNotFoundAnException(), attributeSource.getNaturalIdMutability(), createMetaAttributeContext( attributeBindingContainer, attributeSource ), referencedEntityBinding, @@ -1117,12 +1118,14 @@ public class Binder { idAttributeBinding == referencedAttributeBinding ? null : getRelativePathFromEntityName( referencedAttributeBinding ); +// final boolean isNotFoundAnException = SingularAssociationAttributeBinding.class.isInstance( ) + return metadata.getTypeResolver().getTypeFactory().manyToOne( referencedEntityBinding.getEntity().getName(), uniqueKeyAttributeName, attributeSource.getFetchTiming() != FetchTiming.IMMEDIATE, attributeSource.isUnWrapProxy(), - false, //TODO: should be attributeBinding.isIgnoreNotFound(), + !attributeSource.isNotFoundAnException(), false //TODO: determine if isLogicalOneToOne ); } @@ -1731,7 +1734,7 @@ public class Binder { null, false, false, - false, //TODO: should be attributeBinding.isIgnoreNotFound(), + !elementSource.isNotFoundAnException(), //TODO: should be attributeBinding.isIgnoreNotFound(), false ); final HibernateTypeDescriptor hibernateTypeDescriptor = elementBinding.getHibernateTypeDescriptor(); diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/HibernateTypeHelper.java b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/HibernateTypeHelper.java index d4aa48512b..bf409d257c 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/HibernateTypeHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/HibernateTypeHelper.java @@ -374,7 +374,7 @@ class HibernateTypeHelper { elementSource.getReferencedEntityAttributeName(), false, false, - false, //TODO: should be attributeBinding.isIgnoreNotFound(), + !elementSource.isNotFoundAnException(), false ); final HibernateTypeDescriptor hibernateTypeDescriptor = elementBinding.getHibernateTypeDescriptor(); diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/annotations/ToOneAttributeSourceImpl.java b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/annotations/ToOneAttributeSourceImpl.java index 0fdef5fa82..6b5231702f 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/annotations/ToOneAttributeSourceImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/annotations/ToOneAttributeSourceImpl.java @@ -86,6 +86,11 @@ public class ToOneAttributeSourceImpl extends SingularAttributeSourceImpl implem return associationAttribute.getReferencedEntityType(); } + @Override + public boolean isNotFoundAnException() { + return !associationAttribute.isIgnoreNotFound(); + } + @Override public List getDefaultNamingStrategies( final String entityName, final String tableName, final AttributeBinding referencedAttributeBinding) { diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/hbm/AbstractToOneAttributeSourceImpl.java b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/hbm/AbstractToOneAttributeSourceImpl.java index d6654be929..072db28b43 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/hbm/AbstractToOneAttributeSourceImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/hbm/AbstractToOneAttributeSourceImpl.java @@ -80,6 +80,11 @@ public abstract class AbstractToOneAttributeSourceImpl extends AbstractHbmSource return PropertyGeneration.NEVER; } + @Override + public boolean isNotFoundAnException() { + return true; + } + @Override public boolean isLazy() { return getFetchTiming() != FetchTiming.IMMEDIATE; diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/hbm/ManyToOneAttributeSourceImpl.java b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/hbm/ManyToOneAttributeSourceImpl.java index 945c387dad..e2d84ea5bd 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/hbm/ManyToOneAttributeSourceImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/hbm/ManyToOneAttributeSourceImpl.java @@ -102,6 +102,11 @@ class ManyToOneAttributeSourceImpl extends AbstractToOneAttributeSourceImpl { return manyToOneElement.getAccess(); } + @Override + public boolean isNotFoundAnException() { + return manyToOneElement.getNotFound() == null || !"ignore".equals( manyToOneElement.getNotFound().value() ); + } + @Override public boolean isIncludedInOptimisticLocking() { return manyToOneElement.isOptimisticLock(); diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/spi/binding/AbstractAttributeBindingContainer.java b/hibernate-core/src/main/java/org/hibernate/metamodel/spi/binding/AbstractAttributeBindingContainer.java index c66b39dc65..c952dc3a71 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/spi/binding/AbstractAttributeBindingContainer.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/spi/binding/AbstractAttributeBindingContainer.java @@ -179,6 +179,7 @@ public abstract class AbstractAttributeBindingContainer implements AttributeBind String propertyAccessorName, boolean includedInOptimisticLocking, boolean lazy, + boolean isNotFoundAnException, SingularAttributeBinding.NaturalIdMutability naturalIdMutability, MetaAttributeContext metaAttributeContext, EntityBinding referencedEntityBinding, @@ -190,6 +191,7 @@ public abstract class AbstractAttributeBindingContainer implements AttributeBind propertyAccessorName, includedInOptimisticLocking, lazy, + isNotFoundAnException, naturalIdMutability, metaAttributeContext, referencedEntityBinding, diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/spi/binding/AbstractCompositeAttributeBindingContainer.java b/hibernate-core/src/main/java/org/hibernate/metamodel/spi/binding/AbstractCompositeAttributeBindingContainer.java index 7befd81c66..dbd25428ce 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/spi/binding/AbstractCompositeAttributeBindingContainer.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/spi/binding/AbstractCompositeAttributeBindingContainer.java @@ -176,6 +176,7 @@ public abstract class AbstractCompositeAttributeBindingContainer String propertyAccessorName, boolean includedInOptimisticLocking, boolean lazy, + boolean isNotFoundAnException, SingularAttributeBinding.NaturalIdMutability naturalIdMutability, MetaAttributeContext metaAttributeContext, EntityBinding referencedEntityBinding, @@ -189,6 +190,7 @@ public abstract class AbstractCompositeAttributeBindingContainer propertyAccessorName, includedInOptimisticLocking, lazy, + isNotFoundAnException, naturalIdMutability, metaAttributeContext, referencedEntityBinding, diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/spi/binding/AbstractSingularAssociationAttributeBinding.java b/hibernate-core/src/main/java/org/hibernate/metamodel/spi/binding/AbstractSingularAssociationAttributeBinding.java index 20a90a37a2..4f905e4573 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/spi/binding/AbstractSingularAssociationAttributeBinding.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/spi/binding/AbstractSingularAssociationAttributeBinding.java @@ -46,8 +46,20 @@ public class AbstractSingularAssociationAttributeBinding extends AbstractSingula private FetchTiming fetchTiming; private FetchStyle fetchStyle; private boolean isUnWrapProxy; + private final boolean isNotFoundAnException; - public AbstractSingularAssociationAttributeBinding(AttributeBindingContainer container, SingularAttribute attribute, String propertyAccessorName, boolean includedInOptimisticLocking, boolean isLazy, NaturalIdMutability naturalIdMutability, MetaAttributeContext metaAttributeContext, EntityBinding referencedEntityBinding, List relationalValueBindings, SingularAttributeBinding referencedAttributeBinding) { + public AbstractSingularAssociationAttributeBinding( + AttributeBindingContainer container, + SingularAttribute attribute, + String propertyAccessorName, + boolean includedInOptimisticLocking, + boolean isLazy, + boolean isNotFoundAnException, + NaturalIdMutability naturalIdMutability, + MetaAttributeContext metaAttributeContext, + EntityBinding referencedEntityBinding, + List relationalValueBindings, + SingularAttributeBinding referencedAttributeBinding) { super( container, attribute, @@ -66,6 +78,7 @@ public class AbstractSingularAssociationAttributeBinding extends AbstractSingula this.referencedEntityBinding = referencedEntityBinding; this.relationalValueBindings = Collections.unmodifiableList( relationalValueBindings ); this.referencedAttributeBinding = referencedAttributeBinding; + this.isNotFoundAnException = isNotFoundAnException; } @Override @@ -73,6 +86,11 @@ public class AbstractSingularAssociationAttributeBinding extends AbstractSingula return relationalValueBindings; } + @Override + public boolean isNotFoundAnException() { + return isNotFoundAnException; + } + @Override public boolean hasDerivedValue() { // TODO: support derived value diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/spi/binding/AttributeBindingContainer.java b/hibernate-core/src/main/java/org/hibernate/metamodel/spi/binding/AttributeBindingContainer.java index 4f76c64fd2..e866bf7e93 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/spi/binding/AttributeBindingContainer.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/spi/binding/AttributeBindingContainer.java @@ -209,6 +209,7 @@ public interface AttributeBindingContainer { String propertyAccessorName, boolean includedInOptimisticLocking, boolean lazy, + boolean isNotFoundAnException, SingularAttributeBinding.NaturalIdMutability naturalIdMutability, MetaAttributeContext metaAttributeContext, EntityBinding referencedEntityBinding, diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/spi/binding/CompositeAttributeBinding.java b/hibernate-core/src/main/java/org/hibernate/metamodel/spi/binding/CompositeAttributeBinding.java index b75c31fa63..b1a2303ca9 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/spi/binding/CompositeAttributeBinding.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/spi/binding/CompositeAttributeBinding.java @@ -426,6 +426,7 @@ public class CompositeAttributeBinding String propertyAccessorName, boolean includedInOptimisticLocking, boolean lazy, + boolean isNotFoundAnException, NaturalIdMutability naturalIdMutability, MetaAttributeContext metaAttributeContext, EntityBinding referencedEntityBinding, @@ -436,6 +437,7 @@ public class CompositeAttributeBinding propertyAccessorName, includedInOptimisticLocking, lazy, + isNotFoundAnException, naturalIdMutability, metaAttributeContext, referencedEntityBinding, diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/spi/binding/ManyToOneAttributeBinding.java b/hibernate-core/src/main/java/org/hibernate/metamodel/spi/binding/ManyToOneAttributeBinding.java index 939380c87f..f5373bf156 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/spi/binding/ManyToOneAttributeBinding.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/spi/binding/ManyToOneAttributeBinding.java @@ -43,6 +43,7 @@ public class ManyToOneAttributeBinding String propertyAccessorName, boolean includedInOptimisticLocking, boolean lazy, + boolean isNotFoundAnException, NaturalIdMutability naturalIdMutability, MetaAttributeContext metaAttributeContext, EntityBinding referencedEntityBinding, @@ -54,6 +55,7 @@ public class ManyToOneAttributeBinding propertyAccessorName, includedInOptimisticLocking, lazy, + isNotFoundAnException, naturalIdMutability, metaAttributeContext, referencedEntityBinding, relationalValueBindings, referencedAttributeBinding @@ -65,4 +67,5 @@ public class ManyToOneAttributeBinding return hasNullableRelationalValueBinding( relationalValueBindings ); } + } \ No newline at end of file diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/spi/binding/OneToOneAttributeBinding.java b/hibernate-core/src/main/java/org/hibernate/metamodel/spi/binding/OneToOneAttributeBinding.java index 68a5eae42d..e3c5b5d10d 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/spi/binding/OneToOneAttributeBinding.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/spi/binding/OneToOneAttributeBinding.java @@ -57,6 +57,7 @@ public class OneToOneAttributeBinding propertyAccessorName, includedInOptimisticLocking, lazy, + true, //always true naturalIdMutability, metaAttributeContext, referencedEntityBinding, @@ -70,5 +71,4 @@ public class OneToOneAttributeBinding public boolean isNullable() { return !isConstrained; } - } \ No newline at end of file diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/spi/binding/SingularAssociationAttributeBinding.java b/hibernate-core/src/main/java/org/hibernate/metamodel/spi/binding/SingularAssociationAttributeBinding.java index 37b0b36898..6362713f90 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/spi/binding/SingularAssociationAttributeBinding.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/spi/binding/SingularAssociationAttributeBinding.java @@ -41,4 +41,6 @@ public interface SingularAssociationAttributeBinding extends SingularAttributeBi public EntityBinding getReferencedEntityBinding(); public SingularAttributeBinding getReferencedAttributeBinding(); + + public boolean isNotFoundAnException(); } \ No newline at end of file diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/spi/source/ToOneAttributeSource.java b/hibernate-core/src/main/java/org/hibernate/metamodel/spi/source/ToOneAttributeSource.java index 2ed216f14a..526152d4e9 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/spi/source/ToOneAttributeSource.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/spi/source/ToOneAttributeSource.java @@ -44,6 +44,7 @@ public interface ToOneAttributeSource * @return The name of the referenced entity */ public String getReferencedEntityName(); + public boolean isNotFoundAnException(); public boolean isUnWrapProxy(); ForeignKeyDirection getForeignKeyDirection(); public List getDefaultNamingStrategies(final String entityName, final String tableName, final AttributeBinding referencedAttributeBinding);