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

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

Introduction

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

Prototype

@Override
public Type getPropertyType(String propertyName) throws MappingException 

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.autobizlogic.abl.metadata.hibernate.HibMetaRole.java

License:Open Source License

/**
 * Get the inverse of a one-to-many role
 * @param entityName The entity owning the role
 * @param rName The role name//from  w  w w . j a  va  2s  .c  o m
 * @return Null if no inverse was found, otherwise [entity name, role name] of the inverse role
 */
private String[] getInverseOfCollectionRole(String entityName, String rName) {
    String[] result = new String[2];

    SessionFactory sessFact = ((HibMetaModel) getMetaEntity().getMetaModel()).getSessionFactory();
    AbstractCollectionPersister parentMeta = (AbstractCollectionPersister) sessFact
            .getCollectionMetadata(entityName + "." + rName);
    if (parentMeta == null) { // Could be inherited -- search through superclasses
        while (parentMeta == null) {
            Class<?> cls = null;
            if (getMetaEntity().getEntityType() == MetaEntity.EntityType.POJO)
                cls = sessFact.getClassMetadata(entityName).getMappedClass(EntityMode.POJO);
            else
                cls = sessFact.getClassMetadata(entityName).getMappedClass(EntityMode.MAP);
            Class<?> superCls = cls.getSuperclass();
            if (superCls.getName().equals("java.lang.Object"))
                throw new RuntimeException(
                        "Unable to retrieve Hibernate information for collection " + entityName + "." + rName);
            ClassMetadata clsMeta = sessFact.getClassMetadata(superCls);
            if (clsMeta == null)
                throw new RuntimeException("Unable to retrieve Hibernate information for collection "
                        + entityName + "." + rName + ", even from superclass(es)");
            entityName = clsMeta.getEntityName();
            parentMeta = (AbstractCollectionPersister) sessFact.getCollectionMetadata(entityName + "." + rName);
        }
    }
    String[] colNames = parentMeta.getKeyColumnNames();
    String childName = parentMeta.getElementType().getName();

    AbstractEntityPersister childMeta = (AbstractEntityPersister) sessFact.getClassMetadata(childName);
    String[] propNames = childMeta.getPropertyNames();
    for (int i = 0; i < propNames.length; i++) {
        Type type = childMeta.getPropertyType(propNames[i]);
        if (!type.isEntityType())
            continue;
        EntityType entType = (EntityType) type;
        if (!entType.getAssociatedEntityName().equals(entityName))
            continue;
        String[] cnames = childMeta.getPropertyColumnNames(i);
        if (cnames.length != colNames.length)
            continue;
        boolean columnMatch = true;
        for (int j = 0; j < cnames.length; j++) {
            if (!cnames[j].equals(colNames[j])) {
                columnMatch = false;
                break;
            }
        }
        if (columnMatch) {
            result[0] = childName;
            result[1] = propNames[i];
            return result;
        }
    }

    return null;
}

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

License:Open Source License

/**
 * Get the inverse of a many-to-one role
 * @param entityName The entity owning the role
 * @param rName The role name//from  w w w .  j  a va  2  s .com
 * @return Null if no inverse was found, otherwise [entity name, role name] of the inverse role
 */
private String[] getInverseOfSingleRole(String entityName, String rName) {
    String[] result = new String[2];

    SessionFactory sessFact = ((HibMetaModel) getMetaEntity().getMetaModel()).getSessionFactory();
    AbstractEntityPersister childMeta = (AbstractEntityPersister) sessFact.getClassMetadata(entityName);
    int propIdx = childMeta.getPropertyIndex(rName);
    String[] cnames = childMeta.getPropertyColumnNames(propIdx);
    Type parentType = childMeta.getPropertyType(rName);
    if (parentType instanceof OneToOneType)
        return getInverseOfOneToOneRole(entityName, rName);
    if (!(parentType instanceof ManyToOneType))
        throw new RuntimeException("Inverse of single-valued role " + entityName + "." + rName
                + " is neither single-valued not multi-valued");
    ManyToOneType manyType = (ManyToOneType) parentType;
    String parentEntityName = manyType.getAssociatedEntityName();

    AbstractEntityPersister parentMeta = (AbstractEntityPersister) sessFact.getClassMetadata(parentEntityName);
    String[] propNames = parentMeta.getPropertyNames();
    for (int i = 0; i < propNames.length; i++) {
        Type type = parentMeta.getPropertyType(propNames[i]);
        if (!type.isCollectionType())
            continue;
        CollectionType collType = (CollectionType) type;
        if (!collType.getAssociatedEntityName((SessionFactoryImplementor) sessFact).equals(entityName))
            continue;

        AbstractCollectionPersister persister = (AbstractCollectionPersister) sessFact
                .getCollectionMetadata(parentEntityName + "." + propNames[i]);
        String[] colNames = persister.getKeyColumnNames();
        if (cnames.length != colNames.length)
            continue;
        boolean columnMatch = true;
        for (int j = 0; j < cnames.length; j++) {
            if (!cnames[j].equals(colNames[j])) {
                columnMatch = false;
                break;
            }
        }
        if (columnMatch) {
            result[0] = parentEntityName;
            result[1] = propNames[i];
            return result;
        }
    }

    return null;
}

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

