From fd25e7704bc0cf73595a42c8a842e897fea34190 Mon Sep 17 00:00:00 2001 From: Steve Ebersole Date: Thu, 26 May 2011 15:26:40 -0500 Subject: [PATCH] HHH-6264 - Bind typedef information --- .../hibernate/metamodel/binding/TypeDef.java | 26 ++++++++++------ .../annotations/global/TypeDefBinder.java | 2 +- .../source/hbm/AbstractEntityBinder.java | 2 +- .../metamodel/source/hbm/HbmBinder.java | 31 +++++++++++++------ .../source/internal/MetadataImpl.java | 14 +++++++-- .../source/spi/MetadataImplementor.java | 5 +++ 6 files changed, 56 insertions(+), 24 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/binding/TypeDef.java b/hibernate-core/src/main/java/org/hibernate/metamodel/binding/TypeDef.java index 6378a046b2..a272eec5ad 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/binding/TypeDef.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/binding/TypeDef.java @@ -28,24 +28,30 @@ import java.util.Collections; import java.util.Map; /** - * Placeholder for typedef information + * Represents the metamodel view of a typedef (type definition). + * + * @author John Verhaeg */ public class TypeDef implements Serializable { - + private final String name; private final String typeClass; private final Map parameters; - public TypeDef( String typeClass, - Map parameters ) { - this.typeClass = typeClass; - this.parameters = parameters; + public TypeDef(String name, String typeClass, Map parameters) { + this.name = name; + this.typeClass = typeClass; + this.parameters = parameters; + } + + public String getName() { + return name; + } + + public String getTypeClass() { + return typeClass; } public Map getParameters() { return Collections.unmodifiableMap(parameters); } - - public String getTypeClass() { - return typeClass; - } } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/global/TypeDefBinder.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/global/TypeDefBinder.java index e6e28348e3..4cdcd891ad 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/global/TypeDefBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/global/TypeDefBinder.java @@ -94,7 +94,7 @@ public class TypeDefBinder { Map prms, MetadataImpl metadata) { LOG.debugf( "Binding type definition: %s", name ); - metadata.addTypeDef( name, new TypeDef( typeClass, prms ) ); + metadata.addTypeDef( new TypeDef( name, typeClass, prms ) ); } private TypeDefBinder() { diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/AbstractEntityBinder.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/AbstractEntityBinder.java index 2a31235154..005c124278 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/AbstractEntityBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/AbstractEntityBinder.java @@ -133,7 +133,7 @@ abstract class AbstractEntityBinder { final String entityName = entityBinding.getEntity().getName(); if ( entityClazz.getFetchProfile() != null ) { - hibernateMappingBinder.parseFetchProfiles( entityClazz.getFetchProfile(), entityName ); + hibernateMappingBinder.bindFetchProfiles( entityClazz.getFetchProfile(), entityName ); } getMetadata().addImport( entityName, entityName ); diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/HbmBinder.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/HbmBinder.java index a8c16a3ba8..9c92f439b9 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/HbmBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/HbmBinder.java @@ -23,6 +23,7 @@ */ package org.hibernate.metamodel.source.hbm; +import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; @@ -33,6 +34,7 @@ import org.hibernate.cfg.NamingStrategy; import org.hibernate.internal.util.StringHelper; import org.hibernate.metamodel.binding.FetchProfile; import org.hibernate.metamodel.binding.FetchProfile.Fetch; +import org.hibernate.metamodel.binding.TypeDef; import org.hibernate.metamodel.domain.MetaAttribute; import org.hibernate.metamodel.source.Origin; import org.hibernate.metamodel.source.hbm.util.MappingHelper; @@ -42,6 +44,7 @@ import org.hibernate.metamodel.source.hbm.xml.mapping.XMLHibernateMapping; import org.hibernate.metamodel.source.hbm.xml.mapping.XMLHibernateMapping.XMLClass; import org.hibernate.metamodel.source.hbm.xml.mapping.XMLHibernateMapping.XMLImport; import org.hibernate.metamodel.source.hbm.xml.mapping.XMLJoinedSubclassElement; +import org.hibernate.metamodel.source.hbm.xml.mapping.XMLParamElement; import org.hibernate.metamodel.source.hbm.xml.mapping.XMLQueryElement; import org.hibernate.metamodel.source.hbm.xml.mapping.XMLSqlQueryElement; import org.hibernate.metamodel.source.hbm.xml.mapping.XMLSubclassElement; @@ -149,17 +152,20 @@ public class HbmBinder implements MappingDefaults { } public void processHibernateMapping() { + if ( hibernateMapping.getImport() != null ) { + bindImports( hibernateMapping.getImport() ); + } + if ( hibernateMapping.getTypedef() != null ) { + bindTypeDef( hibernateMapping.getTypedef() ); + } if ( hibernateMapping.getFilterDef() != null ) { // parseFilterDefs( hibernateMapping.getFilterDef() ); } if ( hibernateMapping.getFetchProfile() != null ) { - parseFetchProfiles( hibernateMapping.getFetchProfile(), null ); + bindFetchProfiles( hibernateMapping.getFetchProfile(), null ); } if ( hibernateMapping.getIdentifierGenerator() != null ) { // parseIdentifierGeneratorRegistrations( hibernateMapping.getIdentifierGenerator() ); - } - if ( hibernateMapping.getTypedef() != null ) { -// bindTypeDef( hibernateMapping.getTypedefs() ); } if ( hibernateMapping.getClazzOrSubclassOrJoinedSubclass() != null ) { for ( Object clazzOrSubclass : hibernateMapping.getClazzOrSubclassOrJoinedSubclass() ) { @@ -207,15 +213,12 @@ public class HbmBinder implements MappingDefaults { if ( hibernateMapping.getResultset() != null ) { // bindResultSetMappingDefinitions( element, null, mappings ); } - if ( hibernateMapping.getImport() != null ) { - processImports( hibernateMapping.getImport() ); - } if ( hibernateMapping.getDatabaseObject() != null ) { // bindAuxiliaryDatabaseObjects( element, mappings ); } } - private void processImports(List imports) { + private void bindImports(List imports) { for ( XMLImport importValue : imports ) { String className = getClassName( importValue.getClazz() ); String rename = importValue.getRename(); @@ -224,7 +227,17 @@ public class HbmBinder implements MappingDefaults { } } - protected void parseFetchProfiles(List fetchProfiles, String containingEntityName) { + private void bindTypeDef(List typedefs) { + for ( XMLHibernateMapping.XMLTypedef typedef : typedefs ) { + final Map parameters = new HashMap(); + for ( XMLParamElement paramElement : typedef.getParam() ) { + parameters.put( paramElement.getName(), paramElement.getValue() ); + } + metadata.addTypeDef( new TypeDef( typedef.getName(), typedef.getClazz(), parameters ) ); + } + } + + protected void bindFetchProfiles(List fetchProfiles, String containingEntityName) { for ( XMLFetchProfileElement fetchProfile : fetchProfiles ) { String profileName = fetchProfile.getName(); Set fetches = new HashSet(); 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 d4464bde3d..75a4fd5cf5 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 @@ -156,9 +156,17 @@ public class MetadataImpl implements MetadataImplementor, Serializable { return namedQueryDefs.get( name ); } - public void addTypeDef(String name, TypeDef typeDef) { - // TODO - should we check whether the typedef already exists? Log it? Exception? (HF) - typeDefs.put( name, typeDef ); + @Override + public void addTypeDef(TypeDef typeDef) { + final TypeDef previous = typeDefs.put( typeDef.getName(), typeDef ); + if ( previous != null ) { + LOG.debugf( "Duplicate typedef name [%s] now -> %s", typeDef.getName(), typeDef.getTypeClass() ); + } + } + + @Override + public Iterable getTypeDefs() { + return typeDefs.values(); } public TypeDef getTypeDef(String name) { diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/spi/MetadataImplementor.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/spi/MetadataImplementor.java index e20823bcf4..ae96919239 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/spi/MetadataImplementor.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/spi/MetadataImplementor.java @@ -30,6 +30,7 @@ import org.hibernate.metamodel.Metadata; import org.hibernate.metamodel.binding.EntityBinding; import org.hibernate.metamodel.binding.FetchProfile; import org.hibernate.metamodel.binding.PluralAttributeBinding; +import org.hibernate.metamodel.binding.TypeDef; import org.hibernate.metamodel.relational.Database; import org.hibernate.service.BasicServiceRegistry; import org.hibernate.type.TypeResolver; @@ -55,4 +56,8 @@ public interface MetadataImplementor extends Metadata { public void addCollection(PluralAttributeBinding collectionBinding); public void addFetchProfile( FetchProfile profile ); + + public void addTypeDef(TypeDef typeDef); + + public Iterable getTypeDefs(); }