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

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

Introduction

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

Prototype

public SessionFactoryImplementor getFactory() 

Source Link

Usage

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;
    }//www .ja va  2  s. com
    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

@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  w ww.j a  va  2s.c om

            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[] 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  ww .  ja  v  a  2s  .  c  om

        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 {//from   w  ww. j a va 2  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;
}