Example usage for org.hibernate.engine.spi SessionFactoryImplementor getCollectionMetadata

List of usage examples for org.hibernate.engine.spi SessionFactoryImplementor getCollectionMetadata

Introduction

In this page you can find the example usage for org.hibernate.engine.spi SessionFactoryImplementor getCollectionMetadata.

Prototype

@Deprecated
CollectionMetadata getCollectionMetadata(String roleName);

Source Link

Document

Get the CollectionMetadata associated with the named collection role.

Usage

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  ww w .  j a v a 2  s  .  c o 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!");
}