Example usage for org.hibernate.type Type getReturnedClass

List of usage examples for org.hibernate.type Type getReturnedClass

Introduction

In this page you can find the example usage for org.hibernate.type Type getReturnedClass.

Prototype

Class getReturnedClass();

Source Link

Document

The class returned by #nullSafeGet methods.

Usage

From source file:be.shad.tsqb.helper.TypeSafeQueryHelperImpl.java

License:Apache License

/**
 * Retrieves the type information from hibernate.
 *//*  w w  w.j  a  v  a  2s  .c o  m*/
private Class<?> getTargetEntityClass(Type propertyType) {
    if (CollectionType.class.isAssignableFrom(propertyType.getClass())) {
        CollectionType collectionType = (CollectionType) propertyType;
        Type elementType = collectionType.getElementType((SessionFactoryImplementor) sessionFactory);
        return elementType.getReturnedClass();
    }
    return propertyType.getReturnedClass();
}

From source file:br.com.caelum.vraptor.hibernate.extra.ParameterLoader.java

License:Open Source License

@SuppressWarnings({ "unchecked", "rawtypes" })
private Object load(String name, Class type) {
    String idProperty = session.getSessionFactory().getClassMetadata(type).getIdentifierPropertyName();
    checkArgument(idProperty != null, "Entity %s must have an id property for @Load.", type.getSimpleName());

    String parameter = request.getParameter(name + "." + idProperty);
    if (parameter == null) {
        return null;
    }/*from ww  w.j av  a 2 s  .c  o  m*/

    Type idType = session.getSessionFactory().getClassMetadata(type).getIdentifierType();
    Converter<?> converter = converters.to(idType.getReturnedClass());
    checkArgument(converter != null, "Entity %s id type %s must have a converter", type.getSimpleName(),
            idType);

    Serializable id = (Serializable) converter.convert(parameter, type);
    return session.get(type, id);
}

From source file:br.com.caelum.vraptor.plugin.hibernate4.extra.ParameterLoaderInterceptor.java

License:Open Source License

@SuppressWarnings({ "unchecked", "rawtypes" })
private Object load(String name, Class type) {
    String idProperty = session.getSessionFactory().getClassMetadata(type).getIdentifierPropertyName();
    checkArgument(idProperty != null, "Entity %s must have an id property for @Load.", type.getSimpleName());

    String parameter = request.getParameter(name + "." + idProperty);
    if (parameter == null) {
        return null;
    }/*from   w w w  .j a  v  a2s .c  o  m*/

    Type idType = session.getSessionFactory().getClassMetadata(type).getIdentifierType();
    Converter<?> converter = converters.to(idType.getReturnedClass());
    checkArgument(converter != null, "Entity %s id type %s must have a converter", type.getSimpleName(),
            idType);

    Serializable id = (Serializable) converter.convert(parameter, type, localization.getBundle());
    return session.get(type, id);
}

From source file:br.com.caelum.vraptor.util.hibernate.extra.ParameterLoaderInterceptor.java

License:Open Source License

@SuppressWarnings({ "unchecked", "rawtypes" })
private Object load(String name, Class type) {
    String idProperty = session.getSessionFactory().getClassMetadata(type).getIdentifierPropertyName();
    checkArgument(idProperty != null,// www.  ja  v a 2 s.c  o m
            "Entity " + type.getSimpleName() + " must have an id property for @Load.");

    String parameter = request.getParameter(name + "." + idProperty);
    if (parameter == null) {
        return null;
    }

    Type idType = session.getSessionFactory().getClassMetadata(type).getIdentifierType();
    Converter<?> converter = converters.to(idType.getReturnedClass());
    checkArgument(converter != null,
            "Entity " + type.getSimpleName() + " id type " + idType + " must have a converter");

    Serializable id = (Serializable) converter.convert(parameter, type, localization.getBundle());
    return session.get(type, id);
}

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

License:Open Source License

/**
 * Read all the properties from Hibernate metadata
 *//*from   w  ww  .j  a va 2 s. com*/
private void loadAllProperties() {
    if (allPropsRetrieved)
        return;

    synchronized (this) {
        if (!allPropsRetrieved) {
            metaAttributes = new HashMap<String, MetaAttribute>();
            metaRoles = new HashMap<String, MetaRole>();
            ClassMetadata meta = persister.getClassMetadata();
            String[] propNames = meta.getPropertyNames();
            for (String propName : propNames) {
                Type type;
                try {
                    type = persister.getClassMetadata().getPropertyType(propName);
                } catch (QueryException ex) {
                    throw new RuntimeException("Unable to determine type for property " + propName
                            + " of entity " + persister.getEntityName());
                }
                if (type.isComponentType()) {
                    // Do nothing
                } else if (type.isCollectionType() || type.isEntityType() || type.isAssociationType()) {
                    boolean isParentToChild = type.isCollectionType();
                    MetaRole mr = new HibMetaRole(this, propName, isParentToChild);
                    metaRoles.put(propName, mr);
                } else {
                    MetaAttribute ma = new HibMetaAttribute(propName, type.getReturnedClass(), false);
                    metaAttributes.put(propName, ma);
                }
            }

            // Often the primary attribute(s) is not returned by ClassMetadata.getPropertyNames
            // So we add it by hand here
            String pkName = meta.getIdentifierPropertyName();

            if (pkName == null) { // Can happen for composite keys
                Type pkType = meta.getIdentifierType();
                if (pkType.isComponentType()) {
                    ComponentType ctype = (ComponentType) pkType;
                    String[] pnames = ctype.getPropertyNames();
                    for (String pname : pnames) {
                        MetaAttribute ma = new HibMetaAttribute(pname,
                                meta.getPropertyType(pname).getReturnedClass(), false);
                        metaAttributes.put(pname, ma);
                    }
                } else
                    throw new RuntimeException(
                            "Unexpected: anonymous PK is not composite - class " + meta.getEntityName());
            } else if (!metaAttributes.containsKey(pkName)) {
                MetaAttribute ma = new HibMetaAttribute(pkName, meta.getIdentifierType().getReturnedClass(),
                        false);
                metaAttributes.put(pkName, ma);
            }

            allPropsRetrieved = true;
        }
    }
}

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;//ww  w.  j  av 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:com.blazebit.persistence.integration.hibernate.base.function.HibernateSQLFunctionAdapter.java