License:Open Source License

public EntityPropertyMapper buildPropertyMapper(Class entityClass) {
    AbstractEntityPersister entityPersister = (AbstractEntityPersister) sessionFactory
            .getClassMetadata(entityClass);
    if (entityPersister == null)
        throw new IllegalArgumentException("Class " + entityClass + " is not an Hibernate entity class");
    EntityPropertyMapper mapper = new EntityPropertyMapper(entityClass, entityPersister.getTableName());
    String[] propertyNames = entityPersister.getPropertyNames();
    for (String propertyName : propertyNames) {
        String[] columnNames = entityPersister.getPropertyColumnNames(propertyName);
        Type propertyType = entityPersister.getPropertyType(propertyName);
        mapper.put(propertyName, propertyType, columnNames);
    }//from   www  .j  av  a 2 s.c o  m
    String identifierPropertyName = entityPersister.getIdentifierPropertyName();
    String[] identifierColumnNames = entityPersister.getIdentifierColumnNames();
    Type identifierPropertyType = entityPersister.getIdentifierType();
    mapper.putId(identifierPropertyName, identifierPropertyType, identifierColumnNames);

    return mapper;
}

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

License:Apache License

@Override
public boolean isForeignJoinColumn(EntityType<?> ownerType, String attributeName) {
    AbstractEntityPersister persister = getEntityPersister(ownerType);
    Type propertyType = persister.getPropertyType(attributeName);

    if (propertyType instanceof OneToOneType) {
        OneToOneType oneToOneType = (OneToOneType) propertyType;
        // It is foreign if there is a mapped by attribute
        // But as of Hibernate 5.4 we noticed that we have to treat nullable one-to-ones as "foreign" as well
        return (oneToOneType).getRHSUniqueKeyPropertyName() != null || isNullable(oneToOneType);
    }//  w ww.  j  av a 2 s . c  o m

    // Every entity persister has "owned" properties on table number 0, others have higher numbers
    int tableNumber = persister.getSubclassPropertyTableNumber(attributeName);
    return tableNumber >= persister.getEntityMetamodel().getSubclassEntityNames().size();
}

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

License:Apache License

@Override
public ConstraintType requiresTreatFilter(EntityType<?> ownerType, String attributeName, JoinType joinType) {
    // When we don't support treat joins(Hibernate 4.2), we need to put the constraints into the WHERE clause
    if (!supportsTreatJoin() && joinType == JoinType.INNER) {
        return ConstraintType.WHERE;
    }/*from  w w  w.  j av  a2  s.  co m*/
    AbstractEntityPersister persister = getEntityPersister(ownerType);
    Type propertyType = persister.getPropertyType(attributeName);

    if (!(propertyType instanceof AssociationType)) {
        return ConstraintType.NONE;
    }

    // When the inner treat joined element is collection, we always need the constraint, see HHH-??? TODO: report issue
    if (propertyType instanceof CollectionType) {
        if (joinType == JoinType.INNER) {
            if (isForeignKeyDirectionToParent((CollectionType) propertyType)) {
                return ConstraintType.WHERE;
            }

            return ConstraintType.ON;
        } else if (!((CollectionType) propertyType).getElementType(persister.getFactory()).isEntityType()) {
            return ConstraintType.NONE;
        }
    }

    String propertyEntityName = ((AssociationType) propertyType)
            .getAssociatedEntityName(persister.getFactory());
    AbstractEntityPersister propertyTypePersister = (AbstractEntityPersister) entityPersisters
            .get(propertyEntityName);

    // When the treat joined element is a union subclass, we always need the constraint, see HHH-??? TODO: report issue
    if (propertyTypePersister instanceof UnionSubclassEntityPersister) {
        return ConstraintType.ON;
    }

    return ConstraintType.NONE;
}

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);
            }/* www.  j  av a  2  s  . com*/
        } 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

