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

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

Introduction

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

Prototype

public String[] getIdentifierColumnNames() 

Source Link

Usage

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);
    }//  www.  j a v  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 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 www .  j a  v a  2 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;
    }
}

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

License:Apache License

private static Set<String> getColumnMatchingAttributeNames(AbstractEntityPersister ownerEntityPersister,
        List<String> idColumnNames) {
    Set<String> idAttributeNames = new LinkedHashSet<>();
    Type identifierType = ownerEntityPersister.getIdentifierType();
    if (identifierType instanceof ComponentType) {
        String[] idPropertyNames = ((ComponentType) identifierType).getPropertyNames();
        for (String propertyName : idPropertyNames) {
            String attributeName = ownerEntityPersister.getIdentifierPropertyName() + "." + propertyName;
            String[] propertyColumnNames = ownerEntityPersister.getSubclassPropertyColumnNames(attributeName);
            if (propertyColumnNames != null) {
                for (int j = 0; j < propertyColumnNames.length; j++) {
                    String propertyColumnName = propertyColumnNames[j];
                    if (idColumnNames.contains(propertyColumnName)) {
                        idAttributeNames.add(attributeName);
                        break;
                    }/*from w w w .ja va2 s.c om*/
                }
            }
        }
        // We assume that when a primary identifier attribute is part of the id column names, that we are done
        if (!idAttributeNames.isEmpty()) {
            return idAttributeNames;
        }
    } else {
        for (String identifierColumnName : ownerEntityPersister.getIdentifierColumnNames()) {
            if (idColumnNames.contains(identifierColumnName)) {
                idAttributeNames.add(ownerEntityPersister.getIdentifierPropertyName());
                return idAttributeNames;
            }
        }
    }
    String[] propertyNames = ownerEntityPersister.getPropertyNames();
    for (int i = 0; i < propertyNames.length; i++) {
        String propertyName = propertyNames[i];
        String[] propertyColumnNames = ownerEntityPersister.getSubclassPropertyColumnNames(propertyName);
        if (propertyColumnNames != null) {
            for (int j = 0; j < propertyColumnNames.length; j++) {
                String propertyColumnName = propertyColumnNames[j];
                if (idColumnNames.contains(propertyColumnName)) {
                    idAttributeNames.add(propertyName);
                    break;
                }
            }
        }
    }
    return idAttributeNames;
}

From source file:org.nextframework.persistence.PersistenceUtils.java

License:Apache License

public static InverseCollectionProperties getInverseCollectionProperty(SessionFactory sessionFactory,
        Class<? extends Object> clazz, String collectionProperty) {
    SessionFactoryImplementor sessionFactoryImplementor;
    String[] keyColumnNames;/*from   w  w w . j  a  v a  2s . co  m*/
    Class<?> returnedClass;
    try {
        sessionFactoryImplementor = (SessionFactoryImplementor) sessionFactory;
        ClassMetadata classMetadata = getClassMetadata(clazz, sessionFactory);
        if (classMetadata == null) {
            throw new PersistenceException("Class " + clazz.getName() + " is not mapped. ");
        }
        CollectionType ct = (CollectionType) classMetadata.getPropertyType(collectionProperty);
        AbstractCollectionPersister collectionMetadata = (AbstractCollectionPersister) sessionFactoryImplementor
                .getCollectionMetadata(ct.getRole());
        keyColumnNames = ((AbstractCollectionPersister) collectionMetadata).getKeyColumnNames();
        returnedClass = ct.getElementType(sessionFactoryImplementor).getReturnedClass();
    } catch (ClassCastException e) {
        throw new PersistenceException(
                "Property \"" + collectionProperty + "\" of " + clazz + " is not a mapped as a collection.");
    }

    AbstractEntityPersister collectionItemMetadata = (AbstractEntityPersister) sessionFactoryImplementor
            .getClassMetadata(returnedClass);
    Type[] propertyTypes = collectionItemMetadata.getPropertyTypes();
    String[] propertyNames = collectionItemMetadata.getPropertyNames();
    for (int i = 0; i < propertyTypes.length; i++) {
        Type type = propertyTypes[i];
        String propertyName = propertyNames[i];
        String[] propertyColumnNames = collectionItemMetadata.getPropertyColumnNames(propertyName);
        InverseCollectionProperties inverseCollectionProperties = getInverseCollectionProperties(
                sessionFactoryImplementor, clazz, returnedClass, keyColumnNames, propertyColumnNames, type,
                propertyName);
        if (inverseCollectionProperties != null) {
            return inverseCollectionProperties;
        }
    }
    //check id
    Type identifierType = collectionItemMetadata.getIdentifierType();
    String identifierName = collectionItemMetadata.getIdentifierPropertyName();
    String[] identifierColumnNames = collectionItemMetadata.getIdentifierColumnNames();
    InverseCollectionProperties inverseCollectionProperties = getInverseCollectionProperties(
            sessionFactoryImplementor, clazz, returnedClass, keyColumnNames, identifierColumnNames,
            identifierType, identifierName);
    if (inverseCollectionProperties != null) {
        return inverseCollectionProperties;
    }
    throw new PersistenceException(
            "Collection " + collectionProperty + " of " + clazz + " does not have an inverse path!");
}

From source file:to.etc.domui.hibernate.model.CriteriaCreatingVisitor.java

License:Open Source License

/**
 * Hibernate's jokish metadata does not include the PK in it's properties structures. So
 * we explicitly need to check if the name is the PK property, then return the column names
 * for that PK./*from   w w w.  j av a2  s .  c o  m*/
 *
 * @param aep
 * @param compoundName
 * @return
 */
@NonNull
private String[] getPropertyColumnNamesFromLousyMetadata(AbstractEntityPersister aep, String compoundName) {
    String name = compoundName;
    int dotix = compoundName.lastIndexOf('.');
    if (dotix != -1) {
        name = compoundName.substring(dotix + 1);
    }

    //-- The PK property is not part of the "properties" in hibernate's idiot metadata. So first check if we're looking at that ID property.
    if (name.equals(aep.getIdentifierPropertyName())) {
        return aep.getIdentifierColumnNames();
    }
    int ix = aep.getPropertyIndex(name);
    if (ix < 0)
        throw new QQuerySyntaxException(
                "Cannot obtain Hibernate metadata for property=" + name + ": property index not found");
    String[] colar = aep.getPropertyColumnNames(ix);
    if (colar == null || colar.length != 1/* || colar[0] == null*/)
        throw new QQuerySyntaxException("'Like' cannot be done on multicolumn/0column property " + name);
    return colar;
}