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

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

Introduction

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

Prototype

SessionFactoryImplementor getFactory();

Source Link

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 ww. ja v  a  2 s  .  c o  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  w w .  ja  v  a 2s .co  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;// w w  w . j a  v  a2s. com
    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

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 {/*from  w w w  . j  a v a2s.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  .j av  a  2 s.  c  o  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;
}