use of org.hibernate.engine.query.spi.sql.NativeSQLQueryCollectionReturn in project hibernate-orm by hibernate.
the class SQLQueryReturnProcessor method generateCustomReturns.
public List<Return> generateCustomReturns(boolean queryHadAliases) {
List<Return> customReturns = new ArrayList<Return>();
Map<String, Return> customReturnsByAlias = new HashMap<String, Return>();
for (NativeSQLQueryReturn queryReturn : queryReturns) {
if (queryReturn instanceof NativeSQLQueryScalarReturn) {
NativeSQLQueryScalarReturn rtn = (NativeSQLQueryScalarReturn) queryReturn;
customReturns.add(new ScalarReturn(rtn.getType(), rtn.getColumnAlias()));
} else if (queryReturn instanceof NativeSQLQueryRootReturn) {
NativeSQLQueryRootReturn rtn = (NativeSQLQueryRootReturn) queryReturn;
String alias = rtn.getAlias();
EntityAliases entityAliases;
if (queryHadAliases || hasPropertyResultMap(alias)) {
entityAliases = new DefaultEntityAliases((Map) entityPropertyResultMaps.get(alias), (SQLLoadable) alias2Persister.get(alias), (String) alias2Suffix.get(alias));
} else {
entityAliases = new ColumnEntityAliases((Map) entityPropertyResultMaps.get(alias), (SQLLoadable) alias2Persister.get(alias), (String) alias2Suffix.get(alias));
}
RootReturn customReturn = new RootReturn(alias, rtn.getReturnEntityName(), entityAliases, rtn.getLockMode());
customReturns.add(customReturn);
customReturnsByAlias.put(rtn.getAlias(), customReturn);
} else if (queryReturn instanceof NativeSQLQueryCollectionReturn) {
NativeSQLQueryCollectionReturn rtn = (NativeSQLQueryCollectionReturn) queryReturn;
String alias = rtn.getAlias();
SQLLoadableCollection persister = (SQLLoadableCollection) alias2CollectionPersister.get(alias);
boolean isEntityElements = persister.getElementType().isEntityType();
CollectionAliases collectionAliases;
EntityAliases elementEntityAliases = null;
if (queryHadAliases || hasPropertyResultMap(alias)) {
collectionAliases = new GeneratedCollectionAliases((Map) collectionPropertyResultMaps.get(alias), (SQLLoadableCollection) alias2CollectionPersister.get(alias), (String) alias2CollectionSuffix.get(alias));
if (isEntityElements) {
elementEntityAliases = new DefaultEntityAliases((Map) entityPropertyResultMaps.get(alias), (SQLLoadable) alias2Persister.get(alias), (String) alias2Suffix.get(alias));
}
} else {
collectionAliases = new ColumnCollectionAliases((Map) collectionPropertyResultMaps.get(alias), (SQLLoadableCollection) alias2CollectionPersister.get(alias));
if (isEntityElements) {
elementEntityAliases = new ColumnEntityAliases((Map) entityPropertyResultMaps.get(alias), (SQLLoadable) alias2Persister.get(alias), (String) alias2Suffix.get(alias));
}
}
CollectionReturn customReturn = new CollectionReturn(alias, rtn.getOwnerEntityName(), rtn.getOwnerProperty(), collectionAliases, elementEntityAliases, rtn.getLockMode());
customReturns.add(customReturn);
customReturnsByAlias.put(rtn.getAlias(), customReturn);
} else if (queryReturn instanceof NativeSQLQueryJoinReturn) {
NativeSQLQueryJoinReturn rtn = (NativeSQLQueryJoinReturn) queryReturn;
String alias = rtn.getAlias();
FetchReturn customReturn;
NonScalarReturn ownerCustomReturn = (NonScalarReturn) customReturnsByAlias.get(rtn.getOwnerAlias());
if (alias2CollectionPersister.containsKey(alias)) {
SQLLoadableCollection persister = (SQLLoadableCollection) alias2CollectionPersister.get(alias);
boolean isEntityElements = persister.getElementType().isEntityType();
CollectionAliases collectionAliases;
EntityAliases elementEntityAliases = null;
if (queryHadAliases || hasPropertyResultMap(alias)) {
collectionAliases = new GeneratedCollectionAliases((Map) collectionPropertyResultMaps.get(alias), persister, (String) alias2CollectionSuffix.get(alias));
if (isEntityElements) {
elementEntityAliases = new DefaultEntityAliases((Map) entityPropertyResultMaps.get(alias), (SQLLoadable) alias2Persister.get(alias), (String) alias2Suffix.get(alias));
}
} else {
collectionAliases = new ColumnCollectionAliases((Map) collectionPropertyResultMaps.get(alias), persister);
if (isEntityElements) {
elementEntityAliases = new ColumnEntityAliases((Map) entityPropertyResultMaps.get(alias), (SQLLoadable) alias2Persister.get(alias), (String) alias2Suffix.get(alias));
}
}
customReturn = new CollectionFetchReturn(alias, ownerCustomReturn, rtn.getOwnerProperty(), collectionAliases, elementEntityAliases, rtn.getLockMode());
} else {
EntityAliases entityAliases;
if (queryHadAliases || hasPropertyResultMap(alias)) {
entityAliases = new DefaultEntityAliases((Map) entityPropertyResultMaps.get(alias), (SQLLoadable) alias2Persister.get(alias), (String) alias2Suffix.get(alias));
} else {
entityAliases = new ColumnEntityAliases((Map) entityPropertyResultMaps.get(alias), (SQLLoadable) alias2Persister.get(alias), (String) alias2Suffix.get(alias));
}
customReturn = new EntityFetchReturn(alias, entityAliases, ownerCustomReturn, rtn.getOwnerProperty(), rtn.getLockMode());
}
customReturns.add(customReturn);
customReturnsByAlias.put(alias, customReturn);
} else if (NativeSQLQueryConstructorReturn.class.isInstance(queryReturn)) {
final NativeSQLQueryConstructorReturn constructorReturn = (NativeSQLQueryConstructorReturn) queryReturn;
final ScalarReturn[] scalars = new ScalarReturn[constructorReturn.getColumnReturns().length];
int i = 0;
for (NativeSQLQueryScalarReturn scalarReturn : constructorReturn.getColumnReturns()) {
scalars[i++] = new ScalarReturn(scalarReturn.getType(), scalarReturn.getColumnAlias());
}
customReturns.add(new ConstructorReturn(constructorReturn.getTargetClass(), scalars));
} else {
throw new IllegalStateException("Unrecognized NativeSQLQueryReturn concrete type : " + queryReturn);
}
}
return customReturns;
}
use of org.hibernate.engine.query.spi.sql.NativeSQLQueryCollectionReturn in project hibernate-orm by hibernate.
the class NativeSQLQueryReturnEqualsAndHashCodeTest method testNativeSQLQueryReturnTypes.
@Test
public void testNativeSQLQueryReturnTypes() {
NativeSQLQueryScalarReturn r1 = new NativeSQLQueryScalarReturn("a", sessionFactory().getTypeResolver().basic("int"));
NativeSQLQueryRootReturn r2 = new NativeSQLQueryRootReturn("a", "b", LockMode.NONE);
NativeSQLQueryJoinReturn r3 = new NativeSQLQueryJoinReturn("a", "b", "c", Collections.singletonMap("key", "value"), LockMode.NONE);
NativeSQLQueryCollectionReturn r4 = new NativeSQLQueryCollectionReturn("a", "b", "c", Collections.singletonMap("key", "value"), LockMode.NONE);
check(false, r1, r2);
check(false, r1, r3);
check(false, r1, r4);
check(false, r2, r3);
check(false, r2, r4);
check(false, r3, r4);
}
use of org.hibernate.engine.query.spi.sql.NativeSQLQueryCollectionReturn in project hibernate-orm by hibernate.
the class NativeSQLQueryReturnEqualsAndHashCodeTest method testNativeSQLQueryCollectionReturn.
@Test
public void testNativeSQLQueryCollectionReturn() {
NativeSQLQueryCollectionReturn r1 = new NativeSQLQueryCollectionReturn("a", "b", "c", null, null);
NativeSQLQueryCollectionReturn r2 = new NativeSQLQueryCollectionReturn("a", "c", "b", null, null);
NativeSQLQueryCollectionReturn r3NullMap = new NativeSQLQueryCollectionReturn("b", "c", "a", null, null);
NativeSQLQueryCollectionReturn r3EmptyMap = new NativeSQLQueryCollectionReturn("b", "c", "a", new HashMap(), null);
NativeSQLQueryCollectionReturn r4 = new NativeSQLQueryCollectionReturn("b", "c", "a", Collections.singletonMap("key", "value"), null);
NativeSQLQueryCollectionReturn r5 = new NativeSQLQueryCollectionReturn("b", "c", "a", Collections.singletonMap("otherkey", "othervalue"), null);
NativeSQLQueryCollectionReturn r6 = new NativeSQLQueryCollectionReturn("b", "c", "a", Collections.singletonMap("key", "value"), LockMode.NONE);
NativeSQLQueryCollectionReturn r7 = new NativeSQLQueryCollectionReturn("b", "c", "a", null, LockMode.NONE);
check(false, r1, r2);
check(false, r1, r3NullMap);
check(false, r1, r3EmptyMap);
check(false, r1, r4);
check(false, r1, r5);
check(false, r1, r6);
check(false, r1, r7);
check(false, r2, r3NullMap);
check(false, r2, r3EmptyMap);
check(false, r2, r4);
check(false, r2, r5);
check(false, r2, r6);
check(false, r2, r7);
check(true, r3NullMap, r3EmptyMap);
check(false, r3NullMap, r4);
check(false, r3NullMap, r5);
check(false, r3NullMap, r6);
check(false, r3NullMap, r7);
check(false, r3EmptyMap, r4);
check(false, r3EmptyMap, r5);
check(false, r3EmptyMap, r6);
check(false, r3EmptyMap, r7);
check(false, r4, r5);
check(false, r4, r6);
check(false, r4, r7);
check(false, r5, r6);
check(false, r5, r7);
check(false, r6, r7);
check(true, r1, new NativeSQLQueryCollectionReturn("a", "b", "c", null, null));
check(true, r2, new NativeSQLQueryCollectionReturn("a", "c", "b", null, null));
check(true, r3NullMap, new NativeSQLQueryCollectionReturn("b", "c", "a", null, null));
check(true, r3EmptyMap, new NativeSQLQueryCollectionReturn("b", "c", "a", new HashMap(), null));
check(true, r4, new NativeSQLQueryCollectionReturn("b", "c", "a", Collections.singletonMap("key", "value"), null));
check(true, r5, new NativeSQLQueryCollectionReturn("b", "c", "a", Collections.singletonMap("otherkey", "othervalue"), null));
check(true, r6, new NativeSQLQueryCollectionReturn("b", "c", "a", Collections.singletonMap("key", "value"), LockMode.NONE));
check(true, r7, new NativeSQLQueryCollectionReturn("b", "c", "a", null, LockMode.NONE));
}
use of org.hibernate.engine.query.spi.sql.NativeSQLQueryCollectionReturn in project hibernate-orm by hibernate.
the class SQLQueryReturnProcessor method generateCallableReturns.
public List<Return> generateCallableReturns() {
final List<Return> customReturns = new ArrayList<>();
visitReturns(new QueryReturnVisitor() {
@Override
public void visitScalarReturn(NativeSQLQueryScalarReturn rtn) {
customReturns.add(new ScalarReturn(rtn.getType(), rtn.getColumnAlias()));
}
@Override
public void visitRootReturn(NativeSQLQueryRootReturn rtn) {
customReturns.add(new RootReturn(rtn.getAlias(), rtn.getReturnEntityName(), new ColumnEntityAliases((Map) entityPropertyResultMaps.get(rtn.getAlias()), (SQLLoadable) alias2Persister.get(rtn.getAlias()), (String) alias2Suffix.get(rtn.getAlias())), rtn.getLockMode()));
}
@Override
public void visitCollectionReturn(NativeSQLQueryCollectionReturn rtn) {
throw new UnsupportedOperationException("Collection returns not supported for stored procedure mapping");
}
@Override
public void visitFetch(NativeSQLQueryJoinReturn rtn) {
throw new UnsupportedOperationException("Collection returns not supported for stored procedure mapping");
}
@Override
public void visitDynamicInstantiation(NativeSQLQueryConstructorReturn rtn) {
final ScalarReturn[] scalars = new ScalarReturn[rtn.getColumnReturns().length];
int i = 0;
for (NativeSQLQueryScalarReturn scalarReturn : rtn.getColumnReturns()) {
scalars[i++] = new ScalarReturn(scalarReturn.getType(), scalarReturn.getColumnAlias());
}
customReturns.add(new ConstructorReturn(rtn.getTargetClass(), scalars));
}
});
return customReturns;
}
Aggregations