private Set<String> removeIdentifierAccess(EntityType<?> elementType, Set<String> elementAttributeNames,
        EntityType<?> ownerType, Set<String> columnMatchingAttributeNames) {
    Set<String> set = new LinkedHashSet<>();
    Iterator<String> iterator = elementAttributeNames.iterator();
    AbstractEntityPersister elementPersister = getEntityPersister(elementType);
    AbstractEntityPersister entityPersister = getEntityPersister(ownerType);
    List<String> addedAttributeNames = new ArrayList<>();
    for (String attributeName : columnMatchingAttributeNames) {
        String elementAttributeName = iterator.next();
        Type propertyType = entityPersister.getPropertyType(attributeName);
        if (propertyType instanceof org.hibernate.type.EntityType) {
            // If the columns refer to an association, we map that through instead of trying to set just the identifier properties
            if (elementPersister.getEntityName()
                    .equals(((org.hibernate.type.EntityType) propertyType).getAssociatedEntityName())) {
                List<String> identifierPropertyNames = getIdentifierOrUniqueKeyEmbeddedPropertyNames(ownerType,
                        attributeName);/*  ww w . ja va 2 s.  co  m*/
                iterator.remove();
                for (int i = 1; i < identifierPropertyNames.size(); i++) {
                    iterator.next();
                    iterator.remove();
                }
                addedAttributeNames.add(attributeName);
            }
        } else {
            set.add(attributeName);
        }
    }

    for (String addedAttributeName : addedAttributeNames) {
        set.add(addedAttributeName);
        elementAttributeNames.add("");
    }

    return set;
}

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

License:Apache License

private String[] columnNamesByPropertyName(AbstractEntityPersister persister, String[] propertyNames,
        String subAttributeName, String prefix, String[] elementColumnNames, Mapping factory) {
    int offset = 0;
    for (int i = 0; i < propertyNames.length; i++) {
        String propertyName = propertyNames[i];
        Type propertyType = persister.getPropertyType(prefix + propertyName);
        int span = propertyType.getColumnSpan(factory);
        if (subAttributeName.equals(propertyName)) {
            String[] columnNames = new String[span];
            System.arraycopy(elementColumnNames, offset, columnNames, 0, span);
            return columnNames;
        } else {/*from  w  w w.jav a  2 s  .  c  om*/
            offset += span;
        }
    }

    return null;
}

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;/* w w  w.j  a  v a  2s . 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 List<String> getIdentifierOrUniqueKeyEmbeddedPropertyNames(EntityType<?> owner, String attributeName) {
    AbstractEntityPersister entityPersister = getEntityPersister(owner);
    Type propertyType = entityPersister.getPropertyType(attributeName);
    List<String> identifierOrUniqueKeyPropertyNames = new ArrayList<>();

    if (propertyType instanceof CollectionType) {
        Type elementType = ((CollectionType) propertyType).getElementType(entityPersister.getFactory());
        Collection<String> targetAttributeNames = getJoinTable(owner, attributeName).getTargetAttributeNames();
        if (targetAttributeNames == null) {
            collectPropertyNames(identifierOrUniqueKeyPropertyNames, null, elementType,
                    entityPersister.getFactory());
        } else {// w  ww .  j  av  a2  s  .  c o m
            AbstractEntityPersister elementPersister = (AbstractEntityPersister) entityPersisters
                    .get(((org.hibernate.type.EntityType) elementType).getAssociatedEntityName());
            for (String targetAttributeName : targetAttributeNames) {
                collectPropertyNames(identifierOrUniqueKeyPropertyNames, targetAttributeName,
                        elementPersister.getPropertyType(targetAttributeName), entityPersister.getFactory());
            }
        }
    } else {
        collectPropertyNames(identifierOrUniqueKeyPropertyNames, null, propertyType,
                entityPersister.getFactory());
    }

    return identifierOrUniqueKeyPropertyNames;
}