Example usage for org.hibernate.persister.entity AbstractEntityPersister toColumns

List of usage examples for org.hibernate.persister.entity AbstractEntityPersister toColumns

Introduction

In this page you can find the example usage for org.hibernate.persister.entity AbstractEntityPersister toColumns.

Prototype

@Override
public String[] toColumns(String propertyName) throws QueryException 

Source Link

Document

Warning: When there are duplicated property names in the subclasses then this method may return the wrong results.

Usage

From source file:com.aw.core.dao.meta.HbmUtil.java

License:Open Source License

public String getTableColumnName(Class entityClass, String entityField) {
    AbstractEntityPersister entityPersister = (AbstractEntityPersister) sessionFactory
            .getClassMetadata(entityClass);
    if (entityPersister == null)
        throw new IllegalArgumentException("Class " + entityClass + " is not an Hibernate entity class");
    String[] cols = entityPersister.toColumns(entityField);
    return cols != null && cols.length > 0 ? cols[0] : null;
}

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());
            }/* w w  w  .j  a v  a2 s  . co 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;
    }
}