License:Apache License

@Override
public Class<?> getReturnType(Class<?> firstArgumentType) {
    if (firstArgumentType == null) {
        return null;
    }//from w ww. ja  v a 2 s.  c  o m
    Type type = sfi.getTypeHelper().basic(firstArgumentType);
    if (type == null) {
        if (sfi.getEntityPersisters().get(firstArgumentType.getName()) != null) {
            type = sfi.getTypeHelper().entity(firstArgumentType);
        } else {
            try {
                type = sfi.getTypeHelper().custom(firstArgumentType);
            } catch (MappingException ex) {
                type = sfi.getTypeHelper().heuristicType(firstArgumentType.getName());
            }
        }
    }

    if (type != null) {
        Type returnType = function.getReturnType(type, sfi);
        if (returnType != null) {
            return returnType.getReturnedClass();
        }
    }

    return null;
}

From source file:com.blazebit.security.impl.interceptor.ChangeInterceptor.java

License:Apache License

private void checkAssociation(Object entity, String propertyName, Type type, String action,
        UserContext userContext, PermissionService permissionService, EntityResourceFactory resourceFactory,
        ActionFactory actionFactory) {/* www  . j  a  va  2 s  .  co m*/
    Map<Class<?>, Tuple> toBeChecked = new HashMap<Class<?>, Tuple>();
    // it can only be on collection types
    ManyToOne manyToOne;
    // look for annotation on getter level
    manyToOne = ReflectionUtils.getGetter(entity.getClass(), propertyName).getAnnotation(ManyToOne.class);
    if (manyToOne == null) {
        // on field level
        manyToOne = ReflectionUtils.getField(entity.getClass(), propertyName).getAnnotation(ManyToOne.class);
    }

    if (manyToOne != null) {
        Object val = null;

        try {
            val = ReflectionUtils.getGetter(entity.getClass(), propertyName).invoke(entity);
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }

        if (val != null) {
            toBeChecked.put(type.getReturnedClass(), new Tuple(val, propertyName));
        }

    }

    for (Map.Entry<Class<?>, Tuple> entry : toBeChecked.entrySet()) {
        Class<?> c = entry.getKey();

        // TODO: We do this complicated stuff to retrieve the inverse mapping, this can be simplified
        for (Field f : ReflectionUtils.getInstanceFields(c)) {
            OneToMany oneToMany;
            if ((oneToMany = ReflectionUtils.getGetter(c, f.getName())
                    .getAnnotation(OneToMany.class)) != null) {
                if (entity.getClass().isAssignableFrom(ReflectionUtils.getResolvedFieldTypeArguments(c, f)[0])
                        && (oneToMany.mappedBy().isEmpty() ? true
                                : oneToMany.mappedBy().equals(entry.getValue().fieldName))) {
                    if (!permissionService.isGranted(userContext.getUser(), actionFactory.createAction(action),
                            resourceFactory.createResource(entry.getValue().o, f.getName()))) {
                        throw new PermissionActionException("Entity " + c + "'s field " + f.getName()
                                + " cannot be " + action + "(e)d by " + userContext.getUser());
                    }
                }
            }
        }
    }

}

From source file:com.ejushang.steward.common.genericdao.search.hibernate.HibernateEntityMetadata.java

License:Apache License

public Metadata getPropertyType(String property) {
    Type pType = metadata.getPropertyType(property);
    Class<?> pCollectionType = null;
    if (pType.isCollectionType()) {
        pType = ((CollectionType) pType).getElementType((SessionFactoryImplementor) sessionFactory);
        pCollectionType = pType.getReturnedClass();
    }/*from   w w  w . j  av  a 2  s .  c  om*/

    if (pType.isEntityType()) {
        return new HibernateEntityMetadata(sessionFactory,
                sessionFactory.getClassMetadata(((EntityType) pType).getName()), pCollectionType);
    } else {
        return new HibernateNonEntityMetadata(sessionFactory, pType, pCollectionType);
    }
}

From source file:com.ejushang.steward.common.genericdao.search.hibernate.HibernateNonEntityMetadata.java

License:Apache License

public Metadata getPropertyType(String property) {
    if (!type.isComponentType())
        return null;

    int i = getPropertyIndex(property);
    if (i == -1) {
        return null;
    } else {/*from w ww  . ja va  2  s.c om*/
        Type pType = ((ComponentType) type).getSubtypes()[i];
        Class<?> pCollectionType = null;
        if (pType.isCollectionType()) {
            pType = ((CollectionType) pType).getElementType((SessionFactoryImplementor) sessionFactory);
            pCollectionType = pType.getReturnedClass();
        }
        if (pType.isEntityType()) {
            return new HibernateEntityMetadata(sessionFactory,
                    sessionFactory.getClassMetadata(((EntityType) pType).getName()), pCollectionType);
        } else {
            return new HibernateNonEntityMetadata(sessionFactory, pType, pCollectionType);
        }
    }
}