Search in sources :

Example 1 with AnyMetaDef

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

the class BinderHelper method buildAnyValue.

public static Any buildAnyValue(String anyMetaDefName, Ejb3JoinColumn[] columns, javax.persistence.Column metaColumn, PropertyData inferredData, boolean cascadeOnDelete, Nullability nullability, PropertyHolder propertyHolder, EntityBinder entityBinder, boolean optional, MetadataBuildingContext context) {
    //All FK columns should be in the same table
    Any value = new Any(context.getMetadataCollector(), columns[0].getTable());
    AnyMetaDef metaAnnDef = inferredData.getProperty().getAnnotation(AnyMetaDef.class);
    if (metaAnnDef != null) {
        //local has precedence over general and can be mapped for future reference if named
        bindAnyMetaDefs(inferredData.getProperty(), context);
    } else {
        metaAnnDef = context.getMetadataCollector().getAnyMetaDef(anyMetaDefName);
    }
    if (metaAnnDef != null) {
        value.setIdentifierType(metaAnnDef.idType());
        value.setMetaType(metaAnnDef.metaType());
        HashMap values = new HashMap();
        org.hibernate.type.Type metaType = context.getMetadataCollector().getTypeResolver().heuristicType(value.getMetaType());
        for (MetaValue metaValue : metaAnnDef.metaValues()) {
            try {
                Object discrim = ((org.hibernate.type.DiscriminatorType) metaType).stringToObject(metaValue.value());
                String entityName = metaValue.targetEntity().getName();
                values.put(discrim, entityName);
            } catch (ClassCastException cce) {
                throw new MappingException("metaType was not a DiscriminatorType: " + metaType.getName());
            } catch (Exception e) {
                throw new MappingException("could not interpret metaValue", e);
            }
        }
        if (!values.isEmpty()) {
            value.setMetaValues(values);
        }
    } else {
        throw new AnnotationException("Unable to find @AnyMetaDef for an @(ManyTo)Any mapping: " + StringHelper.qualify(propertyHolder.getPath(), inferredData.getPropertyName()));
    }
    value.setCascadeDeleteEnabled(cascadeOnDelete);
    if (!optional) {
        for (Ejb3JoinColumn column : columns) {
            column.setNullable(false);
        }
    }
    Ejb3Column[] metaColumns = Ejb3Column.buildColumnFromAnnotation(new javax.persistence.Column[] { metaColumn }, null, nullability, propertyHolder, inferredData, entityBinder.getSecondaryTables(), context);
    //set metaColumn to the right table
    for (Ejb3Column column : metaColumns) {
        column.setTable(value.getTable());
    }
    //meta column
    for (Ejb3Column column : metaColumns) {
        column.linkWithValue(value);
    }
    //id columns
    final String propertyName = inferredData.getPropertyName();
    Ejb3Column.checkPropertyConsistency(columns, propertyHolder.getEntityName() + "." + propertyName);
    for (Ejb3JoinColumn column : columns) {
        column.linkWithValue(value);
    }
    return value;
}
Also used : MetaValue(org.hibernate.annotations.MetaValue) HashMap(java.util.HashMap) Any(org.hibernate.mapping.Any) AnnotationException(org.hibernate.AnnotationException) MappingException(org.hibernate.MappingException) ClassLoadingException(org.hibernate.annotations.common.reflection.ClassLoadingException) MappingException(org.hibernate.MappingException) AnyMetaDef(org.hibernate.annotations.AnyMetaDef) AnnotationException(org.hibernate.AnnotationException)

Example 2 with AnyMetaDef

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

the class BinderHelper method bindAnyMetaDefs.

public static void bindAnyMetaDefs(XAnnotatedElement annotatedElement, MetadataBuildingContext context) {
    AnyMetaDef defAnn = annotatedElement.getAnnotation(AnyMetaDef.class);
    AnyMetaDefs defsAnn = annotatedElement.getAnnotation(AnyMetaDefs.class);
    boolean mustHaveName = XClass.class.isAssignableFrom(annotatedElement.getClass()) || XPackage.class.isAssignableFrom(annotatedElement.getClass());
    if (defAnn != null) {
        checkAnyMetaDefValidity(mustHaveName, defAnn, annotatedElement);
        bindAnyMetaDef(defAnn, context);
    }
    if (defsAnn != null) {
        for (AnyMetaDef def : defsAnn.value()) {
            checkAnyMetaDefValidity(mustHaveName, def, annotatedElement);
            bindAnyMetaDef(def, context);
        }
    }
}
Also used : AnyMetaDef(org.hibernate.annotations.AnyMetaDef) XPackage(org.hibernate.annotations.common.reflection.XPackage) XClass(org.hibernate.annotations.common.reflection.XClass) AnyMetaDefs(org.hibernate.annotations.AnyMetaDefs)

Aggregations

AnyMetaDef (org.hibernate.annotations.AnyMetaDef)2 HashMap (java.util.HashMap)1 AnnotationException (org.hibernate.AnnotationException)1 MappingException (org.hibernate.MappingException)1 AnyMetaDefs (org.hibernate.annotations.AnyMetaDefs)1 MetaValue (org.hibernate.annotations.MetaValue)1 ClassLoadingException (org.hibernate.annotations.common.reflection.ClassLoadingException)1 XClass (org.hibernate.annotations.common.reflection.XClass)1 XPackage (org.hibernate.annotations.common.reflection.XPackage)1 Any (org.hibernate.mapping.Any)1