Search in sources :

Example 1 with NativeSQLQueryRootReturn

use of org.hibernate.engine.query.spi.sql.NativeSQLQueryRootReturn in project hibernate-orm by hibernate.

the class ResultSetMappingBinder method extractReturnDescription.

public static NativeSQLQueryRootReturn extractReturnDescription(JaxbHbmNativeQueryReturnType rtnSource, HbmLocalMetadataBuildingContext context, int queryReturnPosition) {
    String alias = rtnSource.getAlias();
    if (StringHelper.isEmpty(alias)) {
        // hack-around as sqlquery impl depend on having a key.
        alias = "alias_" + queryReturnPosition;
    }
    final String entityName = context.determineEntityName(rtnSource.getEntityName(), rtnSource.getClazz());
    final PersistentClass pc = context.getMetadataCollector().getEntityBinding(entityName);
    return new NativeSQLQueryRootReturn(alias, entityName, extractPropertyResults(alias, rtnSource, pc, context), rtnSource.getLockMode());
}
Also used : NativeSQLQueryRootReturn(org.hibernate.engine.query.spi.sql.NativeSQLQueryRootReturn) PersistentClass(org.hibernate.mapping.PersistentClass)

Example 2 with NativeSQLQueryRootReturn

use of org.hibernate.engine.query.spi.sql.NativeSQLQueryRootReturn 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();
        }
    }
}
Also used : ReaderInputStream(org.hibernate.engine.jdbc.ReaderInputStream) NamedSQLQueryDefinition(org.hibernate.engine.spi.NamedSQLQueryDefinition) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) NativeSQLQueryReturn(org.hibernate.engine.query.spi.sql.NativeSQLQueryReturn) MetadataSources(org.hibernate.boot.MetadataSources) StringReader(java.io.StringReader) Metadata(org.hibernate.boot.Metadata) NativeSQLQueryRootReturn(org.hibernate.engine.query.spi.sql.NativeSQLQueryRootReturn) StandardServiceRegistryImpl(org.hibernate.boot.registry.internal.StandardServiceRegistryImpl) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) Test(org.junit.Test)

Example 3 with NativeSQLQueryRootReturn

use of org.hibernate.engine.query.spi.sql.NativeSQLQueryRootReturn 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);
    }
}
Also used : AssertionFailure(org.hibernate.AssertionFailure) NativeSQLQueryConstructorReturn(org.hibernate.engine.query.spi.sql.NativeSQLQueryConstructorReturn) NativeSQLQueryReturn(org.hibernate.engine.query.spi.sql.NativeSQLQueryReturn) ClassLoadingException(org.hibernate.boot.registry.classloading.spi.ClassLoadingException) NativeSQLQueryRootReturn(org.hibernate.engine.query.spi.sql.NativeSQLQueryRootReturn) ResultSetMappingDefinition(org.hibernate.engine.ResultSetMappingDefinition)

Example 4 with NativeSQLQueryRootReturn

use of org.hibernate.engine.query.spi.sql.NativeSQLQueryRootReturn in project hibernate-orm by hibernate.

the class Util method resolveResultClasses.

/**
 * Resolve the given result classes
 *
 * @param context The context for the resolution.  See {@link ResultSetMappingResolutionContext}
 * @param resultClasses The Classes to which the results should be mapped
 */
public static void resolveResultClasses(ResultClassesResolutionContext context, Class... resultClasses) {
    int i = 0;
    for (Class resultClass : resultClasses) {
        context.addQueryReturns(new NativeSQLQueryRootReturn("alias" + (++i), resultClass.getName(), LockMode.READ));
        try {
            final EntityPersister persister = context.getSessionFactory().getEntityPersister(resultClass.getName());
            context.addQuerySpaces((String[]) persister.getQuerySpaces());
        } catch (Exception ignore) {
        }
    }
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) NativeSQLQueryRootReturn(org.hibernate.engine.query.spi.sql.NativeSQLQueryRootReturn) UnknownSqlResultSetMappingException(org.hibernate.procedure.UnknownSqlResultSetMappingException)

Example 5 with NativeSQLQueryRootReturn

