HHH-10405: <return> elements of <sql-query> are not handled properly when building metadata - Refactoring

This commit is contained in:
Andrea Boriero 2016-01-05 12:52:20 +00:00
parent 9e89826f8f
commit 3d237f67ba
2 changed files with 31 additions and 29 deletions

View File

@ -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<NativeSQLQueryReturn> queryReturnList =
new ArrayList<NativeSQLQueryReturn>();
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 );
}
}
}

View File

@ -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;
}
}
}