Example usage for org.hibernate.persister.collection QueryableCollection getElementType

List of usage examples for org.hibernate.persister.collection QueryableCollection getElementType

Introduction

In this page you can find the example usage for org.hibernate.persister.collection QueryableCollection getElementType.

Prototype

Type getElementType();

Source Link

Document

Get the "element" type

Usage

From source file:com.blazebit.persistence.integration.hibernate.base.HibernateJpaProvider.java

License:Apache License

@Override
public String[] getColumnNames(EntityType<?> ownerType, String elementCollectionPath, String attributeName) {
    QueryableCollection persister = getCollectionPersister(ownerType, elementCollectionPath);
    String subAttributeName = attributeName.substring(elementCollectionPath.length() + 1);
    if (persister.getElementType() instanceof ComponentType) {
        ComponentType elementType = (ComponentType) persister.getElementType();
        String[] propertyNames = elementType.getPropertyNames();
        Type[] subtypes = elementType.getSubtypes();
        String[] propertyParts = subAttributeName.split("\\.");
        int offset = 0;
        for (int j = 0; j < propertyParts.length - 1; j++) {
            String propertyName = propertyParts[j];

            for (int i = 0; i < propertyNames.length; i++) {
                int span = subtypes[i].getColumnSpan(persister.getFactory());
                if (propertyName.equals(propertyNames[i])) {
                    if (subtypes[i] instanceof ComponentType) {
                        elementType = (ComponentType) subtypes[i];
                        propertyNames = elementType.getPropertyNames();
                        subtypes = elementType.getSubtypes();
                        break;
                    } else {
                        String[] columnNames = new String[span];
                        String[] elementColumnNames = persister.getElementColumnNames();
                        System.arraycopy(elementColumnNames, offset, columnNames, 0, span);
                        return columnNames;
                    }//w w w  .  ja  va2s.co  m
                } else {
                    offset += span;
                }
            }
        }

        String propertyName = propertyParts[propertyParts.length - 1];
        for (int i = 0; i < propertyNames.length; i++) {
            int span = subtypes[i].getColumnSpan(persister.getFactory());
            if (propertyName.equals(propertyNames[i])) {
                String[] columnNames = new String[span];
                String[] elementColumnNames = persister.getElementColumnNames();
                System.arraycopy(elementColumnNames, offset, columnNames, 0, span);
                return columnNames;
            } else {
                offset += span;
            }
        }
    } else if (persister.getElementType() instanceof org.hibernate.type.EntityType) {
        AbstractEntityPersister elementPersister = (AbstractEntityPersister) entityPersisters
                .get(((org.hibernate.type.EntityType) persister.getElementType()).getAssociatedEntityName());
        Type identifierType = ((org.hibernate.type.EntityType) persister.getElementType())
                .getIdentifierOrUniqueKeyType(persister.getFactory());
        String identifierOrUniqueKeyPropertyName = ((org.hibernate.type.EntityType) persister.getElementType())
                .getIdentifierOrUniqueKeyPropertyName(persister.getFactory());
        String prefix;
        if (identifierType instanceof EmbeddedComponentType) {
            String[] propertyNames = ((EmbeddedComponentType) identifierType).getPropertyNames();
            String[] columnNames = columnNamesByPropertyName(elementPersister, propertyNames, subAttributeName,
                    "", persister.getElementColumnNames(), persister.getFactory());
            if (columnNames != null) {
                return columnNames;
            }
        } else if (subAttributeName.equals(identifierOrUniqueKeyPropertyName)) {
            return persister.getElementColumnNames();
        } else if (identifierType instanceof ComponentType
                && subAttributeName.startsWith(prefix = identifierOrUniqueKeyPropertyName + ".")) {
            String[] propertyNames = ((ComponentType) identifierType).getPropertyNames();
            String[] columnNames = columnNamesByPropertyName(elementPersister, propertyNames,
                    subAttributeName.substring(identifierOrUniqueKeyPropertyName.length() + 1), prefix,
                    persister.getElementColumnNames(), persister.getFactory());
            if (columnNames != null) {
                return columnNames;
            }
        }
    }

    throw new IllegalArgumentException(
            "Couldn't find column names for " + getTypeName(ownerType) + "#" + attributeName);
}

