Search in sources :

Example 1 with CollectionId

use of org.hibernate.annotations.CollectionId 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)

Example 2 with CollectionId

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

the class IdBagBinder method bindStarToManySecondPass.

@Override
protected boolean bindStarToManySecondPass(Map persistentClasses, XClass collType, Ejb3JoinColumn[] fkJoinColumns, Ejb3JoinColumn[] keyColumns, Ejb3JoinColumn[] inverseColumns, Ejb3Column[] elementColumns, boolean isEmbedded, XProperty property, boolean unique, TableBinder associationTableBinder, boolean ignoreNotFound, MetadataBuildingContext buildingContext) {
    boolean result = super.bindStarToManySecondPass(persistentClasses, collType, fkJoinColumns, keyColumns, inverseColumns, elementColumns, isEmbedded, property, unique, associationTableBinder, ignoreNotFound, getBuildingContext());
    CollectionId collectionIdAnn = property.getAnnotation(CollectionId.class);
    if (collectionIdAnn != null) {
        SimpleValueBinder simpleValue = new SimpleValueBinder();
        PropertyData propertyData = new WrappedInferredData(new PropertyInferredData(null, property, //default access should not be useful
        null, buildingContext.getBuildingOptions().getReflectionManager()), "id");
        Ejb3Column[] idColumns = Ejb3Column.buildColumnFromAnnotation(collectionIdAnn.columns(), null, Nullability.FORCED_NOT_NULL, propertyHolder, propertyData, Collections.EMPTY_MAP, buildingContext);
        //we need to make sure all id columns must be not-null.
        for (Ejb3Column idColumn : idColumns) {
            idColumn.setNullable(false);
        }
        Table table = collection.getCollectionTable();
        simpleValue.setTable(table);
        simpleValue.setColumns(idColumns);
        Type typeAnn = collectionIdAnn.type();
        if (typeAnn != null && !BinderHelper.isEmptyAnnotationValue(typeAnn.type())) {
            simpleValue.setExplicitType(typeAnn);
        } else {
            throw new AnnotationException("@CollectionId is missing type: " + StringHelper.qualify(propertyHolder.getPath(), propertyName));
        }
        simpleValue.setBuildingContext(getBuildingContext());
        SimpleValue id = simpleValue.make();
        ((IdentifierCollection) collection).setIdentifier(id);
        String generator = collectionIdAnn.generator();
        String generatorType;
        if ("identity".equals(generator) || "assigned".equals(generator) || "sequence".equals(generator) || "native".equals(generator)) {
            generatorType = generator;
            generator = "";
        } else {
            generatorType = null;
        }
        BinderHelper.makeIdGenerator(id, generatorType, generator, getBuildingContext(), localGenerators);
    }
    return result;
}
Also used : WrappedInferredData(org.hibernate.cfg.WrappedInferredData) PropertyData(org.hibernate.cfg.PropertyData) Table(org.hibernate.mapping.Table) PropertyInferredData(org.hibernate.cfg.PropertyInferredData) SimpleValue(org.hibernate.mapping.SimpleValue) IdentifierCollection(org.hibernate.mapping.IdentifierCollection) Type(org.hibernate.annotations.Type) CollectionId(org.hibernate.annotations.CollectionId) AnnotationException(org.hibernate.AnnotationException) Ejb3Column(org.hibernate.cfg.Ejb3Column)

Aggregations

AnnotationException (org.hibernate.AnnotationException)2 CollectionId (org.hibernate.annotations.CollectionId)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ElementCollection (javax.persistence.ElementCollection)1 CollectionType (org.hibernate.annotations.CollectionType)1 LazyCollection (org.hibernate.annotations.LazyCollection)1 Parameter (org.hibernate.annotations.Parameter)1 Type (org.hibernate.annotations.Type)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 Ejb3Column (org.hibernate.cfg.Ejb3Column)1 PropertyData (org.hibernate.cfg.PropertyData)1 PropertyInferredData (org.hibernate.cfg.PropertyInferredData)1 WrappedInferredData (org.hibernate.cfg.WrappedInferredData)1 Collection (org.hibernate.mapping.Collection)1 IdentifierCollection (org.hibernate.mapping.IdentifierCollection)1 PersistentClass (org.hibernate.mapping.PersistentClass)1