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

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

Introduction

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

Prototype

@Deprecated
ClassMetadata getClassMetadata(Class entityClass);

Source Link

Document

Retrieve the ClassMetadata associated with the given entity class.

Usage

From source file:com.blazebit.persistence.impl.hibernate.function.HibernateJpqlFunctionAdapter.java

License:Apache License

@Override
public Type getReturnType(Type firstArgumentType, Mapping mapping) throws QueryException {
    SessionFactoryImplementor sfi = (SessionFactoryImplementor) mapping;
    Class<?> argumentClass;

    if (firstArgumentType == null) {
        argumentClass = null;/*  w  w  w  . jav a  2s  . c o m*/
    } else {
        argumentClass = firstArgumentType.getReturnedClass();
    }

    Class<?> returnType = function.getReturnType(argumentClass);

    if (returnType == null) {
        return null;
    } else if (argumentClass == returnType) {
        return firstArgumentType;
    }

    Type type = sfi.getTypeHelper().basic(returnType);

    if (type != null) {
        return type;
    }

    if (sfi.getClassMetadata(returnType) != null) {
        return sfi.getTypeHelper().entity(returnType);
    }

    return sfi.getTypeHelper().custom(returnType);
}

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. ja v a  2 s.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!");
}