From source file:com.blazebit.persistence.integration.hibernate.base.HibernateJpaProvider.java

License:Apache License

@Override
public String[] getColumnTypes(EntityType<?> entityType, String attributeName) {
    QueryableCollection collectionPersister = getCollectionPersister(entityType, attributeName);
    if (collectionPersister == null) {
        AbstractEntityPersister entityPersister = getEntityPersister(entityType);
        SessionFactoryImplementor sfi = entityPersister.getFactory();
        String[] columnNames = entityPersister.getPropertyColumnNames(attributeName);
        Database database = sfi.getServiceRegistry().locateServiceBinding(Database.class).getService();
        Table[] tables;//from   w ww.jav a2 s  . c o  m

        if (entityPersister instanceof JoinedSubclassEntityPersister) {
            tables = new Table[((JoinedSubclassEntityPersister) entityPersister).getSubclassTableSpan()];
            for (int i = 0; i < tables.length; i++) {
                tables[i] = database.getTable(entityPersister.getSubclassTableName(i));
            }
        } else if (entityPersister instanceof UnionSubclassEntityPersister) {
            tables = new Table[((UnionSubclassEntityPersister) entityPersister).getSubclassTableSpan()];
            for (int i = 0; i < tables.length; i++) {
                tables[i] = database.getTable(unquote(entityPersister.getSubclassTableName(i)));
            }
        } else if (entityPersister instanceof SingleTableEntityPersister) {
            tables = new Table[((SingleTableEntityPersister) entityPersister).getSubclassTableSpan()];
            for (int i = 0; i < tables.length; i++) {
                tables[i] = database.getTable(unquote(entityPersister.getSubclassTableName(i)));
            }
        } else {
            tables = new Table[] { database.getTable(unquote(entityPersister.getTableName())) };
        }

        // In this case, the property might represent a formula
        boolean isFormula = columnNames.length == 1 && columnNames[0] == null;
        boolean isSubselect = tables.length == 1 && tables[0] == null;

        if (isFormula || isSubselect) {
            Type propertyType = entityPersister.getPropertyType(attributeName);
            return getColumnTypeForPropertyType(entityType, attributeName, sfi, propertyType);
        }

        return getColumnTypesForColumnNames(entityType, columnNames, tables);
    } else {
        SessionFactoryImplementor sfi = collectionPersister.getFactory();
        return getColumnTypeForPropertyType(entityType, attributeName, sfi,
                collectionPersister.getElementType());
    }
}

From source file:com.blazebit.persistence.integration.hibernate.base.HibernateJpaProvider.java

License:Apache License

