Search in sources :

Example 1 with Map

use of org.hibernate.mapping.Map in project hibernate-orm by hibernate.

the class AttributeFactory method determineAttributeMetadata.

/**
	 * Here is most of the nuts and bolts of this factory, where we interpret the known JPA metadata
	 * against the known Hibernate metadata and build a descriptor for the attribute.
	 *
	 * @param attributeContext The attribute to be described
	 * @param memberResolver Strategy for how to resolve the member defining the attribute.
	 * @param <X> The owner type
	 * @param <Y> The attribute type
	 *
	 * @return The attribute description
	 */
@SuppressWarnings({ "unchecked" })
private <X, Y> AttributeMetadata<X, Y> determineAttributeMetadata(AttributeContext<X> attributeContext, MemberResolver memberResolver) {
    LOG.trace("Starting attribute metadata determination [" + attributeContext.getPropertyMapping().getName() + "]");
    final Member member = memberResolver.resolveMember(attributeContext);
    LOG.trace("    Determined member [" + member + "]");
    final Value value = attributeContext.getPropertyMapping().getValue();
    final org.hibernate.type.Type type = value.getType();
    LOG.trace("    Determined type [name=" + type.getName() + ", class=" + type.getClass().getName() + "]");
    if (type.isAnyType()) {
        // ANY mappings are currently not supported in the JPA metamodel; see HHH-6589
        if (context.isIgnoreUnsupported()) {
            return null;
        } else {
            throw new UnsupportedOperationException("ANY not supported");
        }
    } else if (type.isAssociationType()) {
        // collection or entity
        if (type.isEntityType()) {
            // entity
            return new SingularAttributeMetadataImpl<X, Y>(attributeContext.getPropertyMapping(), attributeContext.getOwnerType(), member, determineSingularAssociationAttributeType(member));
        }
        // collection
        if (value instanceof Collection) {
            final Collection collValue = (Collection) value;
            final Value elementValue = collValue.getElement();
            final org.hibernate.type.Type elementType = elementValue.getType();
            // First, determine the type of the elements and use that to help determine the
            // collection type)
            final Attribute.PersistentAttributeType elementPersistentAttributeType;
            final Attribute.PersistentAttributeType persistentAttributeType;
            if (elementType.isAnyType()) {
                if (context.isIgnoreUnsupported()) {
                    return null;
                } else {
                    throw new UnsupportedOperationException("collection of any not supported yet");
                }
            }
            final boolean isManyToMany = isManyToMany(member);
            if (elementValue instanceof Component) {
                elementPersistentAttributeType = Attribute.PersistentAttributeType.EMBEDDED;
                persistentAttributeType = Attribute.PersistentAttributeType.ELEMENT_COLLECTION;
            } else if (elementType.isAssociationType()) {
                elementPersistentAttributeType = isManyToMany ? Attribute.PersistentAttributeType.MANY_TO_MANY : Attribute.PersistentAttributeType.ONE_TO_MANY;
                persistentAttributeType = elementPersistentAttributeType;
            } else {
                elementPersistentAttributeType = Attribute.PersistentAttributeType.BASIC;
                persistentAttributeType = Attribute.PersistentAttributeType.ELEMENT_COLLECTION;
            }
            final Attribute.PersistentAttributeType keyPersistentAttributeType;
            // Finally, we determine the type of the map key (if needed)
            if (value instanceof Map) {
                final Value keyValue = ((Map) value).getIndex();
                final org.hibernate.type.Type keyType = keyValue.getType();
                if (keyType.isAnyType()) {
                    if (context.isIgnoreUnsupported()) {
                        return null;
                    } else {
                        throw new UnsupportedOperationException("collection of any not supported yet");
                    }
                }
                if (keyValue instanceof Component) {
                    keyPersistentAttributeType = Attribute.PersistentAttributeType.EMBEDDED;
                } else if (keyType.isAssociationType()) {
                    keyPersistentAttributeType = Attribute.PersistentAttributeType.MANY_TO_ONE;
                } else {
                    keyPersistentAttributeType = Attribute.PersistentAttributeType.BASIC;
                }
            } else {
                keyPersistentAttributeType = null;
            }
            return new PluralAttributeMetadataImpl(attributeContext.getPropertyMapping(), attributeContext.getOwnerType(), member, persistentAttributeType, elementPersistentAttributeType, keyPersistentAttributeType);
        } else if (value instanceof OneToMany) {
            // element value within a o.h.mapping.Collection (see logic branch above)
            throw new IllegalArgumentException("HUH???");
        //					final boolean isManyToMany = isManyToMany( member );
        //					//one to many with FK => entity
        //					return new PluralAttributeMetadataImpl(
        //							attributeContext.getPropertyMapping(),
        //							attributeContext.getOwnerType(),
        //							member,
        //							isManyToMany
        //									? Attribute.PersistentAttributeType.MANY_TO_MANY
        //									: Attribute.PersistentAttributeType.ONE_TO_MANY
        //							value,
        //							AttributeContext.TypeStatus.ENTITY,
        //							Attribute.PersistentAttributeType.ONE_TO_MANY,
        //							null, null, null
        //					);
        }
    } else if (attributeContext.getPropertyMapping().isComposite()) {
        // component
        return new SingularAttributeMetadataImpl<X, Y>(attributeContext.getPropertyMapping(), attributeContext.getOwnerType(), member, Attribute.PersistentAttributeType.EMBEDDED);
    } else {
        // basic type
        return new SingularAttributeMetadataImpl<X, Y>(attributeContext.getPropertyMapping(), attributeContext.getOwnerType(), member, Attribute.PersistentAttributeType.BASIC);
    }
    throw new UnsupportedOperationException("oops, we are missing something: " + attributeContext.getPropertyMapping());
}
Also used : OneToMany(org.hibernate.mapping.OneToMany) EmbeddedComponentType(org.hibernate.type.EmbeddedComponentType) EntityType(org.hibernate.type.EntityType) ComponentType(org.hibernate.type.ComponentType) Type(javax.persistence.metamodel.Type) ParameterizedType(java.lang.reflect.ParameterizedType) Value(org.hibernate.mapping.Value) Collection(org.hibernate.mapping.Collection) Component(org.hibernate.mapping.Component) Member(java.lang.reflect.Member) Map(org.hibernate.mapping.Map)