use of org.hibernate.engine.query.spi.sql.NativeSQLQueryRootReturn 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;
}
Also used : DefaultEntityAliases(org.hibernate.loader.DefaultEntityAliases) EntityFetchReturn(org.hibernate.loader.custom.EntityFetchReturn) HashMap(java.util.HashMap) NativeSQLQueryReturn(org.hibernate.engine.query.spi.sql.NativeSQLQueryReturn) NativeSQLQueryCollectionReturn(org.hibernate.engine.query.spi.sql.NativeSQLQueryCollectionReturn) CollectionReturn(org.hibernate.loader.custom.CollectionReturn) CollectionFetchReturn(org.hibernate.loader.custom.CollectionFetchReturn) ArrayList(java.util.ArrayList) NativeSQLQueryRootReturn(org.hibernate.engine.query.spi.sql.NativeSQLQueryRootReturn) RootReturn(org.hibernate.loader.custom.RootReturn) NativeSQLQueryConstructorReturn(org.hibernate.engine.query.spi.sql.NativeSQLQueryConstructorReturn) ConstructorReturn(org.hibernate.loader.custom.ConstructorReturn) NativeSQLQueryRootReturn(org.hibernate.engine.query.spi.sql.NativeSQLQueryRootReturn) NonScalarReturn(org.hibernate.loader.custom.NonScalarReturn) NativeSQLQueryNonScalarReturn(org.hibernate.engine.query.spi.sql.NativeSQLQueryNonScalarReturn) NativeSQLQueryConstructorReturn(org.hibernate.engine.query.spi.sql.NativeSQLQueryConstructorReturn) NonScalarReturn(org.hibernate.loader.custom.NonScalarReturn) NativeSQLQueryScalarReturn(org.hibernate.engine.query.spi.sql.NativeSQLQueryScalarReturn) ScalarReturn(org.hibernate.loader.custom.ScalarReturn) NativeSQLQueryNonScalarReturn(org.hibernate.engine.query.spi.sql.NativeSQLQueryNonScalarReturn) SQLLoadable(org.hibernate.persister.entity.SQLLoadable) NativeSQLQueryJoinReturn(org.hibernate.engine.query.spi.sql.NativeSQLQueryJoinReturn) Return(org.hibernate.loader.custom.Return) NonScalarReturn(org.hibernate.loader.custom.NonScalarReturn) EntityFetchReturn(org.hibernate.loader.custom.EntityFetchReturn) NativeSQLQueryJoinReturn(org.hibernate.engine.query.spi.sql.NativeSQLQueryJoinReturn) NativeSQLQueryRootReturn(org.hibernate.engine.query.spi.sql.NativeSQLQueryRootReturn) CollectionFetchReturn(org.hibernate.loader.custom.CollectionFetchReturn) NativeSQLQueryReturn(org.hibernate.engine.query.spi.sql.NativeSQLQueryReturn) NativeSQLQueryConstructorReturn(org.hibernate.engine.query.spi.sql.NativeSQLQueryConstructorReturn) ConstructorReturn(org.hibernate.loader.custom.ConstructorReturn) NativeSQLQueryCollectionReturn(org.hibernate.engine.query.spi.sql.NativeSQLQueryCollectionReturn) CollectionReturn(org.hibernate.loader.custom.CollectionReturn) RootReturn(org.hibernate.loader.custom.RootReturn) NativeSQLQueryScalarReturn(org.hibernate.engine.query.spi.sql.NativeSQLQueryScalarReturn) ScalarReturn(org.hibernate.loader.custom.ScalarReturn) NativeSQLQueryNonScalarReturn(org.hibernate.engine.query.spi.sql.NativeSQLQueryNonScalarReturn) FetchReturn(org.hibernate.loader.custom.FetchReturn) NativeSQLQueryCollectionReturn(org.hibernate.engine.query.spi.sql.NativeSQLQueryCollectionReturn) ColumnCollectionAliases(org.hibernate.loader.custom.ColumnCollectionAliases) GeneratedCollectionAliases(org.hibernate.loader.GeneratedCollectionAliases) SQLLoadableCollection(org.hibernate.persister.collection.SQLLoadableCollection) ColumnEntityAliases(org.hibernate.loader.ColumnEntityAliases) EntityAliases(org.hibernate.loader.EntityAliases) DefaultEntityAliases(org.hibernate.loader.DefaultEntityAliases) ColumnEntityAliases(org.hibernate.loader.ColumnEntityAliases) HashMap(java.util.HashMap) Map(java.util.Map) GeneratedCollectionAliases(org.hibernate.loader.GeneratedCollectionAliases) CollectionAliases(org.hibernate.loader.CollectionAliases) ColumnCollectionAliases(org.hibernate.loader.custom.ColumnCollectionAliases) EntityFetchReturn(org.hibernate.loader.custom.EntityFetchReturn) CollectionFetchReturn(org.hibernate.loader.custom.CollectionFetchReturn) FetchReturn(org.hibernate.loader.custom.FetchReturn) NativeSQLQueryScalarReturn(org.hibernate.engine.query.spi.sql.NativeSQLQueryScalarReturn)

Aggregations

NativeSQLQueryRootReturn (org.hibernate.engine.query.spi.sql.NativeSQLQueryRootReturn)11 HashMap (java.util.HashMap)5 NativeSQLQueryReturn (org.hibernate.engine.query.spi.sql.NativeSQLQueryReturn)5 NativeSQLQueryConstructorReturn (org.hibernate.engine.query.spi.sql.NativeSQLQueryConstructorReturn)4 NativeSQLQueryScalarReturn (org.hibernate.engine.query.spi.sql.NativeSQLQueryScalarReturn)4 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 NativeSQLQueryCollectionReturn (org.hibernate.engine.query.spi.sql.NativeSQLQueryCollectionReturn)3 NativeSQLQueryJoinReturn (org.hibernate.engine.query.spi.sql.NativeSQLQueryJoinReturn)3 NamedSQLQueryDefinition (org.hibernate.engine.spi.NamedSQLQueryDefinition)3 Test (org.junit.Test)3 AnnotationException (org.hibernate.AnnotationException)2 ResultSetMappingDefinition (org.hibernate.engine.ResultSetMappingDefinition)2 NativeSQLQueryNonScalarReturn (org.hibernate.engine.query.spi.sql.NativeSQLQueryNonScalarReturn)2 NamedSQLQueryDefinitionBuilder (org.hibernate.engine.spi.NamedSQLQueryDefinitionBuilder)2 ColumnEntityAliases (org.hibernate.loader.ColumnEntityAliases)2 CollectionFetchReturn (org.hibernate.loader.custom.CollectionFetchReturn)2 CollectionReturn (org.hibernate.loader.custom.CollectionReturn)2 ConstructorReturn (org.hibernate.loader.custom.ConstructorReturn)2 EntityFetchReturn (org.hibernate.loader.custom.EntityFetchReturn)2