use of org.hibernate.engine.query.spi.sql.NativeSQLQueryReturn in project hibernate-orm by hibernate.
the class QueryReturnTest method testQueryReturn.
@Test
public void testQueryReturn() {
StandardServiceRegistryBuilder serviceRegistryBuilder = new StandardServiceRegistryBuilder().applySetting("hibernate.dialect", "org.hibernate.dialect.HSQLDialect");
StandardServiceRegistry standardServiceRegistry = serviceRegistryBuilder.build();
MetadataSources metadataSources = new MetadataSources(standardServiceRegistry);
try {
metadataSources.addInputStream(new ReaderInputStream(new StringReader(QUERY_RETURN_HBM_XML)));
Metadata metadata = metadataSources.buildMetadata();
NamedSQLQueryDefinition myQuery = metadata.getNamedNativeQueryDefinition("myQuery");
Assert.assertNotNull(myQuery);
NativeSQLQueryReturn[] myQueryReturns = myQuery.getQueryReturns();
Assert.assertNotNull(myQueryReturns);
Assert.assertEquals(1, myQueryReturns.length);
Assert.assertTrue(NativeSQLQueryRootReturn.class.isInstance(myQueryReturns[0]));
NativeSQLQueryRootReturn myQueryRootReturn = (NativeSQLQueryRootReturn) myQueryReturns[0];
Assert.assertEquals("e", myQueryRootReturn.getAlias());
Assert.assertEquals("org.hibernate.test.hbm.query.QueryReturnTest$Bar", myQueryRootReturn.getReturnEntityName());
} finally {
if (standardServiceRegistry instanceof StandardServiceRegistryImpl) {
((StandardServiceRegistryImpl) standardServiceRegistry).destroy();
}
}
}
use of org.hibernate.engine.query.spi.sql.NativeSQLQueryReturn in project hibernate-orm by hibernate.
the class ResultSetMappingDefinition method traceLoggableFormat.
public String traceLoggableFormat() {
final StringBuilder buffer = new StringBuilder().append("ResultSetMappingDefinition[\n").append(" name=").append(name).append("\n").append(" returns=[\n");
for (NativeSQLQueryReturn rtn : queryReturns) {
rtn.traceLog(new NativeSQLQueryReturn.TraceLogger() {
@Override
public void writeLine(String traceLine) {
buffer.append(" ").append(traceLine).append("\n");
}
});
}
buffer.append(" ]\n").append("]");
return buffer.toString();
}
use of org.hibernate.engine.query.spi.sql.NativeSQLQueryReturn in project hibernate-orm by hibernate.
the class AbstractSharedSessionContract method resultClassChecking.
@SuppressWarnings({ "unchecked", "WeakerAccess" })
protected void resultClassChecking(Class resultType, NamedSQLQueryDefinition namedQueryDefinition) {
final NativeSQLQueryReturn[] queryReturns;
if (namedQueryDefinition.getQueryReturns() != null) {
queryReturns = namedQueryDefinition.getQueryReturns();
} else if (namedQueryDefinition.getResultSetRef() != null) {
final ResultSetMappingDefinition rsMapping = getFactory().getNamedQueryRepository().getResultSetMappingDefinition(namedQueryDefinition.getResultSetRef());
queryReturns = rsMapping.getQueryReturns();
} else {
throw new AssertionFailure("Unsupported named query model. Please report the bug in Hibernate EntityManager");
}
if (queryReturns.length > 1) {
throw new IllegalArgumentException("Cannot create TypedQuery for query with more than one return");
}
final NativeSQLQueryReturn nativeSQLQueryReturn = queryReturns[0];
if (nativeSQLQueryReturn instanceof NativeSQLQueryRootReturn) {
final Class<?> actualReturnedClass;
final String entityClassName = ((NativeSQLQueryRootReturn) nativeSQLQueryReturn).getReturnEntityName();
try {
actualReturnedClass = getFactory().getServiceRegistry().getService(ClassLoaderService.class).classForName(entityClassName);
} catch (ClassLoadingException e) {
throw new AssertionFailure("Unable to load class [" + entityClassName + "] declared on named native query [" + namedQueryDefinition.getName() + "]");
}
if (!resultType.isAssignableFrom(actualReturnedClass)) {
throw buildIncompatibleException(resultType, actualReturnedClass);
}
} else if (nativeSQLQueryReturn instanceof NativeSQLQueryConstructorReturn) {
final NativeSQLQueryConstructorReturn ctorRtn = (NativeSQLQueryConstructorReturn) nativeSQLQueryReturn;
if (!resultType.isAssignableFrom(ctorRtn.getTargetClass())) {
throw buildIncompatibleException(resultType, ctorRtn.getTargetClass());
}
} else {
log.debugf("Skiping unhandled NativeSQLQueryReturn type : " + nativeSQLQueryReturn);
}
}
use of org.hibernate.engine.query.spi.sql.NativeSQLQueryReturn in project hibernate-orm by hibernate.
the class NativeQueryImpl method beforeQuery.
@Override
protected void beforeQuery() {
prepareQueryReturnsIfNecessary();
boolean noReturns = queryReturns == null || queryReturns.isEmpty();
if (noReturns) {
this.autoDiscoverTypes = true;
} else {
for (NativeSQLQueryReturn queryReturn : queryReturns) {
if (queryReturn instanceof NativeSQLQueryScalarReturn) {
NativeSQLQueryScalarReturn scalar = (NativeSQLQueryScalarReturn) queryReturn;
if (scalar.getType() == null) {
autoDiscoverTypes = true;
break;
}
} else if (NativeSQLQueryConstructorReturn.class.isInstance(queryReturn)) {
autoDiscoverTypes = true;
break;
}
}
}
super.beforeQuery();
if (getSynchronizedQuerySpaces() != null && !getSynchronizedQuerySpaces().isEmpty()) {
// perform a partial flush according to the defined query spaces, no need to do a full flush.
return;
}
// NOTE : this was added for JPA initially. Perhaps we want to only do this from JPA usage?
if (shouldFlush()) {
getProducer().flush();
}
}
use of org.hibernate.engine.query.spi.sql.NativeSQLQueryReturn 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