Search in sources :

Example 1 with CollectionType

use of org.hibernate.annotations.CollectionType in project hibernate-orm by hibernate.

the class CollectionBinder method getCollectionBinder.

/**
 * collection binder factory
 */
public static CollectionBinder getCollectionBinder(String entityName, XProperty property, boolean isIndexed, boolean isHibernateExtensionMapping, MetadataBuildingContext buildingContext) {
    final CollectionBinder result;
    if (property.isArray()) {
        if (property.getElementClass().isPrimitive()) {
            result = new PrimitiveArrayBinder();
        } else {
            result = new ArrayBinder();
        }
    } else if (property.isCollection()) {
        // TODO consider using an XClass
        Class returnedClass = property.getCollectionClass();
        if (java.util.Set.class.equals(returnedClass)) {
            if (property.isAnnotationPresent(CollectionId.class)) {
                throw new AnnotationException("Set do not support @CollectionId: " + StringHelper.qualify(entityName, property.getName()));
            }
            result = new SetBinder(false);
        } else if (java.util.SortedSet.class.equals(returnedClass)) {
            if (property.isAnnotationPresent(CollectionId.class)) {
                throw new AnnotationException("Set do not support @CollectionId: " + StringHelper.qualify(entityName, property.getName()));
            }
            result = new SetBinder(true);
        } else if (java.util.Map.class.equals(returnedClass)) {
            if (property.isAnnotationPresent(CollectionId.class)) {
                throw new AnnotationException("Map do not support @CollectionId: " + StringHelper.qualify(entityName, property.getName()));
            }
            result = new MapBinder(false);
        } else if (java.util.SortedMap.class.equals(returnedClass)) {
            if (property.isAnnotationPresent(CollectionId.class)) {
                throw new AnnotationException("Map do not support @CollectionId: " + StringHelper.qualify(entityName, property.getName()));
            }
            result = new MapBinder(true);
        } else if (java.util.Collection.class.equals(returnedClass)) {
            if (property.isAnnotationPresent(CollectionId.class)) {
                result = new IdBagBinder();
            } else {
                result = new BagBinder();
            }
        } else if (java.util.List.class.equals(returnedClass)) {
            if (isIndexed) {
                if (property.isAnnotationPresent(CollectionId.class)) {
                    throw new AnnotationException("List do not support @CollectionId and @OrderColumn (or @IndexColumn) at the same time: " + StringHelper.qualify(entityName, property.getName()));
                }
                result = new ListBinder();
            } else if (property.isAnnotationPresent(CollectionId.class)) {
                result = new IdBagBinder();
            } else {
                result = new BagBinder();
            }
        } else {
            throw new AnnotationException(returnedClass.getName() + " collection not yet supported: " + StringHelper.qualify(entityName, property.getName()));
        }
    } else {
        throw new AnnotationException("Illegal attempt to map a non collection as a @OneToMany, @ManyToMany or @CollectionOfElements: " + StringHelper.qualify(entityName, property.getName()));
    }
    result.setIsHibernateExtensionMapping(isHibernateExtensionMapping);
    final CollectionType typeAnnotation = property.getAnnotation(CollectionType.class);
    if (typeAnnotation != null) {
        final String typeName = typeAnnotation.type();
        // see if it names a type-def
        final TypeDefinition typeDef = buildingContext.getMetadataCollector().getTypeDefinition(typeName);
        if (typeDef != null) {
            result.explicitType = typeDef.getTypeImplementorClass().getName();
            result.explicitTypeParameters.putAll(typeDef.getParameters());
        } else {
            result.explicitType = typeName;
            for (Parameter param : typeAnnotation.parameters()) {
                result.explicitTypeParameters.setProperty(param.name(), param.value());
            }
        }
    }
    return result;
}
Also used : TypeDefinition(org.hibernate.boot.model.TypeDefinition) CollectionId(org.hibernate.annotations.CollectionId) CollectionType(org.hibernate.annotations.CollectionType) AnnotationException(org.hibernate.AnnotationException) LazyCollection(org.hibernate.annotations.LazyCollection) Collection(org.hibernate.mapping.Collection) ElementCollection(javax.persistence.ElementCollection) Parameter(org.hibernate.annotations.Parameter) PersistentClass(org.hibernate.mapping.PersistentClass) XClass(org.hibernate.annotations.common.reflection.XClass) Map(java.util.Map) BinderHelper.toAliasTableMap(org.hibernate.cfg.BinderHelper.toAliasTableMap) HashMap(java.util.HashMap) BinderHelper.toAliasEntityMap(org.hibernate.cfg.BinderHelper.toAliasEntityMap)

Aggregations

HashMap (java.util.HashMap)1 Map (java.util.Map)1 ElementCollection (javax.persistence.ElementCollection)1 AnnotationException (org.hibernate.AnnotationException)1 CollectionId (org.hibernate.annotations.CollectionId)1 CollectionType (org.hibernate.annotations.CollectionType)1 LazyCollection (org.hibernate.annotations.LazyCollection)1 Parameter (org.hibernate.annotations.Parameter)1 XClass (org.hibernate.annotations.common.reflection.XClass)1 TypeDefinition (org.hibernate.boot.model.TypeDefinition)1 BinderHelper.toAliasEntityMap (org.hibernate.cfg.BinderHelper.toAliasEntityMap)1 BinderHelper.toAliasTableMap (org.hibernate.cfg.BinderHelper.toAliasTableMap)1 Collection (org.hibernate.mapping.Collection)1 PersistentClass (org.hibernate.mapping.PersistentClass)1