HHH-7450 simplify xsd

This commit is contained in:
Strong Liu 2012-07-16 21:30:30 +08:00
parent d6ab4a203a
commit 95a06fc49f
3 changed files with 132 additions and 68 deletions

View File

@ -32,10 +32,11 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import org.jboss.logging.Logger;
import org.hibernate.CacheMode; import org.hibernate.CacheMode;
import org.hibernate.FlushMode; import org.hibernate.FlushMode;
import org.hibernate.LockMode; import org.hibernate.LockMode;
import org.hibernate.bytecode.buildtime.spi.Logger;
import org.hibernate.cfg.HbmBinder; import org.hibernate.cfg.HbmBinder;
import org.hibernate.engine.ResultSetMappingDefinition; import org.hibernate.engine.ResultSetMappingDefinition;
import org.hibernate.engine.query.spi.sql.NativeSQLQueryJoinReturn; 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.CoreMessageLogger;
import org.hibernate.internal.jaxb.Origin; import org.hibernate.internal.jaxb.Origin;
import org.hibernate.internal.jaxb.mapping.hbm.EntityElement; 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.JaxbDatabaseObjectElement;
import org.hibernate.internal.jaxb.mapping.hbm.JaxbFetchProfileElement; import org.hibernate.internal.jaxb.mapping.hbm.JaxbFetchProfileElement;
import org.hibernate.internal.jaxb.mapping.hbm.JaxbFilterDefElement; import org.hibernate.internal.jaxb.mapping.hbm.JaxbFilterDefElement;
import org.hibernate.internal.jaxb.mapping.hbm.JaxbHibernateMapping; import org.hibernate.internal.jaxb.mapping.hbm.JaxbHibernateMapping;
import org.hibernate.internal.jaxb.mapping.hbm.JaxbIdentifierGeneratorElement; import org.hibernate.internal.jaxb.mapping.hbm.JaxbIdentifierGeneratorElement;
import org.hibernate.internal.jaxb.mapping.hbm.JaxbImportElement; 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.JaxbLoadCollectionElement;
import org.hibernate.internal.jaxb.mapping.hbm.JaxbQueryElement; import org.hibernate.internal.jaxb.mapping.hbm.JaxbQueryElement;
import org.hibernate.internal.jaxb.mapping.hbm.JaxbQueryParamElement; 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.JaxbReturnJoinElement;
import org.hibernate.internal.jaxb.mapping.hbm.JaxbReturnScalarElement; import org.hibernate.internal.jaxb.mapping.hbm.JaxbReturnScalarElement;
import org.hibernate.internal.jaxb.mapping.hbm.JaxbSqlQueryElement; 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.JaxbSynchronizeElement;
import org.hibernate.internal.jaxb.mapping.hbm.JaxbTypedefElement; 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.StringHelper;
import org.hibernate.internal.util.Value; import org.hibernate.internal.util.Value;
import org.hibernate.internal.util.collections.CollectionHelper; 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.EntityBinding;
import org.hibernate.metamodel.spi.binding.FetchProfile; import org.hibernate.metamodel.spi.binding.FetchProfile;
import org.hibernate.metamodel.spi.relational.AuxiliaryDatabaseObject; import org.hibernate.metamodel.spi.relational.AuxiliaryDatabaseObject;
@ -88,9 +92,7 @@ import org.hibernate.type.Type;
* @author Strong Liu * @author Strong Liu
*/ */
public class HibernateMappingProcessor { public class HibernateMappingProcessor {
private static final CoreMessageLogger LOG = org.jboss private static final CoreMessageLogger LOG = Logger
.logging
.Logger
.getMessageLogger( CoreMessageLogger.class, HibernateMappingProcessor.class.getName() ); .getMessageLogger( CoreMessageLogger.class, HibernateMappingProcessor.class.getName() );
private final MetadataImplementor metadata; private final MetadataImplementor metadata;
private final MappingDocument mappingDocument; private final MappingDocument mappingDocument;
@ -244,23 +246,50 @@ public class HibernateMappingProcessor {
metadata.addImport( className, rename ); metadata.addImport( className, rename );
} }
if ( root.isAutoImport() ) { if ( root.isAutoImport() ) {
for ( Object obj : root.getClazzOrSubclassOrJoinedSubclass() ) { for(final JaxbClassElement element : root.getClazz()){
EntityElement entityElement = ( EntityElement ) obj; processEntityElement( element );
String qualifiedName = bindingContext().determineEntityName( entityElement ); }
metadata.addImport( entityElement.getEntityName() == null for(final JaxbJoinedSubclassElement element : root.getJoinedSubclass()){
? entityElement.getName() processEntityElement( element );
: entityElement.getEntityName(), qualifiedName ); }
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() { private void processResultSetMappings() {
List<JaxbResultsetElement> resultsetElements = new ArrayList<JaxbResultsetElement>(); List<JaxbResultsetElement> resultsetElements = new ArrayList<JaxbResultsetElement>();
if ( CollectionHelper.isNotEmpty( mappingRoot().getResultset() ) ) { if ( CollectionHelper.isNotEmpty( mappingRoot().getResultset() ) ) {
resultsetElements.addAll( mappingRoot().getResultset() ); resultsetElements.addAll( mappingRoot().getResultset() );
} }
for ( Object obj : mappingRoot().getClazzOrSubclassOrJoinedSubclass() ) { for(final JaxbClassElement element : mappingRoot().getClazz()){
EntityElement element = EntityElement.class.cast( obj ); 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() ) ) { if ( CollectionHelper.isNotEmpty( element.getResultset() ) ) {
resultsetElements.addAll( element.getResultset() ); resultsetElements.addAll( element.getResultset() );
} }
@ -276,33 +305,34 @@ public class HibernateMappingProcessor {
private void bindResultSetMappingDefinitions(JaxbResultsetElement element) { private void bindResultSetMappingDefinitions(JaxbResultsetElement element) {
final ResultSetMappingDefinition definition = new ResultSetMappingDefinition( element.getName() ); final ResultSetMappingDefinition definition = new ResultSetMappingDefinition( element.getName() );
final List returns = element.getReturnScalarOrReturnOrReturnJoin();
int cnt=0; int cnt=0;
for ( final Object obj : returns ) { NativeSQLQueryReturn nativeSQLQueryReturn;
for(final JaxbReturnScalarElement r : element.getReturnScalar()){
cnt++; cnt++;
final NativeSQLQueryReturn nativeSQLQueryReturn; String column = r.getColumn();
if ( JaxbReturnScalarElement.class.isInstance( obj ) ) { String typeFromXML = r.getType();
JaxbReturnScalarElement scalarElement = JaxbReturnScalarElement.class.cast( obj ); Type type = metadata.getTypeResolver().heuristicType( typeFromXML );
String column = scalarElement.getColumn(); nativeSQLQueryReturn = new NativeSQLQueryScalarReturn( column, type );
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());
}
definition.addQueryReturn( nativeSQLQueryReturn ); 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 ); metadata.addResultSetMapping( definition );
} }

View File

@ -65,40 +65,48 @@ public class HierarchyBuilder {
} }
private void processCurrentMappingDocument() { private void processCurrentMappingDocument() {
for ( Object entityElementO : currentMappingDocument.getMappingRoot().getClazzOrSubclassOrJoinedSubclass() ) { for(final JaxbClassElement jaxbClass : currentMappingDocument.getMappingRoot().getClazz()){
final EntityElement entityElement = (EntityElement) entityElementO; // we can immediately handle <class/> elements in terms of creating the hierarchy entry
if ( JaxbClassElement.class.isInstance( entityElement ) ) { final RootEntitySourceImpl rootEntitySource = new RootEntitySourceImpl( currentMappingDocument,
// we can immediately handle <class/> elements in terms of creating the hierarchy entry jaxbClass
final JaxbClassElement jaxbClass = (JaxbClassElement) entityElement; );
final RootEntitySourceImpl rootEntitySource = new RootEntitySourceImpl( currentMappingDocument, final EntityHierarchyImpl hierarchy = new EntityHierarchyImpl( rootEntitySource );
jaxbClass
);
final EntityHierarchyImpl hierarchy = new EntityHierarchyImpl( rootEntitySource );
entityHierarchies.add( hierarchy ); entityHierarchies.add( hierarchy );
subEntityContainerMap.put( rootEntitySource.getEntityName(), rootEntitySource ); subEntityContainerMap.put( rootEntitySource.getEntityName(), rootEntitySource );
processSubElements( entityElement, rootEntitySource ); processSubElements( jaxbClass, rootEntitySource );
} }
else { for(final JaxbJoinedSubclassElement element : currentMappingDocument.getMappingRoot().getJoinedSubclass()){
// we have to see if this things super-type has been found yet, and if not add it to the processSubclassElement( element );
// extends queue
final String entityItExtends = currentMappingDocument.getMappingLocalBindingContext() }
.qualifyClassName( ( (SubEntityElement) entityElement ).getExtends() ); for(final JaxbUnionSubclassElement element : currentMappingDocument.getMappingRoot().getUnionSubclass()){
final SubclassEntityContainer container = subEntityContainerMap.get( entityItExtends ); processSubclassElement( element );
final SubclassEntitySourceImpl subClassEntitySource = new SubclassEntitySourceImpl( currentMappingDocument, entityElement, ( EntitySource ) container ); }
final String entityName = subClassEntitySource.getEntityName(); for(final JaxbSubclassElement element : currentMappingDocument.getMappingRoot().getSubclass()){
subEntityContainerMap.put( entityName, subClassEntitySource ); processSubclassElement( element );
processSubElements( entityElement, subClassEntitySource ); }
if ( container != null ) {
// we already have this entity's super, attach it and continue }
container.add( subClassEntitySource );
} private void processSubclassElement(SubEntityElement element) {
else { // we have to see if this things super-type has been found yet, and if not add it to the
// we do not yet have the super and have to wait, so add it fto the extends queue // extends queue
extendsQueue.add( new ExtendsQueueEntry( subClassEntitySource, entityItExtends ) ); 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 ) );
} }
} }

View File

@ -43,6 +43,11 @@ arbitrary number of queries, and import declarations of arbitrary classes.
--> -->
<xs:element name="import" minOccurs="0" maxOccurs="unbounded" type="import-element"/> <xs:element name="import" minOccurs="0" maxOccurs="unbounded" type="import-element"/>
<xs:choice minOccurs="0" maxOccurs="unbounded"> <xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:annotation>
<xs:appinfo>
<simplify:as-element-property/>
</xs:appinfo>
</xs:annotation>
<!-- <!--
Root entity mapping. Poorly named as entities do not have to be represented by Root entity mapping. Poorly named as entities do not have to be represented by
classes at all. Mapped entities may be represented via different methodologies classes at all. Mapped entities may be represented via different methodologies
@ -991,6 +996,11 @@ arbitrary number of queries, and import declarations of arbitrary classes.
<xs:complexType name="composite-map-key-element"> <xs:complexType name="composite-map-key-element">
<xs:sequence> <xs:sequence>
<xs:choice maxOccurs="unbounded"> <xs:choice maxOccurs="unbounded">
<xs:annotation>
<xs:appinfo>
<simplify:as-element-property/>
</xs:appinfo>
</xs:annotation>
<xs:element name="key-property" type="key-property-element"/> <xs:element name="key-property" type="key-property-element"/>
<xs:element name="key-many-to-one" type="key-many-to-one-element"/> <xs:element name="key-many-to-one" type="key-many-to-one-element"/>
</xs:choice> </xs:choice>
@ -1010,6 +1020,11 @@ arbitrary number of queries, and import declarations of arbitrary classes.
<xs:complexType name="composite-index-element"> <xs:complexType name="composite-index-element">
<xs:sequence> <xs:sequence>
<xs:choice maxOccurs="unbounded"> <xs:choice maxOccurs="unbounded">
<xs:annotation>
<xs:appinfo>
<simplify:as-element-property/>
</xs:appinfo>
</xs:annotation>
<xs:element name="key-property" type="key-property-element"/> <xs:element name="key-property" type="key-property-element"/>
<xs:element name="key-many-to-one" type="key-many-to-one-element"/> <xs:element name="key-many-to-one" type="key-many-to-one-element"/>
</xs:choice> </xs:choice>
@ -1211,6 +1226,11 @@ arbitrary number of queries, and import declarations of arbitrary classes.
<xs:complexType name="properties-element"> <xs:complexType name="properties-element">
<xs:sequence> <xs:sequence>
<xs:choice minOccurs="0" maxOccurs="unbounded"> <xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:annotation>
<xs:appinfo>
<simplify:as-element-property/>
</xs:appinfo>
</xs:annotation>
<xs:element name="property" type="property-element"/> <xs:element name="property" type="property-element"/>
<xs:element name="many-to-one" type="many-to-one-element"/> <xs:element name="many-to-one" type="many-to-one-element"/>
<xs:element name="component" type="component-element"/> <xs:element name="component" type="component-element"/>
@ -1279,6 +1299,11 @@ arbitrary number of queries, and import declarations of arbitrary classes.
<!-- The resultset element declares a named resultset mapping definition for SQL queries --> <!-- The resultset element declares a named resultset mapping definition for SQL queries -->
<xs:complexType name="resultset-element"> <xs:complexType name="resultset-element">
<xs:choice minOccurs="0" maxOccurs="unbounded"> <xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:annotation>
<xs:appinfo>
<simplify:as-element-property/>
</xs:appinfo>
</xs:annotation>
<xs:element name="return-scalar" type="return-scalar-element"/> <xs:element name="return-scalar" type="return-scalar-element"/>
<xs:element name="return" type="return-element"/> <xs:element name="return" type="return-element"/>
<xs:element name="return-join" type="return-join-element"/> <xs:element name="return-join" type="return-join-element"/>
@ -1397,6 +1422,7 @@ arbitrary number of queries, and import declarations of arbitrary classes.
<!-- The sql-query element declares a named SQL query string --> <!-- The sql-query element declares a named SQL query string -->
<xs:complexType name="sql-query-element" mixed="true"> <xs:complexType name="sql-query-element" mixed="true">
<xs:choice minOccurs="0" maxOccurs="unbounded"> <xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="return-scalar" type="return-scalar-element"/> <xs:element name="return-scalar" type="return-scalar-element"/>
<xs:element name="return" type="return-element"/> <xs:element name="return" type="return-element"/>
<xs:element name="return-join" type="return-join-element"/> <xs:element name="return-join" type="return-join-element"/>