use of org.hibernate.persister.collection.SQLLoadableCollection in project hibernate-orm by hibernate.
the class SQLQueryParser method resolveCollectionProperties.
private String resolveCollectionProperties(String aliasName, String propertyName) {
Map fieldResults = context.getPropertyResultsMapByAlias(aliasName);
SQLLoadableCollection collectionPersister = context.getCollectionPersisterByAlias(aliasName);
String collectionSuffix = context.getCollectionSuffixByAlias(aliasName);
if ("*".equals(propertyName)) {
if (!fieldResults.isEmpty()) {
throw new QueryException("Using return-propertys together with * syntax is not supported.");
}
String selectFragment = collectionPersister.selectFragment(aliasName, collectionSuffix);
aliasesFound++;
return selectFragment + ", " + resolveProperties(aliasName, propertyName);
} else if ("element.*".equals(propertyName)) {
return resolveProperties(aliasName, "*");
} else {
String[] columnAliases;
// Let return-propertys override whatever the persister has for aliases.
columnAliases = (String[]) fieldResults.get(propertyName);
if (columnAliases == null) {
columnAliases = collectionPersister.getCollectionPropertyColumnAliases(propertyName, collectionSuffix);
}
if (columnAliases == null || columnAliases.length == 0) {
throw new QueryException("No column name found for property [" + propertyName + "] for alias [" + aliasName + "]", originalQueryString);
}
if (columnAliases.length != 1) {
// TODO: better error message since we actually support composites if names are explicitly listed.
throw new QueryException("SQL queries only support properties mapped to a single column - property [" + propertyName + "] is mapped to " + columnAliases.length + " columns.", originalQueryString);
}
aliasesFound++;
return columnAliases[0];
}
}
use of org.hibernate.persister.collection.SQLLoadableCollection in project hibernate-orm by hibernate.
the class SQLQueryReturnProcessor method addCollection.
private void addCollection(String role, String alias, Map propertyResults) {
SQLLoadableCollection collectionPersister = (SQLLoadableCollection) factory.getCollectionPersister(role);
alias2CollectionPersister.put(alias, collectionPersister);
String suffix = generateCollectionSuffix();
LOG.tracev("Mapping alias [{0}] to collection-suffix [{1}]", alias, suffix);
alias2CollectionSuffix.put(alias, suffix);
collectionPropertyResultMaps.put(alias, propertyResults);
if (collectionPersister.isOneToMany() || collectionPersister.isManyToMany()) {
SQLLoadable persister = (SQLLoadable) collectionPersister.getElementPersister();
addPersister(alias, filter(propertyResults), persister);
}
}
use of org.hibernate.persister.collection.SQLLoadableCollection 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;
}
Aggregations