@Override
public String[] getColumnTypes(EntityType<?> ownerType, String elementCollectionPath, String attributeName) {
    QueryableCollection persister = getCollectionPersister(ownerType, elementCollectionPath);
    SessionFactoryImplementor sfi = persister.getFactory();
    String[] columnNames = null;//from ww  w.  j  a va2 s. c o m
    Type propertyType = null;
    String subAttributeName = attributeName.substring(elementCollectionPath.length() + 1);
    if (persister.getElementType() instanceof ComponentType) {
        ComponentType elementType = (ComponentType) persister.getElementType();
        String[] propertyNames = elementType.getPropertyNames();
        Type[] subtypes = elementType.getSubtypes();
        String[] propertyParts = subAttributeName.split("\\.");
        int offset = 0;
        for (int j = 0; j < propertyParts.length - 1; j++) {
            String propertyName = propertyParts[j];

            for (int i = 0; i < propertyNames.length; i++) {
                int span = subtypes[i].getColumnSpan(persister.getFactory());
                if (propertyName.equals(propertyNames[i])) {
                    if (subtypes[i] instanceof ComponentType) {
                        elementType = (ComponentType) subtypes[i];
                        propertyNames = elementType.getPropertyNames();
                        subtypes = elementType.getSubtypes();
                        break;
                    } else {
                        columnNames = new String[span];
                        String[] elementColumnNames = persister.getElementColumnNames();
                        System.arraycopy(elementColumnNames, offset, columnNames, 0, span);
                        break;
                    }
                } else {
                    offset += span;
                }
            }
        }

        if (columnNames == null) {
            String propertyName = propertyParts[propertyParts.length - 1];
            for (int i = 0; i < propertyNames.length; i++) {
                int span = subtypes[i].getColumnSpan(persister.getFactory());
                if (propertyName.equals(propertyNames[i])) {
                    columnNames = new String[span];
                    String[] elementColumnNames = persister.getElementColumnNames();
                    System.arraycopy(elementColumnNames, offset, columnNames, 0, span);
                    break;
                } else {
                    offset += span;
                }
            }
        }
    } else if (persister.getElementType() instanceof org.hibernate.type.EntityType) {
        Type identifierType = ((org.hibernate.type.EntityType) persister.getElementType())
                .getIdentifierOrUniqueKeyType(persister.getFactory());
        String identifierOrUniqueKeyPropertyName = ((org.hibernate.type.EntityType) persister.getElementType())
                .getIdentifierOrUniqueKeyPropertyName(persister.getFactory());
        String prefix;
        if (identifierType instanceof EmbeddedComponentType) {
            String[] propertyNames = ((EmbeddedComponentType) identifierType).getPropertyNames();
            Type[] subtypes = ((EmbeddedComponentType) identifierType).getSubtypes();
            int offset = 0;
            for (int i = 0; i < propertyNames.length; i++) {
                String propertyName = propertyNames[i];
                int span = subtypes[i].getColumnSpan(persister.getFactory());
                if (subAttributeName.equals(propertyName)) {
                    columnNames = new String[span];
                    String[] elementColumnNames = persister.getElementColumnNames();
                    System.arraycopy(elementColumnNames, offset, columnNames, 0, span);
                    propertyType = subtypes[i];
                    break;
                } else {
                    offset += span;
                }
            }
        } else if (subAttributeName.equals(identifierOrUniqueKeyPropertyName)) {
            columnNames = persister.getElementColumnNames();
            propertyType = identifierType;
        } else if (identifierType instanceof ComponentType
                && subAttributeName.startsWith(prefix = identifierOrUniqueKeyPropertyName + ".")) {
            String[] propertyNames = ((ComponentType) identifierType).getPropertyNames();
            Type[] subtypes = ((ComponentType) identifierType).getSubtypes();
            String subPropertyName = subAttributeName.substring(prefix.length());
            int offset = 0;
            for (int i = 0; i < propertyNames.length; i++) {
                String propertyName = propertyNames[i];
                int span = subtypes[i].getColumnSpan(persister.getFactory());
                if (subPropertyName.equals(propertyName)) {
                    columnNames = new String[span];
                    String[] elementColumnNames = persister.getElementColumnNames();
                    System.arraycopy(elementColumnNames, offset, columnNames, 0, span);
                    propertyType = subtypes[i];
                    break;
                } else {
                    offset += span;
                }
            }
        }
    }

    if (columnNames == null) {
        throw new IllegalArgumentException(
                "Couldn't find column names for " + getTypeName(ownerType) + "#" + attributeName);
    }
    boolean isFormula = columnNames.length == 1 && columnNames[0] == null;

    if (isFormula) {
        return getColumnTypeForPropertyType(ownerType, attributeName, sfi, propertyType);
    }

    Database database = sfi.getServiceRegistry().locateServiceBinding(Database.class).getService();
    Table[] tables = new Table[] { database.getTable(unquote(persister.getTableName())) };

    return getColumnTypesForColumnNames(ownerType, columnNames, tables);
}

From source file:com.blazebit.persistence.integration.hibernate.base.HibernateJpaProvider.java

License:Apache License