Example 2 with Map

use of org.hibernate.mapping.Map in project hibernate-orm by hibernate.

the class MapKeyCustomEnumTypeTest method getMapKeyType.

private Type getMapKeyType(Property prop) {
    Value value = prop.getValue();
    assertEquals(Map.class, value.getClass());
    Map map = (Map) value;
    return map.getIndex().getType();
}
Also used : Value(org.hibernate.mapping.Value) Map(org.hibernate.mapping.Map)

Example 3 with Map

use of org.hibernate.mapping.Map in project hibernate-orm by hibernate.

the class ValueVisitorTest method testProperCallbacks.

@Test
public void testProperCallbacks() {
    final MetadataImplementor metadata = (MetadataImplementor) new MetadataSources(serviceRegistry).buildMetadata();
    final Table tbl = new Table();
    final RootClass rootClass = new RootClass(metadataBuildingContext);
    ValueVisitor vv = new ValueVisitorValidator();
    new Any(metadata, tbl).accept(vv);
    new Array(metadata, rootClass).accept(vv);
    new Bag(metadata, rootClass).accept(vv);
    new Component(metadata, rootClass).accept(vv);
    new DependantValue(metadata, tbl, null).accept(vv);
    new IdentifierBag(metadata, rootClass).accept(vv);
    new List(metadata, rootClass).accept(vv);
    new ManyToOne(metadata, tbl).accept(vv);
    new Map(metadata, rootClass).accept(vv);
    new OneToMany(metadata, rootClass).accept(vv);
    new OneToOne(metadata, tbl, rootClass).accept(vv);
    new PrimitiveArray(metadata, rootClass).accept(vv);
    new Set(metadata, rootClass).accept(vv);
    new SimpleValue(metadata).accept(vv);
}
Also used : RootClass(org.hibernate.mapping.RootClass) Table(org.hibernate.mapping.Table) Set(org.hibernate.mapping.Set) ValueVisitor(org.hibernate.mapping.ValueVisitor) DependantValue(org.hibernate.mapping.DependantValue) MetadataSources(org.hibernate.boot.MetadataSources) Bag(org.hibernate.mapping.Bag) IdentifierBag(org.hibernate.mapping.IdentifierBag) MetadataImplementor(org.hibernate.boot.spi.MetadataImplementor) IdentifierBag(org.hibernate.mapping.IdentifierBag) OneToMany(org.hibernate.mapping.OneToMany) Any(org.hibernate.mapping.Any) ManyToOne(org.hibernate.mapping.ManyToOne) SimpleValue(org.hibernate.mapping.SimpleValue) Array(org.hibernate.mapping.Array) PrimitiveArray(org.hibernate.mapping.PrimitiveArray) OneToOne(org.hibernate.mapping.OneToOne) PrimitiveArray(org.hibernate.mapping.PrimitiveArray) List(org.hibernate.mapping.List) Component(org.hibernate.mapping.Component) Map(org.hibernate.mapping.Map) Test(org.junit.Test)

Aggregations

Map (org.hibernate.mapping.Map)3 Component (org.hibernate.mapping.Component)2 OneToMany (org.hibernate.mapping.OneToMany)2 Value (org.hibernate.mapping.Value)2 Member (java.lang.reflect.Member)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (javax.persistence.metamodel.Type)1 MetadataSources (org.hibernate.boot.MetadataSources)1 MetadataImplementor (org.hibernate.boot.spi.MetadataImplementor)1 Any (org.hibernate.mapping.Any)1 Array (org.hibernate.mapping.Array)1 Bag (org.hibernate.mapping.Bag)1 Collection (org.hibernate.mapping.Collection)1 DependantValue (org.hibernate.mapping.DependantValue)1 IdentifierBag (org.hibernate.mapping.IdentifierBag)1 List (org.hibernate.mapping.List)1 ManyToOne (org.hibernate.mapping.ManyToOne)1 OneToOne (org.hibernate.mapping.OneToOne)1 PrimitiveArray (org.hibernate.mapping.PrimitiveArray)1 RootClass (org.hibernate.mapping.RootClass)1