Skip to content

Commit 95a06fc

Browse files
committed
HHH-7450 simplify xsd
1 parent d6ab4a2 commit 95a06fc

File tree

3 files changed

+132
-68
lines changed

3 files changed

+132
-68
lines changed

hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/hbm/HibernateMappingProcessor.java

Lines changed: 64 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,11 @@
3232
import java.util.Map;
3333
import java.util.Set;
3434

35+
import org.jboss.logging.Logger;
36+
3537
import org.hibernate.CacheMode;
3638
import org.hibernate.FlushMode;
3739
import org.hibernate.LockMode;
38-
import org.hibernate.bytecode.buildtime.spi.Logger;
3940
import org.hibernate.cfg.HbmBinder;
4041
import org.hibernate.engine.ResultSetMappingDefinition;
4142
import org.hibernate.engine.query.spi.sql.NativeSQLQueryJoinReturn;
@@ -47,12 +48,14 @@
4748
import org.hibernate.internal.CoreMessageLogger;
4849
import org.hibernate.internal.jaxb.Origin;
4950
import org.hibernate.internal.jaxb.mapping.hbm.EntityElement;
51+
import org.hibernate.internal.jaxb.mapping.hbm.JaxbClassElement;
5052
import org.hibernate.internal.jaxb.mapping.hbm.JaxbDatabaseObjectElement;
5153
import org.hibernate.internal.jaxb.mapping.hbm.JaxbFetchProfileElement;
5254
import org.hibernate.internal.jaxb.mapping.hbm.JaxbFilterDefElement;
5355
import org.hibernate.internal.jaxb.mapping.hbm.JaxbHibernateMapping;
5456
import org.hibernate.internal.jaxb.mapping.hbm.JaxbIdentifierGeneratorElement;
5557
import org.hibernate.internal.jaxb.mapping.hbm.JaxbImportElement;
58+
import org.hibernate.internal.jaxb.mapping.hbm.JaxbJoinedSubclassElement;
5659
import org.hibernate.internal.jaxb.mapping.hbm.JaxbLoadCollectionElement;
5760
import org.hibernate.internal.jaxb.mapping.hbm.JaxbQueryElement;
5861
import org.hibernate.internal.jaxb.mapping.hbm.JaxbQueryParamElement;
@@ -61,12 +64,13 @@
6164
import org.hibernate.internal.jaxb.mapping.hbm.JaxbReturnJoinElement;
6265
import org.hibernate.internal.jaxb.mapping.hbm.JaxbReturnScalarElement;
6366
import org.hibernate.internal.jaxb.mapping.hbm.JaxbSqlQueryElement;
67+
import org.hibernate.internal.jaxb.mapping.hbm.JaxbSubclassElement;
6468
import org.hibernate.internal.jaxb.mapping.hbm.JaxbSynchronizeElement;
6569
import org.hibernate.internal.jaxb.mapping.hbm.JaxbTypedefElement;
70+
import org.hibernate.internal.jaxb.mapping.hbm.JaxbUnionSubclassElement;
6671
import org.hibernate.internal.util.StringHelper;
6772
import org.hibernate.internal.util.Value;
6873
import org.hibernate.internal.util.collections.CollectionHelper;
69-
import org.hibernate.mapping.PersistentClass;
7074
import org.hibernate.metamodel.spi.binding.EntityBinding;
7175
import org.hibernate.metamodel.spi.binding.FetchProfile;
7276
import org.hibernate.metamodel.spi.relational.AuxiliaryDatabaseObject;
@@ -88,9 +92,7 @@
8892
* @author Strong Liu
8993
*/
9094
public class HibernateMappingProcessor {
91-
private static final CoreMessageLogger LOG = org.jboss
92-
.logging
93-
.Logger
95+
private static final CoreMessageLogger LOG = Logger
9496
.getMessageLogger( CoreMessageLogger.class, HibernateMappingProcessor.class.getName() );
9597
private final MetadataImplementor metadata;
9698
private final MappingDocument mappingDocument;
@@ -244,23 +246,50 @@ private void processImports() {
244246
metadata.addImport( className, rename );
245247
}
246248
if ( root.isAutoImport() ) {
247-
for ( Object obj : root.getClazzOrSubclassOrJoinedSubclass() ) {
248-
EntityElement entityElement = ( EntityElement ) obj;
249-
String qualifiedName = bindingContext().determineEntityName( entityElement );
250-
metadata.addImport( entityElement.getEntityName() == null
251-
? entityElement.getName()
252-
: entityElement.getEntityName(), qualifiedName );
249+
for(final JaxbClassElement element : root.getClazz()){
250+
processEntityElement( element );
251+
}
252+
for(final JaxbJoinedSubclassElement element : root.getJoinedSubclass()){
253+
processEntityElement( element );
254+
}
255+
for(final JaxbUnionSubclassElement element : root.getUnionSubclass()){
256+
processEntityElement( element );
257+
}
258+
for(final JaxbSubclassElement element : root.getSubclass()){
259+
processEntityElement( element );
253260
}
254261
}
255262
}
256263

264+
private void processEntityElement(EntityElement element) {
265+
EntityElement entityElement = element;
266+
String qualifiedName = bindingContext().determineEntityName( entityElement );
267+
metadata.addImport( entityElement.getEntityName() == null
268+
? entityElement.getName()
269+
: entityElement.getEntityName(), qualifiedName );
270+
}
271+
257272
private void processResultSetMappings() {
258273
List<JaxbResultsetElement> resultsetElements = new ArrayList<JaxbResultsetElement>();
259274
if ( CollectionHelper.isNotEmpty( mappingRoot().getResultset() ) ) {
260275
resultsetElements.addAll( mappingRoot().getResultset() );
261276
}
262-
for ( Object obj : mappingRoot().getClazzOrSubclassOrJoinedSubclass() ) {
263-
EntityElement element = EntityElement.class.cast( obj );
277+
for(final JaxbClassElement element : mappingRoot().getClazz()){
278+
if ( CollectionHelper.isNotEmpty( element.getResultset() ) ) {
279+
resultsetElements.addAll( element.getResultset() );
280+
}
281+
}
282+
for(final JaxbJoinedSubclassElement element : mappingRoot().getJoinedSubclass()){
283+
if ( CollectionHelper.isNotEmpty( element.getResultset() ) ) {
284+
resultsetElements.addAll( element.getResultset() );
285+
}
286+
}
287+
for(final JaxbUnionSubclassElement element : mappingRoot().getUnionSubclass()){
288+
if ( CollectionHelper.isNotEmpty( element.getResultset() ) ) {
289+
resultsetElements.addAll( element.getResultset() );
290+
}
291+
}
292+
for(final JaxbSubclassElement element : mappingRoot().getSubclass()){
264293
if ( CollectionHelper.isNotEmpty( element.getResultset() ) ) {
265294
resultsetElements.addAll( element.getResultset() );
266295
}
@@ -276,32 +305,33 @@ private void processResultSetMappings() {
276305

277306
private void bindResultSetMappingDefinitions(JaxbResultsetElement element) {
278307
final ResultSetMappingDefinition definition = new ResultSetMappingDefinition( element.getName() );
279-
final List returns = element.getReturnScalarOrReturnOrReturnJoin();
280308
int cnt=0;
281-
for ( final Object obj : returns ) {
309+
NativeSQLQueryReturn nativeSQLQueryReturn;
310+
for(final JaxbReturnScalarElement r : element.getReturnScalar()){
282311
cnt++;
283-
final NativeSQLQueryReturn nativeSQLQueryReturn;
284-
if ( JaxbReturnScalarElement.class.isInstance( obj ) ) {
285-
JaxbReturnScalarElement scalarElement = JaxbReturnScalarElement.class.cast( obj );
286-
String column = scalarElement.getColumn();
287-
String typeFromXML = scalarElement.getType();
288-
Type type = metadata.getTypeResolver().heuristicType( typeFromXML );
289-
nativeSQLQueryReturn = new NativeSQLQueryScalarReturn( column, type );
290-
}
291-
else if ( JaxbReturnJoinElement.class.isInstance( obj ) ) {
292-
nativeSQLQueryReturn = bindReturnJoin(JaxbReturnJoinElement.class.cast( obj ), cnt);
312+
String column = r.getColumn();
313+
String typeFromXML = r.getType();
314+
Type type = metadata.getTypeResolver().heuristicType( typeFromXML );
315+
nativeSQLQueryReturn = new NativeSQLQueryScalarReturn( column, type );
316+
definition.addQueryReturn( nativeSQLQueryReturn );
317+
}
318+
for(final JaxbReturnJoinElement r : element.getReturnJoin()){
319+
cnt++;
320+
nativeSQLQueryReturn = bindReturnJoin(r, cnt);
321+
definition.addQueryReturn( nativeSQLQueryReturn );
293322

294-
}
295-
else if ( JaxbLoadCollectionElement.class.isInstance( obj ) ) {
296-
nativeSQLQueryReturn = bindLoadCollection(JaxbLoadCollectionElement.class.cast( obj ), cnt);
297-
}
298-
else if ( JaxbReturnElement.class.isInstance( obj ) ) {
299-
nativeSQLQueryReturn= bindReturn(JaxbReturnElement.class.cast( obj ), cnt);
323+
}
324+
for(final JaxbLoadCollectionElement r : element.getLoadCollection()){
325+
cnt++;
326+
nativeSQLQueryReturn = bindLoadCollection( r, cnt );
327+
definition.addQueryReturn( nativeSQLQueryReturn );
300328

301-
}else {
302-
throw new MappingException( "unknown type of Result set mapping return: "+obj.getClass().getName() , origin());
303-
}
329+
}
330+
for(final JaxbReturnElement r : element.getReturn()){
331+
cnt++;
332+
nativeSQLQueryReturn = bindReturn( r, cnt );
304333
definition.addQueryReturn( nativeSQLQueryReturn );
334+
305335
}
306336
metadata.addResultSetMapping( definition );
307337

hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/hbm/HierarchyBuilder.java

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -65,40 +65,48 @@ public void processMappingDocument(MappingDocument mappingDocument) {
6565
}
6666

6767
private void processCurrentMappingDocument() {
68-
for ( Object entityElementO : currentMappingDocument.getMappingRoot().getClazzOrSubclassOrJoinedSubclass() ) {
69-
final EntityElement entityElement = (EntityElement) entityElementO;
70-
if ( JaxbClassElement.class.isInstance( entityElement ) ) {
71-
// we can immediately handle <class/> elements in terms of creating the hierarchy entry
72-
final JaxbClassElement jaxbClass = (JaxbClassElement) entityElement;
73-
final RootEntitySourceImpl rootEntitySource = new RootEntitySourceImpl( currentMappingDocument,
74-
jaxbClass
75-
);
76-
final EntityHierarchyImpl hierarchy = new EntityHierarchyImpl( rootEntitySource );
77-
78-
entityHierarchies.add( hierarchy );
79-
subEntityContainerMap.put( rootEntitySource.getEntityName(), rootEntitySource );
80-
81-
processSubElements( entityElement, rootEntitySource );
82-
}
83-
else {
84-
// we have to see if this things super-type has been found yet, and if not add it to the
85-
// extends queue
86-
final String entityItExtends = currentMappingDocument.getMappingLocalBindingContext()
87-
.qualifyClassName( ( (SubEntityElement) entityElement ).getExtends() );
88-
final SubclassEntityContainer container = subEntityContainerMap.get( entityItExtends );
89-
final SubclassEntitySourceImpl subClassEntitySource = new SubclassEntitySourceImpl( currentMappingDocument, entityElement, ( EntitySource ) container );
90-
final String entityName = subClassEntitySource.getEntityName();
91-
subEntityContainerMap.put( entityName, subClassEntitySource );
92-
processSubElements( entityElement, subClassEntitySource );
93-
if ( container != null ) {
94-
// we already have this entity's super, attach it and continue
95-
container.add( subClassEntitySource );
96-
}
97-
else {
98-
// we do not yet have the super and have to wait, so add it fto the extends queue
99-
extendsQueue.add( new ExtendsQueueEntry( subClassEntitySource, entityItExtends ) );
100-
}
101-
}
68+
for(final JaxbClassElement jaxbClass : currentMappingDocument.getMappingRoot().getClazz()){
69+
// we can immediately handle <class/> elements in terms of creating the hierarchy entry
70+
final RootEntitySourceImpl rootEntitySource = new RootEntitySourceImpl( currentMappingDocument,
71+
jaxbClass
72+
);
73+
final EntityHierarchyImpl hierarchy = new EntityHierarchyImpl( rootEntitySource );
74+
75+
entityHierarchies.add( hierarchy );
76+
subEntityContainerMap.put( rootEntitySource.getEntityName(), rootEntitySource );
77+
78+
processSubElements( jaxbClass, rootEntitySource );
79+
}
80+
for(final JaxbJoinedSubclassElement element : currentMappingDocument.getMappingRoot().getJoinedSubclass()){
81+
processSubclassElement( element );
82+
83+
}
84+
for(final JaxbUnionSubclassElement element : currentMappingDocument.getMappingRoot().getUnionSubclass()){
85+
processSubclassElement( element );
86+
}
87+
for(final JaxbSubclassElement element : currentMappingDocument.getMappingRoot().getSubclass()){
88+
processSubclassElement( element );
89+
}
90+
91+
}
92+
93+
private void processSubclassElement(SubEntityElement element) {
94+
// we have to see if this things super-type has been found yet, and if not add it to the
95+
// extends queue
96+
final String entityItExtends = currentMappingDocument.getMappingLocalBindingContext()
97+
.qualifyClassName( element.getExtends() );
98+
final SubclassEntityContainer container = subEntityContainerMap.get( entityItExtends );
99+
final SubclassEntitySourceImpl subClassEntitySource = new SubclassEntitySourceImpl( currentMappingDocument, element, (EntitySource) container );
100+
final String entityName = subClassEntitySource.getEntityName();
101+
subEntityContainerMap.put( entityName, subClassEntitySource );
102+
processSubElements( element, subClassEntitySource );
103+
if ( container != null ) {
104+
// we already have this entity's super, attach it and continue
105+
container.add( subClassEntitySource );
106+
}
107+
else {
108+
// we do not yet have the super and have to wait, so add it fto the extends queue
109+
extendsQueue.add( new ExtendsQueueEntry( subClassEntitySource, entityItExtends ) );
102110
}
103111
}
104112

hibernate-core/src/main/resources/org/hibernate/hibernate-mapping-4.0.xsd

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ arbitrary number of queries, and import declarations of arbitrary classes.
4343
-->
4444
<xs:element name="import" minOccurs="0" maxOccurs="unbounded" type="import-element"/>
4545
<xs:choice minOccurs="0" maxOccurs="unbounded">
46+
<xs:annotation>
47+
<xs:appinfo>
48+
<simplify:as-element-property/>
49+
</xs:appinfo>
50+
</xs:annotation>
4651
<!--
4752
Root entity mapping. Poorly named as entities do not have to be represented by
4853
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.
991996
<xs:complexType name="composite-map-key-element">
992997
<xs:sequence>
993998
<xs:choice maxOccurs="unbounded">
999+
<xs:annotation>
1000+
<xs:appinfo>
1001+
<simplify:as-element-property/>
1002+
</xs:appinfo>
1003+
</xs:annotation>
9941004
<xs:element name="key-property" type="key-property-element"/>
9951005
<xs:element name="key-many-to-one" type="key-many-to-one-element"/>
9961006
</xs:choice>
@@ -1010,6 +1020,11 @@ arbitrary number of queries, and import declarations of arbitrary classes.
10101020
<xs:complexType name="composite-index-element">
10111021
<xs:sequence>
10121022
<xs:choice maxOccurs="unbounded">
1023+
<xs:annotation>
1024+
<xs:appinfo>
1025+
<simplify:as-element-property/>
1026+
</xs:appinfo>
1027+
</xs:annotation>
10131028
<xs:element name="key-property" type="key-property-element"/>
10141029
<xs:element name="key-many-to-one" type="key-many-to-one-element"/>
10151030
</xs:choice>
@@ -1211,6 +1226,11 @@ arbitrary number of queries, and import declarations of arbitrary classes.
12111226
<xs:complexType name="properties-element">
12121227
<xs:sequence>
12131228
<xs:choice minOccurs="0" maxOccurs="unbounded">
1229+
<xs:annotation>
1230+
<xs:appinfo>
1231+
<simplify:as-element-property/>
1232+
</xs:appinfo>
1233+
</xs:annotation>
12141234
<xs:element name="property" type="property-element"/>
12151235
<xs:element name="many-to-one" type="many-to-one-element"/>
12161236
<xs:element name="component" type="component-element"/>
@@ -1279,6 +1299,11 @@ arbitrary number of queries, and import declarations of arbitrary classes.
12791299
<!-- The resultset element declares a named resultset mapping definition for SQL queries -->
12801300
<xs:complexType name="resultset-element">
12811301
<xs:choice minOccurs="0" maxOccurs="unbounded">
1302+
<xs:annotation>
1303+
<xs:appinfo>
1304+
<simplify:as-element-property/>
1305+
</xs:appinfo>
1306+
</xs:annotation>
12821307
<xs:element name="return-scalar" type="return-scalar-element"/>
12831308
<xs:element name="return" type="return-element"/>
12841309
<xs:element name="return-join" type="return-join-element"/>
@@ -1397,6 +1422,7 @@ arbitrary number of queries, and import declarations of arbitrary classes.
13971422
<!-- The sql-query element declares a named SQL query string -->
13981423
<xs:complexType name="sql-query-element" mixed="true">
13991424
<xs:choice minOccurs="0" maxOccurs="unbounded">
1425+
14001426
<xs:element name="return-scalar" type="return-scalar-element"/>
14011427
<xs:element name="return" type="return-element"/>
14021428
<xs:element name="return-join" type="return-join-element"/>

0 commit comments

Comments
 (0)