Example usage for org.hibernate.type ComponentType getPropertyNames

List of usage examples for org.hibernate.type ComponentType getPropertyNames

Introduction

In this page you can find the example usage for org.hibernate.type ComponentType getPropertyNames.

Prototype

@Override
    public String[] getPropertyNames() 

Source Link

Usage

From source file:com.autobizlogic.abl.metadata.hibernate.HibMetaEntity.java

License:Open Source License

/**
 * Read all the properties from Hibernate metadata
 *//*  w w w  . j a v  a 2s  . co  m*/
private void loadAllProperties() {
    if (allPropsRetrieved)
        return;

    synchronized (this) {
        if (!allPropsRetrieved) {
            metaAttributes = new HashMap<String, MetaAttribute>();
            metaRoles = new HashMap<String, MetaRole>();
            ClassMetadata meta = persister.getClassMetadata();
            String[] propNames = meta.getPropertyNames();
            for (String propName : propNames) {
                Type type;
                try {
                    type = persister.getClassMetadata().getPropertyType(propName);
                } catch (QueryException ex) {
                    throw new RuntimeException("Unable to determine type for property " + propName
                            + " of entity " + persister.getEntityName());
                }
                if (type.isComponentType()) {
                    // Do nothing
                } else if (type.isCollectionType() || type.isEntityType() || type.isAssociationType()) {
                    boolean isParentToChild = type.isCollectionType();
                    MetaRole mr = new HibMetaRole(this, propName, isParentToChild);
                    metaRoles.put(propName, mr);
                } else {
                    MetaAttribute ma = new HibMetaAttribute(propName, type.getReturnedClass(), false);
                    metaAttributes.put(propName, ma);
                }
            }

            // Often the primary attribute(s) is not returned by ClassMetadata.getPropertyNames
            // So we add it by hand here
            String pkName = meta.getIdentifierPropertyName();

            if (pkName == null) { // Can happen for composite keys
                Type pkType = meta.getIdentifierType();
                if (pkType.isComponentType()) {
                    ComponentType ctype = (ComponentType) pkType;
                    String[] pnames = ctype.getPropertyNames();
                    for (String pname : pnames) {
                        MetaAttribute ma = new HibMetaAttribute(pname,
                                meta.getPropertyType(pname).getReturnedClass(), false);
                        metaAttributes.put(pname, ma);
                    }
                } else
                    throw new RuntimeException(
                            "Unexpected: anonymous PK is not composite - class " + meta.getEntityName());
            } else if (!metaAttributes.containsKey(pkName)) {
                MetaAttribute ma = new HibMetaAttribute(pkName, meta.getIdentifierType().getReturnedClass(),
                        false);
                metaAttributes.put(pkName, ma);
            }

            allPropsRetrieved = true;
        }
    }
}

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

License:Apache License

private boolean isColumnShared(AbstractEntityPersister subclassPersister, String[] columnNames) {
    List<String> propertiesToCheck = new ArrayList<>(Arrays.asList(subclassPersister.getPropertyNames()));
    while (!propertiesToCheck.isEmpty()) {
        String propertyName = propertiesToCheck.remove(propertiesToCheck.size() - 1);
        Type propertyType = subclassPersister.getPropertyType(propertyName);
        if (propertyType instanceof ComponentType) {
            ComponentType componentType = (ComponentType) propertyType;
            for (String subPropertyName : componentType.getPropertyNames()) {
                propertiesToCheck.add(propertyName + "." + subPropertyName);
            }//  w ww .ja v a 2 s .  c o m
        } else {
            String[] subclassColumnNames = subclassPersister.getSubclassPropertyColumnNames(propertyName);
            if (Arrays.deepEquals(columnNames, subclassColumnNames)) {
                return true;
            }
        }
    }

    return false;
}

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

License:Apache License

