Search in sources :

Example 1 with NativeSQLQueryCollectionReturn

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

Example 2 with NativeSQLQueryCollectionReturn

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);
}
Also used : NativeSQLQueryCollectionReturn(org.hibernate.engine.query.spi.sql.NativeSQLQueryCollectionReturn) NativeSQLQueryRootReturn(org.hibernate.engine.query.spi.sql.NativeSQLQueryRootReturn) NativeSQLQueryJoinReturn(org.hibernate.engine.query.spi.sql.NativeSQLQueryJoinReturn) NativeSQLQueryScalarReturn(org.hibernate.engine.query.spi.sql.NativeSQLQueryScalarReturn) Test(org.junit.Test)

Example 3 with NativeSQLQueryCollectionReturn

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));
}
Also used : NativeSQLQueryCollectionReturn(org.hibernate.engine.query.spi.sql.NativeSQLQueryCollectionReturn) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 4 with NativeSQLQueryCollectionReturn

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;
}
Also used : 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) 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) NativeSQLQueryConstructorReturn(org.hibernate.engine.query.spi.sql.NativeSQLQueryConstructorReturn) ColumnEntityAliases(org.hibernate.loader.ColumnEntityAliases) 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) HashMap(java.util.HashMap) Map(java.util.Map) NativeSQLQueryJoinReturn(org.hibernate.engine.query.spi.sql.NativeSQLQueryJoinReturn) NativeSQLQueryScalarReturn(org.hibernate.engine.query.spi.sql.NativeSQLQueryScalarReturn)

Aggregations

NativeSQLQueryCollectionReturn (org.hibernate.engine.query.spi.sql.NativeSQLQueryCollectionReturn)4 HashMap (java.util.HashMap)3 NativeSQLQueryJoinReturn (org.hibernate.engine.query.spi.sql.NativeSQLQueryJoinReturn)3 NativeSQLQueryRootReturn (org.hibernate.engine.query.spi.sql.NativeSQLQueryRootReturn)3 NativeSQLQueryScalarReturn (org.hibernate.engine.query.spi.sql.NativeSQLQueryScalarReturn)3 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 NativeSQLQueryConstructorReturn (org.hibernate.engine.query.spi.sql.NativeSQLQueryConstructorReturn)2 NativeSQLQueryNonScalarReturn (org.hibernate.engine.query.spi.sql.NativeSQLQueryNonScalarReturn)2 NativeSQLQueryReturn (org.hibernate.engine.query.spi.sql.NativeSQLQueryReturn)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 FetchReturn (org.hibernate.loader.custom.FetchReturn)2 NonScalarReturn (org.hibernate.loader.custom.NonScalarReturn)2 Return (org.hibernate.loader.custom.Return)2 RootReturn (org.hibernate.loader.custom.RootReturn)2 ScalarReturn (org.hibernate.loader.custom.ScalarReturn)2