diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/domain/AbstractAttributeContainer.java b/hibernate-core/src/main/java/org/hibernate/metamodel/domain/AbstractAttributeContainer.java index f7d6f1a79c..c15929e329 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/domain/AbstractAttributeContainer.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/domain/AbstractAttributeContainer.java @@ -138,11 +138,11 @@ public abstract class AbstractAttributeContainer implements AttributeContainer, this.attributeContainer = attributeContainer; } - boolean isTypeResolved() { + public boolean isTypeResolved() { return type != null; } - void resolveType(Type type) { + public void resolveType(Type type) { if ( type == null ) { throw new IllegalArgumentException( "Attempt to resolve with null type" ); } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/internal/AttributeTypeResolver.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/internal/AttributeTypeResolver.java new file mode 100644 index 0000000000..abbd3d04b2 --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/internal/AttributeTypeResolver.java @@ -0,0 +1,92 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2011, 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.metamodel.source.internal; + +import java.util.Properties; + + +import org.hibernate.MappingException; +import org.hibernate.metamodel.binding.AttributeBinding; +import org.hibernate.metamodel.binding.EntityBinding; +import org.hibernate.metamodel.binding.HibernateTypeDescriptor; +import org.hibernate.metamodel.source.spi.MetadataImplementor; + +/** + * This is a TEMPORARY way to initialize HibernateTypeDescriptor.explicitType. + * This class will be removed when types are resolved properly. + * + * @author Gail Badner + */ +class AttributeTypeResolver { + + private final MetadataImplementor metadata; + + AttributeTypeResolver(MetadataImplementor metadata) { + this.metadata = metadata; + } + + void resolve() { + for ( EntityBinding entityBinding : metadata.getEntityBindings() ) { + for ( AttributeBinding attributeBinding : entityBinding.getAttributeBindings() ) { + resolve( attributeBinding ); + } + } + } + + private void resolve(AttributeBinding attributeBinding) { + if ( attributeBinding.getHibernateTypeDescriptor().getExplicitType() != null ) { + return; // already resolved + } + + // this only works for "basic" attribute types + HibernateTypeDescriptor typeDescriptor = attributeBinding.getHibernateTypeDescriptor(); + if ( typeDescriptor == null || typeDescriptor.getTypeName() == null) { + throw new MappingException( "Hibernate type name has not been defined for attribute: " + + getQualifiedAttributeName( attributeBinding ) + ); + } + if ( typeDescriptor.getTypeName() != null ) { + Properties typeParameters = null; + if ( typeDescriptor.getTypeParameters() != null ) { + typeParameters = new Properties(); + typeParameters.putAll( typeDescriptor.getTypeParameters() ); + } + typeDescriptor.setExplicitType( + metadata.getTypeResolver().heuristicType( + typeDescriptor.getTypeName(), + typeParameters + ) + ); + } + } + + // TODO: this does not work for components + private static String getQualifiedAttributeName(AttributeBinding attributebinding) { + return new StringBuilder() + .append( attributebinding.getEntityBinding().getEntity().getJavaType().getName() ) + .append( "." ) + .append( attributebinding.getAttribute().getName() ) + .toString(); + } +} diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/internal/MetadataImpl.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/internal/MetadataImpl.java index aef42cdee5..acb9001c29 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/internal/MetadataImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/internal/MetadataImpl.java @@ -141,6 +141,7 @@ public class MetadataImpl implements MetadataImplementor, Serializable { // todo : remove this by coordinated ordering of entity processing new EntityReferenceResolver( this ).resolve(); + new AttributeTypeResolver( this ).resolve(); } private void prepare(Binder[] binders, MetadataSources metadataSources) { diff --git a/hibernate-core/src/test/java/org/hibernate/metamodel/binding/AbstractBasicBindingTests.java b/hibernate-core/src/test/java/org/hibernate/metamodel/binding/AbstractBasicBindingTests.java index 6e54be196b..966124d3ff 100644 --- a/hibernate-core/src/test/java/org/hibernate/metamodel/binding/AbstractBasicBindingTests.java +++ b/hibernate-core/src/test/java/org/hibernate/metamodel/binding/AbstractBasicBindingTests.java @@ -38,6 +38,8 @@ import org.hibernate.service.BasicServiceRegistry; import org.hibernate.service.ServiceRegistryBuilder; import org.hibernate.service.internal.BasicServiceRegistryImpl; import org.hibernate.testing.junit4.BaseUnitTestCase; +import org.hibernate.type.LongType; +import org.hibernate.type.StringType; import static junit.framework.Assert.assertEquals; import static junit.framework.Assert.assertFalse; @@ -129,12 +131,14 @@ public abstract class AbstractBasicBindingTests extends BaseUnitTestCase { AttributeBinding idAttributeBinding = entityBinding.getAttributeBinding( "id" ); assertNotNull( idAttributeBinding ); assertSame( idAttributeBinding, entityBinding.getEntityIdentifier().getValueBinding() ); + assertSame( LongType.INSTANCE, idAttributeBinding.getHibernateTypeDescriptor().getExplicitType() ); assertNotNull( idAttributeBinding.getAttribute() ); assertNotNull( idAttributeBinding.getValue() ); assertTrue( idAttributeBinding.getValue() instanceof Column ); AttributeBinding nameBinding = entityBinding.getAttributeBinding( "name" ); assertNotNull( nameBinding ); + assertSame( StringType.INSTANCE, nameBinding.getHibernateTypeDescriptor().getExplicitType() ); assertNotNull( nameBinding.getAttribute() ); assertNotNull( nameBinding.getValue() ); }