@Override
public Map<String, String> getWritableMappedByMappings(EntityType<?> inverseType, EntityType<?> ownerType,
        String attributeName, String inverseAttribute) {
    AbstractEntityPersister entityPersister = getEntityPersister(ownerType);
    int propertyIndex = entityPersister.getEntityMetamodel().getPropertyIndex(attributeName);
    Type propertyType = entityPersister.getPropertyTypes()[propertyIndex];
    org.hibernate.type.EntityType ownerPropertyType;
    if (propertyType instanceof CollectionType) {
        QueryableCollection persister = getCollectionPersister(ownerType, attributeName);
        AbstractEntityPersister inversePersister = getEntityPersister(inverseType);
        if (!persister.isInverse() && persister.getTableName().equals(inversePersister.getTableName())) {
            // We have a one-to-many relationship that has just join columns

            // Find properties for element columns in entityPersister
            // Map to properties for key columns of inverseType
            Set<String> elementAttributes = getColumnMatchingAttributeNames(entityPersister,
                    Arrays.asList((entityPersister.toColumns(attributeName))));
            Set<String> keyAttributes = removeIdentifierAccess(ownerType, elementAttributes, inverseType,
                    getColumnMatchingAttributeNames(inversePersister,
                            Arrays.asList(persister.getKeyColumnNames())));

            Map<String, String> mapping = new HashMap<>();
            Iterator<String> elemAttrIter = elementAttributes.iterator();
            Iterator<String> keyAttrIter = keyAttributes.iterator();

            while (elemAttrIter.hasNext()) {
                mapping.put(elemAttrIter.next(), keyAttrIter.next());
            }/*from   ww w  .ja  v  a 2 s.  c o m*/

            if (mapping.isEmpty()) {
                throw new IllegalArgumentException("Mapped by property '" + inverseType.getName() + "#"
                        + attributeName + "' must be writable or the column must be part of the id!");
            }
            return mapping;
        } else {
            // We only support detection when the inverse collection is writable
            if (entityPersister.getEntityMetamodel().getPropertyInsertability()[propertyIndex]) {
                return null;
            }
            throw new IllegalArgumentException("Mapped by property '" + inverseType.getName() + "#"
                    + attributeName + "' must be writable!");
        }
    } else {
        // Either the mapped by property is writable
        if (entityPersister.getEntityMetamodel().getPropertyInsertability()[propertyIndex]) {
            return null;
        }
        // Or the columns of the mapped by property are part of the target id
        ownerPropertyType = (org.hibernate.type.EntityType) propertyType;
    }
    AbstractEntityPersister sourceType = (AbstractEntityPersister) entityPersisters
            .get(ownerPropertyType.getAssociatedEntityName());
    Type identifierType = ownerPropertyType.getIdentifierOrUniqueKeyType(entityPersister.getFactory());
    String sourcePropertyPrefix;
    String[] sourcePropertyNames;
    if (identifierType.isComponentType()) {
        ComponentType componentType = (ComponentType) identifierType;
        sourcePropertyPrefix = sourceType.getIdentifierPropertyName() + ".";
        sourcePropertyNames = componentType.getPropertyNames();
    } else {
        sourcePropertyPrefix = "";
        sourcePropertyNames = new String[] { sourceType.getIdentifierPropertyName() };
    }
    String[] targetColumnNames = entityPersister.getPropertyColumnNames(propertyIndex);
    Type targetIdType = entityPersister.getIdentifierType();
    if (targetIdType.isComponentType()) {
        ComponentType targetIdentifierType = (ComponentType) entityPersister.getIdentifierType();
        String targetPropertyPrefix = entityPersister.getIdentifierPropertyName() + ".";
        String[] identifierColumnNames = entityPersister.getIdentifierColumnNames();
        String[] targetIdentifierTypePropertyNames = targetIdentifierType.getPropertyNames();
        Map<String, String> mapping = new HashMap<>();
        for (int i = 0; i < targetColumnNames.length; i++) {
            for (int j = 0; j < identifierColumnNames.length; j++) {
                if (targetColumnNames[i].equals(identifierColumnNames[j])) {
                    mapping.put(sourcePropertyPrefix + sourcePropertyNames[i],
                            targetPropertyPrefix + targetIdentifierTypePropertyNames[j]);
                    break;
                }
            }
        }
        if (mapping.isEmpty()) {
            throw new IllegalArgumentException("Mapped by property '" + inverseType.getName() + "#"
                    + attributeName + "' must be writable or the column must be part of the id!");
        }
        return mapping;
    } else {
        String targetIdColumnName = entityPersister.getIdentifierColumnNames()[0];
        if (!targetIdColumnName.equals(targetColumnNames[0])) {
            throw new IllegalArgumentException("Mapped by property '" + inverseType.getName() + "#"
                    + attributeName + "' must be writable or the column must be part of the id!");
        }
        Map<String, String> mapping = new HashMap<>();
        mapping.put(sourcePropertyPrefix + sourcePropertyNames[0], entityPersister.getIdentifierPropertyName());
        return mapping;
    }
}

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;
                    }/*from   w ww  .  ja  v a2  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<?> ownerType, String elementCollectionPath, String attributeName) {
    QueryableCollection persister = getCollectionPersister(ownerType, elementCollectionPath);
    SessionFactoryImplementor sfi = persister.getFactory();
    String[] columnNames = null;//from  w w w  .j  a va2  s  . c om
    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 void collectPropertyNames(Collection<String> propertyNames, String prefix, Type propertyType,
        Mapping factory) {/*from   w  w w .  java 2  s  .c om*/
    if (propertyType instanceof ComponentType) {
        ComponentType componentType = (ComponentType) propertyType;
        for (String propertyName : componentType.getPropertyNames()) {
            Type subtype = componentType.getSubtypes()[componentType.getPropertyIndex(propertyName)];
            collectPropertyNames(propertyNames, prefix == null ? propertyName : prefix + "." + propertyName,
                    subtype, factory);
        }
    } else if (propertyType instanceof org.hibernate.type.EntityType) {
        org.hibernate.type.EntityType entityType = (org.hibernate.type.EntityType) propertyType;
        Type identifierOrUniqueKeyType = entityType.getIdentifierOrUniqueKeyType(factory);

        if (identifierOrUniqueKeyType instanceof EmbeddedComponentType) {
            EmbeddedComponentType embeddedComponentType = (EmbeddedComponentType) identifierOrUniqueKeyType;
            for (String propertyName : embeddedComponentType.getPropertyNames()) {
                propertyNames.add(prefix == null ? propertyName : prefix + "." + propertyName);
            }
        } else {
            String identifierOrUniqueKeyPropertyName = entityType.getIdentifierOrUniqueKeyPropertyName(factory);
            propertyNames.add(prefix == null ? identifierOrUniqueKeyPropertyName
                    : prefix + "." + identifierOrUniqueKeyPropertyName);
        }
    } else if (!(propertyType instanceof CollectionType) && prefix != null) {
        propertyNames.add(prefix);
    }
}

