From 95a06fc49f3d22cbe938f7c995f4f45c367f1d7f Mon Sep 17 00:00:00 2001 From: Strong Liu Date: Mon, 16 Jul 2012 21:30:30 +0800 Subject: [PATCH] HHH-7450 simplify xsd --- .../source/hbm/HibernateMappingProcessor.java | 102 +++++++++++------- .../internal/source/hbm/HierarchyBuilder.java | 72 +++++++------ .../org/hibernate/hibernate-mapping-4.0.xsd | 26 +++++ 3 files changed, 132 insertions(+), 68 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/hbm/HibernateMappingProcessor.java b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/hbm/HibernateMappingProcessor.java index 39ef5489fb..f8335ec871 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/hbm/HibernateMappingProcessor.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/hbm/HibernateMappingProcessor.java @@ -32,10 +32,11 @@ import java.util.List; import java.util.Map; import java.util.Set; +import org.jboss.logging.Logger; + import org.hibernate.CacheMode; import org.hibernate.FlushMode; import org.hibernate.LockMode; -import org.hibernate.bytecode.buildtime.spi.Logger; import org.hibernate.cfg.HbmBinder; import org.hibernate.engine.ResultSetMappingDefinition; import org.hibernate.engine.query.spi.sql.NativeSQLQueryJoinReturn; @@ -47,12 +48,14 @@ import org.hibernate.engine.spi.NamedSQLQueryDefinition; import org.hibernate.internal.CoreMessageLogger; import org.hibernate.internal.jaxb.Origin; import org.hibernate.internal.jaxb.mapping.hbm.EntityElement; +import org.hibernate.internal.jaxb.mapping.hbm.JaxbClassElement; import org.hibernate.internal.jaxb.mapping.hbm.JaxbDatabaseObjectElement; import org.hibernate.internal.jaxb.mapping.hbm.JaxbFetchProfileElement; import org.hibernate.internal.jaxb.mapping.hbm.JaxbFilterDefElement; import org.hibernate.internal.jaxb.mapping.hbm.JaxbHibernateMapping; import org.hibernate.internal.jaxb.mapping.hbm.JaxbIdentifierGeneratorElement; import org.hibernate.internal.jaxb.mapping.hbm.JaxbImportElement; +import org.hibernate.internal.jaxb.mapping.hbm.JaxbJoinedSubclassElement; import org.hibernate.internal.jaxb.mapping.hbm.JaxbLoadCollectionElement; import org.hibernate.internal.jaxb.mapping.hbm.JaxbQueryElement; import org.hibernate.internal.jaxb.mapping.hbm.JaxbQueryParamElement; @@ -61,12 +64,13 @@ import org.hibernate.internal.jaxb.mapping.hbm.JaxbReturnElement; import org.hibernate.internal.jaxb.mapping.hbm.JaxbReturnJoinElement; import org.hibernate.internal.jaxb.mapping.hbm.JaxbReturnScalarElement; import org.hibernate.internal.jaxb.mapping.hbm.JaxbSqlQueryElement; +import org.hibernate.internal.jaxb.mapping.hbm.JaxbSubclassElement; import org.hibernate.internal.jaxb.mapping.hbm.JaxbSynchronizeElement; import org.hibernate.internal.jaxb.mapping.hbm.JaxbTypedefElement; +import org.hibernate.internal.jaxb.mapping.hbm.JaxbUnionSubclassElement; import org.hibernate.internal.util.StringHelper; import org.hibernate.internal.util.Value; import org.hibernate.internal.util.collections.CollectionHelper; -import org.hibernate.mapping.PersistentClass; import org.hibernate.metamodel.spi.binding.EntityBinding; import org.hibernate.metamodel.spi.binding.FetchProfile; import org.hibernate.metamodel.spi.relational.AuxiliaryDatabaseObject; @@ -88,9 +92,7 @@ import org.hibernate.type.Type; * @author Strong Liu */ public class HibernateMappingProcessor { - private static final CoreMessageLogger LOG = org.jboss - .logging - .Logger + private static final CoreMessageLogger LOG = Logger .getMessageLogger( CoreMessageLogger.class, HibernateMappingProcessor.class.getName() ); private final MetadataImplementor metadata; private final MappingDocument mappingDocument; @@ -244,23 +246,50 @@ public class HibernateMappingProcessor { metadata.addImport( className, rename ); } if ( root.isAutoImport() ) { - for ( Object obj : root.getClazzOrSubclassOrJoinedSubclass() ) { - EntityElement entityElement = ( EntityElement ) obj; - String qualifiedName = bindingContext().determineEntityName( entityElement ); - metadata.addImport( entityElement.getEntityName() == null - ? entityElement.getName() - : entityElement.getEntityName(), qualifiedName ); + for(final JaxbClassElement element : root.getClazz()){ + processEntityElement( element ); + } + for(final JaxbJoinedSubclassElement element : root.getJoinedSubclass()){ + processEntityElement( element ); + } + for(final JaxbUnionSubclassElement element : root.getUnionSubclass()){ + processEntityElement( element ); + } + for(final JaxbSubclassElement element : root.getSubclass()){ + processEntityElement( element ); } } } + private void processEntityElement(EntityElement element) { + EntityElement entityElement = element; + String qualifiedName = bindingContext().determineEntityName( entityElement ); + metadata.addImport( entityElement.getEntityName() == null + ? entityElement.getName() + : entityElement.getEntityName(), qualifiedName ); + } + private void processResultSetMappings() { List resultsetElements = new ArrayList(); if ( CollectionHelper.isNotEmpty( mappingRoot().getResultset() ) ) { resultsetElements.addAll( mappingRoot().getResultset() ); } - for ( Object obj : mappingRoot().getClazzOrSubclassOrJoinedSubclass() ) { - EntityElement element = EntityElement.class.cast( obj ); + for(final JaxbClassElement element : mappingRoot().getClazz()){ + if ( CollectionHelper.isNotEmpty( element.getResultset() ) ) { + resultsetElements.addAll( element.getResultset() ); + } + } + for(final JaxbJoinedSubclassElement element : mappingRoot().getJoinedSubclass()){ + if ( CollectionHelper.isNotEmpty( element.getResultset() ) ) { + resultsetElements.addAll( element.getResultset() ); + } + } + for(final JaxbUnionSubclassElement element : mappingRoot().getUnionSubclass()){ + if ( CollectionHelper.isNotEmpty( element.getResultset() ) ) { + resultsetElements.addAll( element.getResultset() ); + } + } + for(final JaxbSubclassElement element : mappingRoot().getSubclass()){ if ( CollectionHelper.isNotEmpty( element.getResultset() ) ) { resultsetElements.addAll( element.getResultset() ); } @@ -276,33 +305,34 @@ public class HibernateMappingProcessor { private void bindResultSetMappingDefinitions(JaxbResultsetElement element) { final ResultSetMappingDefinition definition = new ResultSetMappingDefinition( element.getName() ); - final List returns = element.getReturnScalarOrReturnOrReturnJoin(); int cnt=0; - for ( final Object obj : returns ) { + NativeSQLQueryReturn nativeSQLQueryReturn; + for(final JaxbReturnScalarElement r : element.getReturnScalar()){ cnt++; - final NativeSQLQueryReturn nativeSQLQueryReturn; - if ( JaxbReturnScalarElement.class.isInstance( obj ) ) { - JaxbReturnScalarElement scalarElement = JaxbReturnScalarElement.class.cast( obj ); - String column = scalarElement.getColumn(); - String typeFromXML = scalarElement.getType(); - Type type = metadata.getTypeResolver().heuristicType( typeFromXML ); - nativeSQLQueryReturn = new NativeSQLQueryScalarReturn( column, type ); - } - else if ( JaxbReturnJoinElement.class.isInstance( obj ) ) { - nativeSQLQueryReturn = bindReturnJoin(JaxbReturnJoinElement.class.cast( obj ), cnt); - - } - else if ( JaxbLoadCollectionElement.class.isInstance( obj ) ) { - nativeSQLQueryReturn = bindLoadCollection(JaxbLoadCollectionElement.class.cast( obj ), cnt); - } - else if ( JaxbReturnElement.class.isInstance( obj ) ) { - nativeSQLQueryReturn= bindReturn(JaxbReturnElement.class.cast( obj ), cnt); - - }else { - throw new MappingException( "unknown type of Result set mapping return: "+obj.getClass().getName() , origin()); - } + String column = r.getColumn(); + String typeFromXML = r.getType(); + Type type = metadata.getTypeResolver().heuristicType( typeFromXML ); + nativeSQLQueryReturn = new NativeSQLQueryScalarReturn( column, type ); definition.addQueryReturn( nativeSQLQueryReturn ); } + for(final JaxbReturnJoinElement r : element.getReturnJoin()){ + cnt++; + nativeSQLQueryReturn = bindReturnJoin(r, cnt); + definition.addQueryReturn( nativeSQLQueryReturn ); + + } + for(final JaxbLoadCollectionElement r : element.getLoadCollection()){ + cnt++; + nativeSQLQueryReturn = bindLoadCollection( r, cnt ); + definition.addQueryReturn( nativeSQLQueryReturn ); + + } + for(final JaxbReturnElement r : element.getReturn()){ + cnt++; + nativeSQLQueryReturn = bindReturn( r, cnt ); + definition.addQueryReturn( nativeSQLQueryReturn ); + + } metadata.addResultSetMapping( definition ); } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/hbm/HierarchyBuilder.java b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/hbm/HierarchyBuilder.java index 3476f72172..250f287c7d 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/hbm/HierarchyBuilder.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/hbm/HierarchyBuilder.java @@ -65,40 +65,48 @@ public class HierarchyBuilder { } private void processCurrentMappingDocument() { - for ( Object entityElementO : currentMappingDocument.getMappingRoot().getClazzOrSubclassOrJoinedSubclass() ) { - final EntityElement entityElement = (EntityElement) entityElementO; - if ( JaxbClassElement.class.isInstance( entityElement ) ) { - // we can immediately handle elements in terms of creating the hierarchy entry - final JaxbClassElement jaxbClass = (JaxbClassElement) entityElement; - final RootEntitySourceImpl rootEntitySource = new RootEntitySourceImpl( currentMappingDocument, - jaxbClass - ); - final EntityHierarchyImpl hierarchy = new EntityHierarchyImpl( rootEntitySource ); + for(final JaxbClassElement jaxbClass : currentMappingDocument.getMappingRoot().getClazz()){ + // we can immediately handle elements in terms of creating the hierarchy entry + final RootEntitySourceImpl rootEntitySource = new RootEntitySourceImpl( currentMappingDocument, + jaxbClass + ); + final EntityHierarchyImpl hierarchy = new EntityHierarchyImpl( rootEntitySource ); - entityHierarchies.add( hierarchy ); - subEntityContainerMap.put( rootEntitySource.getEntityName(), rootEntitySource ); + entityHierarchies.add( hierarchy ); + subEntityContainerMap.put( rootEntitySource.getEntityName(), rootEntitySource ); - processSubElements( entityElement, rootEntitySource ); - } - else { - // we have to see if this things super-type has been found yet, and if not add it to the - // extends queue - final String entityItExtends = currentMappingDocument.getMappingLocalBindingContext() - .qualifyClassName( ( (SubEntityElement) entityElement ).getExtends() ); - final SubclassEntityContainer container = subEntityContainerMap.get( entityItExtends ); - final SubclassEntitySourceImpl subClassEntitySource = new SubclassEntitySourceImpl( currentMappingDocument, entityElement, ( EntitySource ) container ); - final String entityName = subClassEntitySource.getEntityName(); - subEntityContainerMap.put( entityName, subClassEntitySource ); - processSubElements( entityElement, subClassEntitySource ); - if ( container != null ) { - // we already have this entity's super, attach it and continue - container.add( subClassEntitySource ); - } - else { - // we do not yet have the super and have to wait, so add it fto the extends queue - extendsQueue.add( new ExtendsQueueEntry( subClassEntitySource, entityItExtends ) ); - } - } + processSubElements( jaxbClass, rootEntitySource ); + } + for(final JaxbJoinedSubclassElement element : currentMappingDocument.getMappingRoot().getJoinedSubclass()){ + processSubclassElement( element ); + + } + for(final JaxbUnionSubclassElement element : currentMappingDocument.getMappingRoot().getUnionSubclass()){ + processSubclassElement( element ); + } + for(final JaxbSubclassElement element : currentMappingDocument.getMappingRoot().getSubclass()){ + processSubclassElement( element ); + } + + } + + private void processSubclassElement(SubEntityElement element) { + // we have to see if this things super-type has been found yet, and if not add it to the + // extends queue + final String entityItExtends = currentMappingDocument.getMappingLocalBindingContext() + .qualifyClassName( element.getExtends() ); + final SubclassEntityContainer container = subEntityContainerMap.get( entityItExtends ); + final SubclassEntitySourceImpl subClassEntitySource = new SubclassEntitySourceImpl( currentMappingDocument, element, (EntitySource) container ); + final String entityName = subClassEntitySource.getEntityName(); + subEntityContainerMap.put( entityName, subClassEntitySource ); + processSubElements( element, subClassEntitySource ); + if ( container != null ) { + // we already have this entity's super, attach it and continue + container.add( subClassEntitySource ); + } + else { + // we do not yet have the super and have to wait, so add it fto the extends queue + extendsQueue.add( new ExtendsQueueEntry( subClassEntitySource, entityItExtends ) ); } } diff --git a/hibernate-core/src/main/resources/org/hibernate/hibernate-mapping-4.0.xsd b/hibernate-core/src/main/resources/org/hibernate/hibernate-mapping-4.0.xsd index 5c95077d7d..b6f57061e7 100644 --- a/hibernate-core/src/main/resources/org/hibernate/hibernate-mapping-4.0.xsd +++ b/hibernate-core/src/main/resources/org/hibernate/hibernate-mapping-4.0.xsd @@ -43,6 +43,11 @@ arbitrary number of queries, and import declarations of arbitrary classes. --> + + + + + + + + + + @@ -1397,6 +1422,7 @@ arbitrary number of queries, and import declarations of arbitrary classes. +