@Override
public JoinTable getJoinTable(EntityType<?> ownerType, String attributeName) {
    CollectionPersister persister = getCollectionPersister(ownerType, attributeName);
    if (persister instanceof QueryableCollection) {
        QueryableCollection queryableCollection = (QueryableCollection) persister;

        if (!queryableCollection.getElementType().isEntityType()) {
            String[] targetColumnMetaData = queryableCollection.getElementColumnNames();
            Map<String, String> targetColumnMapping = new HashMap<>();

            for (int i = 0; i < targetColumnMetaData.length; i++) {
                targetColumnMapping.put(targetColumnMetaData[i], targetColumnMetaData[i]);
            }/*from w  w  w  .j a v a  2 s.  c o m*/
            return createJoinTable(ownerType, queryableCollection, targetColumnMapping, null, attributeName);
        } else if (queryableCollection.getElementPersister() instanceof Joinable) {
            String elementTableName = ((Joinable) queryableCollection.getElementPersister()).getTableName();
            if (!queryableCollection.getTableName().equals(elementTableName)) {
                String[] targetColumnMetaData = queryableCollection.getElementColumnNames();
                AbstractEntityPersister elementPersister = (AbstractEntityPersister) queryableCollection
                        .getElementPersister();
                String[] targetPrimaryKeyColumnMetaData = elementPersister.getKeyColumnNames();
                Map<String, String> targetIdColumnMapping = new HashMap<>();

                for (int i = 0; i < targetColumnMetaData.length; i++) {
                    targetIdColumnMapping.put(targetColumnMetaData[i], targetPrimaryKeyColumnMetaData[i]);
                }
                Set<String> idAttributeNames = getColumnMatchingAttributeNames(elementPersister,
                        Arrays.asList(targetPrimaryKeyColumnMetaData));
                return createJoinTable(ownerType, queryableCollection, targetIdColumnMapping, idAttributeNames,
                        attributeName);
            }
        }
    }
    return null;
}

From source file:com.blazebit.persistence.integration.hibernate.base.HibernateJpaProvider.java

License:Apache License

private JoinTable createJoinTable(EntityType<?> ownerType, QueryableCollection queryableCollection,
        Map<String, String> targetColumnMapping, Set<String> targetIdAttributeNames, String attributeName) {
    String[] indexColumnNames = queryableCollection.getIndexColumnNames();
    Map<String, String> keyColumnMapping = null;
    Map<String, String> keyColumnTypes = null;
    if (indexColumnNames != null) {
        keyColumnMapping = new HashMap<>(indexColumnNames.length);
        keyColumnTypes = new HashMap<>(indexColumnNames.length);
        if (queryableCollection.getKeyType().isEntityType()) {
            throw new IllegalArgumentException(
                    "Determining the join table key foreign key mappings is not yet supported!");
        } else {/*  ww w  .ja va2s  .  c om*/
            keyColumnMapping.put(indexColumnNames[0], indexColumnNames[0]);
            keyColumnTypes.put(indexColumnNames[0], getColumnTypeForPropertyType(ownerType, attributeName,
                    queryableCollection.getFactory(), queryableCollection.getIndexType())[0]);
        }
    }
    AbstractEntityPersister ownerEntityPersister = (AbstractEntityPersister) queryableCollection
            .getOwnerEntityPersister();
    String[] primaryKeyColumnMetaData = ownerEntityPersister.getKeyColumnNames();
    String[] foreignKeyColumnMetaData = queryableCollection.getKeyColumnNames();
    Map<String, String> idColumnMapping = new HashMap<>(primaryKeyColumnMetaData.length);
    for (int i = 0; i < foreignKeyColumnMetaData.length; i++) {
        idColumnMapping.put(foreignKeyColumnMetaData[i], primaryKeyColumnMetaData[i]);
    }
    Set<String> idAttributeNames = getColumnMatchingAttributeNames(ownerEntityPersister,
            Arrays.asList(primaryKeyColumnMetaData));
    if (targetIdAttributeNames == null) {
        Type elementType = queryableCollection.getElementType();
        if (elementType instanceof ComponentType) {
            targetIdAttributeNames = new HashSet<>();
            collectPropertyNames(targetIdAttributeNames, null, elementType, queryableCollection.getFactory());
        }
    }

    return new JoinTable(queryableCollection.getTableName(), idAttributeNames, idColumnMapping,
            keyColumnMapping, keyColumnTypes, targetIdAttributeNames, targetColumnMapping);
}

From source file:com.blazebit.persistence.integration.hibernate.base.HibernateJpaProvider.java

License:Apache License