From source file:com.evolveum.midpoint.repo.sql.util.MidpointPersisterUtil.java

License:Apache License

private static void killUnwantedAssociationValues(String[] propertyNames, Type[] propertyTypes, Object[] values,
        int depth) {
    if (values == null) {
        return;//  w w w .ja  v  a 2s  .  c o  m
    }

    for (int i = 0; i < propertyTypes.length; i++) {
        String name = propertyNames[i];
        Type type = propertyTypes[i];
        Object value = values[i];
        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace("{}- killUnwantedAssociationValues processing #{}: {} (type={}, value={})",
                    StringUtils.repeat("  ", depth), i, name, type, value);
        }
        if (type instanceof ComponentType) {
            ComponentType componentType = (ComponentType) type;
            killUnwantedAssociationValues(componentType.getPropertyNames(), componentType.getSubtypes(),
                    (Object[]) value, depth + 1);
        } else if (type instanceof ManyToOneType) {
            if (ASSOCIATION_TO_REMOVE.equals(name)) {
                if (LOGGER.isTraceEnabled()) {
                    LOGGER.trace("{}- killUnwantedAssociationValues KILLED #{}: {} (type={}, value={})",
                            StringUtils.repeat("  ", depth), i, name, type, value);
                }
                values[i] = null;
            }
        }
    }
}

From source file:com.vaadin.data.hbnutil.HbnContainer.java

License:Open Source License

/**
 * This is an HbnContainer specific utility method that is used to retrieve the list of embedded property key
 * identifiers.//from w  w w.  j ava  2s .  c o  m
 */
private Collection<String> getEmbeddedKeyPropertyIds() {
    logger.executionTrace();

    final ArrayList<String> embeddedKeyPropertyIds = new ArrayList<String>();
    final Type identifierType = classMetadata.getIdentifierType();

    if (identifierType.isComponentType()) {
        final ComponentType idComponent = (ComponentType) identifierType;
        final String[] propertyNameArray = idComponent.getPropertyNames();

        if (propertyNameArray != null) {
            final List<String> propertyNames = Arrays.asList(propertyNameArray);
            embeddedKeyPropertyIds.addAll(propertyNames);
        }
    }

    return embeddedKeyPropertyIds;
}

From source file:com.vaadin.data.hbnutil.HbnContainer.java

License:Open Source License

/**
 * Gets the data type of all Properties identified by the given Property ID. This method does pretty much the same
 * thing as EntityItemProperty#getType()
 *///from  w  ww.  ja v a 2 s  .  com
public Class<?> getType(Object propertyId) {
    logger.executionTrace();

    // TODO: refactor to use same code as EntityItemProperty#getType()
    // This will also fix incomplete implementation of this method (for association types). Not critical as
    // Components don't really rely on this methods.

    if (addedProperties.keySet().contains(propertyId))
        return addedProperties.get(propertyId);

    if (propertyInEmbeddedKey(propertyId)) {
        final ComponentType idType = (ComponentType) classMetadata.getIdentifierType();
        final String[] propertyNames = idType.getPropertyNames();

        for (int i = 0; i < propertyNames.length; i++) {
            String name = propertyNames[i];
            if (name.equals(propertyId)) {
                String idName = classMetadata.getIdentifierPropertyName();
                try {
                    Field idField = entityType.getDeclaredField(idName);
                    Field propertyField = idField.getType().getDeclaredField((String) propertyId);
                    return propertyField.getType();
                } catch (NoSuchFieldException ex) {
                    throw new RuntimeException("Could not find the type of specified container property.", ex);
                }
            }
        }
    }

    Type propertyType = classMetadata.getPropertyType(propertyId.toString());
    return propertyType.getReturnedClass();
}

From source file:com.vaadin.data.hbnutil.HbnContainer.java

License:Open Source License

/**
 * This is an internal HbnContainer utility method. Determines if a property is contained within an embedded key.
 *//* w w  w  .  ja v a2 s  .co m*/
private boolean propertyInEmbeddedKey(Object propertyId) {
    logger.executionTrace();

    if (embeddedPropertiesCache.containsKey(propertyId))
        return embeddedPropertiesCache.get(propertyId);

    final Type identifierType = classMetadata.getIdentifierType();

    if (identifierType.isComponentType()) {
        final ComponentType componentType = (ComponentType) identifierType;
        final String[] idPropertyNames = componentType.getPropertyNames();
        final List<String> idPropertyNameList = Arrays.asList(idPropertyNames);
        return idPropertyNameList.contains(propertyId);
    }

    return false;
}