From 3d237f67bad064df51a72acf3c524bc918599d0d Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Tue, 5 Jan 2016 12:52:20 +0000 Subject: [PATCH] HHH-10405: elements of are not handled properly when building metadata - Refactoring --- .../source/internal/hbm/NamedQueryBinder.java | 33 +++++-------------- .../engine/spi/NamedSQLQueryDefinition.java | 27 ++++++++++++--- 2 files changed, 31 insertions(+), 29 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/boot/model/source/internal/hbm/NamedQueryBinder.java b/hibernate-core/src/main/java/org/hibernate/boot/model/source/internal/hbm/NamedQueryBinder.java index 86ab810fc8..be2bfb56e7 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/model/source/internal/hbm/NamedQueryBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/model/source/internal/hbm/NamedQueryBinder.java @@ -6,14 +6,11 @@ */ package org.hibernate.boot.model.source.internal.hbm; -import java.util.ArrayList; +import javax.xml.bind.JAXBElement; import java.util.HashMap; -import java.util.List; import java.util.Locale; import java.util.Map; -import javax.xml.bind.JAXBElement; - import org.hibernate.MappingException; import org.hibernate.boot.jaxb.hbm.internal.ImplicitResultSetMappingDefinition; import org.hibernate.boot.jaxb.hbm.spi.JaxbHbmNamedNativeQueryType; @@ -170,32 +167,18 @@ public class NamedQueryBinder { final ImplicitResultSetMappingDefinition implicitResultSetMappingDefinition = implicitResultSetMappingBuilder.build(); builder.setResultSetRef( implicitResultSetMappingDefinition.getName() ); - context.getMetadataCollector().addSecondPass( new SecondPass() { @Override public void doSecondPass(Map persistentClasses) throws MappingException { - ResultSetMappingDefinition resultSetMappingDefinition = - ResultSetMappingBinder.bind(implicitResultSetMappingDefinition, context); - context.getMetadataCollector().addResultSetMapping(resultSetMappingDefinition); + final ResultSetMappingDefinition resultSetMappingDefinition = + ResultSetMappingBinder.bind( implicitResultSetMappingDefinition, context ); + context.getMetadataCollector().addResultSetMapping( resultSetMappingDefinition ); NativeSQLQueryReturn[] newQueryReturns = resultSetMappingDefinition.getQueryReturns(); - if (newQueryReturns != null && newQueryReturns.length > 0) { - List queryReturnList = - new ArrayList(); - NamedSQLQueryDefinition queryDefinition = - context.getMetadataCollector().getNamedNativeQueryDefinition(queryName); - NativeSQLQueryReturn[] existingQueryReturns = queryDefinition.getQueryReturns(); - if (existingQueryReturns != null && existingQueryReturns.length > 0) { - for (NativeSQLQueryReturn queryReturn : existingQueryReturns) { - queryReturnList.add(queryReturn); - } - } - for (NativeSQLQueryReturn queryReturn : newQueryReturns) { - queryReturnList.add(queryReturn); - } - NativeSQLQueryReturn[] allQueryReturns = - queryReturnList.toArray(new NativeSQLQueryReturn[queryReturnList.size()]); - queryDefinition.setQueryReturns(allQueryReturns); + final NamedSQLQueryDefinition queryDefinition = + context.getMetadataCollector().getNamedNativeQueryDefinition( queryName ); + if ( queryDefinition != null ) { + queryDefinition.addQueryReturns( newQueryReturns ); } } } diff --git a/hibernate-core/src/main/java/org/hibernate/engine/spi/NamedSQLQueryDefinition.java b/hibernate-core/src/main/java/org/hibernate/engine/spi/NamedSQLQueryDefinition.java index c1d6527993..22fe0d56b8 100755 --- a/hibernate-core/src/main/java/org/hibernate/engine/spi/NamedSQLQueryDefinition.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/spi/NamedSQLQueryDefinition.java @@ -6,6 +6,7 @@ */ package org.hibernate.engine.spi; +import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -222,8 +223,26 @@ public class NamedSQLQueryDefinition extends NamedQueryDefinition { getQueryReturns() ); } - - public void setQueryReturns(NativeSQLQueryReturn[] queryReturns) { - this.queryReturns = queryReturns; - } + + public void addQueryReturns(NativeSQLQueryReturn[] queryReturnsToAdd) { + if ( queryReturnsToAdd != null && queryReturnsToAdd.length > 0 ) { + int initialQueryReturnsLength = 0; + if ( this.queryReturns != null ) { + initialQueryReturnsLength = this.queryReturns.length; + } + NativeSQLQueryReturn[] allQueryReturns = new NativeSQLQueryReturn[initialQueryReturnsLength + queryReturnsToAdd.length]; + + int i = 0; + for ( i = 0; i < initialQueryReturnsLength; i++ ) { + allQueryReturns[i] = this.queryReturns[i]; + } + + for ( int j = 0; j < queryReturnsToAdd.length; j++ ) { + allQueryReturns[i] = queryReturnsToAdd[j]; + i++; + } + + this.queryReturns = allQueryReturns; + } + } }