@Override
public List<String> getIdentifierOrUniqueKeyEmbeddedPropertyNames(EntityType<?> owner,
        String elementCollectionPath, String attributeName) {
    QueryableCollection persister = getCollectionPersister(owner, elementCollectionPath);
    ComponentType componentType = (ComponentType) persister.getElementType();
    String subAttribute = attributeName.substring(elementCollectionPath.length() + 1);
    // Component types only store direct properties, so we have to go deeper
    String[] propertyParts = subAttribute.split("\\.");
    Type propertyType;//from w  w w .ja  va 2  s.co m
    for (int i = 0; i < propertyParts.length - 1; i++) {
        int index = componentType.getPropertyIndex(propertyParts[i]);
        propertyType = componentType.getSubtypes()[index];
        if (propertyType instanceof ComponentType) {
            componentType = (ComponentType) propertyType;
        } else {
            // A path expression shouldn't navigate over an association..
            throw new IllegalStateException("Can't get the id properties for: " + attributeName);
        }
    }

    propertyType = componentType.getSubtypes()[componentType
            .getPropertyIndex(propertyParts[propertyParts.length - 1])];
    List<String> identifierOrUniqueKeyPropertyNames = new ArrayList<>();
    collectPropertyNames(identifierOrUniqueKeyPropertyNames, null, propertyType, persister.getFactory());
    return identifierOrUniqueKeyPropertyNames;
}

From source file:org.eclipse.emf.cdo.server.internal.hibernate.HibernateStoreChunkReader.java

License:Open Source License

public List<Chunk> executeRead() {
    // get a transaction, the hibernateStoreAccessor is placed in a threadlocal
    // so all db access uses the same session.
    final Session session = getAccessor().getHibernateSession();

    // reread the revision as it is probably unreferenced
    final InternalCDORevision latestRevision = getLatestRevision(session);
    Object value = latestRevision.getValue(getFeature());
    if (value instanceof WrappedHibernateList) {
        value = ((WrappedHibernateList) value).getDelegate();
    }/*from   www.j a  v  a  2s. c o m*/

    // hibernate details...
    boolean useExtraLazyMode = false;
    boolean standardCDOList = false;
    QueryableCollection persister = null;
    CollectionEntry entry = null;
    if (value instanceof PersistentCollection) {
        final PersistentCollection persistentCollection = (PersistentCollection) value;
        persister = (QueryableCollection) ((SessionFactoryImplementor) session.getSessionFactory())
                .getCollectionPersister(persistentCollection.getRole());
        entry = ((SessionImplementor) session).getPersistenceContext().getCollectionEntry(persistentCollection);

        useExtraLazyMode = !persister.getElementType().isEntityType();
        if (useExtraLazyMode && ((PersistentCollection) value).hasQueuedOperations()) {
            session.flush();
        }
    } else {
        standardCDOList = true;
    }

    final List<Chunk> chunks = getChunks();
    for (Chunk chunk : chunks) {
        final int startIndex = chunk.getStartIndex();
        final int maxElements = chunk.size();
        if (standardCDOList) {
            // for eattributes just read them all, no chunking there...
            final CDOList list = (CDOList) value;
            if (startIndex >= list.size()) {
                return chunks;
            }
            for (int i = startIndex; i < startIndex + maxElements; i++) {
                if (i >= list.size()) {
                    break;
                }
                addToChunk(chunk, i - startIndex, list.get(i));
            }
        } else if (useExtraLazyMode) {
            if (getFeature() instanceof EReference) {
                for (int i = startIndex; i < startIndex + maxElements; i++) {
                    final Object object = persister.getElementByIndex(entry.getLoadedKey(), i,
                            (SessionImplementor) session, latestRevision);
                    // could happen if the index > size)
                    if (object == null) {
                        continue;
                    }
                    addToChunk(chunk, i - startIndex, object);
                }
            } else {
                // for eattributes just read them all, no chunking there...
                final List<?> list = (List<?>) value;
                if (startIndex >= list.size()) {
                    return chunks;
                }
                for (int i = startIndex; i < startIndex + maxElements; i++) {
                    if (i >= list.size()) {
                        break;
                    }
                    addToChunk(chunk, i - startIndex, list.get(i));
                }
            }
        } else {
            final Query filterQuery = session.createFilter(value, "");
            filterQuery.setMaxResults(maxElements);
            filterQuery.setFirstResult(startIndex);
            int i = 0;
            for (Object object : filterQuery.list()) {
                addToChunk(chunk, i++, object);
            }
        }
    }
    return chunks;
}