Search in sources :

Example 1 with SQLLoadable

use of org.hibernate.persister.entity.SQLLoadable in project hibernate-orm by hibernate.

the class SQLQueryReturnProcessor method processJoinReturn.

private void processJoinReturn(NativeSQLQueryJoinReturn fetchReturn) {
    String alias = fetchReturn.getAlias();
    //		if ( alias2Persister.containsKey( alias ) || collectionAliases.contains( alias ) ) {
    if (alias2Persister.containsKey(alias) || alias2CollectionPersister.containsKey(alias)) {
        // already been processed...
        return;
    }
    String ownerAlias = fetchReturn.getOwnerAlias();
    // Make sure the owner alias is known...
    if (!alias2Return.containsKey(ownerAlias)) {
        throw new HibernateException("Owner alias [" + ownerAlias + "] is unknown for alias [" + alias + "]");
    }
    // If this return's alias has not been processed yet, do so b4 further processing of this return
    if (!alias2Persister.containsKey(ownerAlias)) {
        NativeSQLQueryNonScalarReturn ownerReturn = (NativeSQLQueryNonScalarReturn) alias2Return.get(ownerAlias);
        processReturn(ownerReturn);
    }
    SQLLoadable ownerPersister = (SQLLoadable) alias2Persister.get(ownerAlias);
    Type returnType = ownerPersister.getPropertyType(fetchReturn.getOwnerProperty());
    if (returnType.isCollectionType()) {
        String role = ownerPersister.getEntityName() + '.' + fetchReturn.getOwnerProperty();
        addCollection(role, alias, fetchReturn.getPropertyResultsMap());
    //			collectionOwnerAliases.add( ownerAlias );
    } else if (returnType.isEntityType()) {
        EntityType eType = (EntityType) returnType;
        String returnEntityName = eType.getAssociatedEntityName();
        SQLLoadable persister = getSQLLoadable(returnEntityName);
        addPersister(alias, fetchReturn.getPropertyResultsMap(), persister);
    }
}
Also used : EntityType(org.hibernate.type.EntityType) NativeSQLQueryNonScalarReturn(org.hibernate.engine.query.spi.sql.NativeSQLQueryNonScalarReturn) EntityType(org.hibernate.type.EntityType) Type(org.hibernate.type.Type) HibernateException(org.hibernate.HibernateException) SQLLoadable(org.hibernate.persister.entity.SQLLoadable)

Example 2 with SQLLoadable

use of org.hibernate.persister.entity.SQLLoadable 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);
    }
}
Also used : SQLLoadableCollection(org.hibernate.persister.collection.SQLLoadableCollection) SQLLoadable(org.hibernate.persister.entity.SQLLoadable)

Example 3 with SQLLoadable

use of org.hibernate.persister.entity.SQLLoadable in project hibernate-orm by hibernate.

the class SQLQueryParser method resolveProperties.

private String resolveProperties(String aliasName, String propertyName) {
    Map fieldResults = context.getPropertyResultsMapByAlias(aliasName);
    SQLLoadable persister = context.getEntityPersisterByAlias(aliasName);
    String suffix = context.getEntitySuffixByAlias(aliasName);
    if ("*".equals(propertyName)) {
        if (!fieldResults.isEmpty()) {
            throw new QueryException("Using return-propertys together with * syntax is not supported.");
        }
        aliasesFound++;
        return persister.selectFragment(aliasName, suffix);
    } else {
        String[] columnAliases;
        // Let return-propertys override whatever the persister has for aliases.
        columnAliases = (String[]) fieldResults.get(propertyName);
        if (columnAliases == null) {
            columnAliases = persister.getSubclassPropertyColumnAliases(propertyName, suffix);
        }
        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];
    }
}
Also used : QueryException(org.hibernate.QueryException) SQLLoadable(org.hibernate.persister.entity.SQLLoadable) Map(java.util.Map) HashMap(java.util.HashMap)

Example 4 with SQLLoadable

use of org.hibernate.persister.entity.SQLLoadable 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 5 with SQLLoadable

use of org.hibernate.persister.entity.SQLLoadable in project hibernate-orm by hibernate.

the class SQLQueryReturnProcessor method processRootReturn.

private void processRootReturn(NativeSQLQueryRootReturn rootReturn) {
    if (alias2Persister.containsKey(rootReturn.getAlias())) {
        // already been processed...
        return;
    }
    SQLLoadable persister = getSQLLoadable(rootReturn.getReturnEntityName());
    addPersister(rootReturn.getAlias(), rootReturn.getPropertyResultsMap(), persister);
}
Also used : SQLLoadable(org.hibernate.persister.entity.SQLLoadable)

Aggregations

SQLLoadable (org.hibernate.persister.entity.SQLLoadable)5 HashMap (java.util.HashMap)2 Map (java.util.Map)2 NativeSQLQueryNonScalarReturn (org.hibernate.engine.query.spi.sql.NativeSQLQueryNonScalarReturn)2 SQLLoadableCollection (org.hibernate.persister.collection.SQLLoadableCollection)2 ArrayList (java.util.ArrayList)1 HibernateException (org.hibernate.HibernateException)1 QueryException (org.hibernate.QueryException)1 NativeSQLQueryCollectionReturn (org.hibernate.engine.query.spi.sql.NativeSQLQueryCollectionReturn)1 NativeSQLQueryConstructorReturn (org.hibernate.engine.query.spi.sql.NativeSQLQueryConstructorReturn)1 NativeSQLQueryJoinReturn (org.hibernate.engine.query.spi.sql.NativeSQLQueryJoinReturn)1 NativeSQLQueryReturn (org.hibernate.engine.query.spi.sql.NativeSQLQueryReturn)1 NativeSQLQueryRootReturn (org.hibernate.engine.query.spi.sql.NativeSQLQueryRootReturn)1 NativeSQLQueryScalarReturn (org.hibernate.engine.query.spi.sql.NativeSQLQueryScalarReturn)1 CollectionAliases (org.hibernate.loader.CollectionAliases)1 ColumnEntityAliases (org.hibernate.loader.ColumnEntityAliases)1 DefaultEntityAliases (org.hibernate.loader.DefaultEntityAliases)1 EntityAliases (org.hibernate.loader.EntityAliases)1 GeneratedCollectionAliases (org.hibernate.loader.GeneratedCollectionAliases)1 CollectionFetchReturn (org.hibernate.loader.custom.CollectionFetchReturn)1