HHH-6372 : Provide a temporary way to initialize HibernateTypeDescriptor.explicitType for "basic" types

This commit is contained in:
Gail Badner 2011-06-27 19:59:51 -07:00
parent f4cc72b2d3
commit 919cdf7c69
4 changed files with 99 additions and 2 deletions

View File

@ -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" );
}

View File

@ -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();
}
}

View File

@ -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) {

View File

@ -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() );
}