Search in sources :

Example 1 with OneToMany

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

the class CollectionMetadataGenerator method addCollection.

void addCollection() {
    final Type type = propertyValue.getType();
    final Value value = propertyValue.getElement();
    final boolean oneToManyAttachedType = type instanceof BagType || type instanceof SetType || type instanceof MapType || type instanceof ListType;
    final boolean inverseOneToMany = (value instanceof OneToMany) && (propertyValue.isInverse());
    final boolean owningManyToOneWithJoinTableBidirectional = (value instanceof ManyToOne) && (propertyAuditingData.getRelationMappedBy() != null);
    final boolean fakeOneToManyBidirectional = (value instanceof OneToMany) && (propertyAuditingData.getAuditMappedBy() != null);
    if (oneToManyAttachedType && (inverseOneToMany || fakeOneToManyBidirectional || owningManyToOneWithJoinTableBidirectional)) {
        // A one-to-many relation mapped using @ManyToOne and @OneToMany(mappedBy="...")
        addOneToManyAttached(fakeOneToManyBidirectional);
    } else {
        // All other kinds of relations require a middle (join) table.
        addWithMiddleTable();
    }
}
Also used : ListType(org.hibernate.type.ListType) ManyToOneType(org.hibernate.type.ManyToOneType) MaterializedNClobType(org.hibernate.type.MaterializedNClobType) SetType(org.hibernate.type.SetType) MaterializedClobType(org.hibernate.type.MaterializedClobType) MapType(org.hibernate.type.MapType) ComponentType(org.hibernate.type.ComponentType) SortedSetType(org.hibernate.type.SortedSetType) SortedMapType(org.hibernate.type.SortedMapType) BagType(org.hibernate.type.BagType) Type(org.hibernate.type.Type) SetType(org.hibernate.type.SetType) SortedSetType(org.hibernate.type.SortedSetType) BagType(org.hibernate.type.BagType) ListType(org.hibernate.type.ListType) Value(org.hibernate.mapping.Value) OneToMany(org.hibernate.mapping.OneToMany) MapType(org.hibernate.type.MapType) SortedMapType(org.hibernate.type.SortedMapType) ManyToOne(org.hibernate.mapping.ManyToOne)

Example 2 with OneToMany

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

the class ListBinder method bindIndex.

private void bindIndex(final MetadataBuildingContext buildingContext) {
    if (!indexColumn.isImplicit()) {
        PropertyHolder valueHolder = PropertyHolderBuilder.buildPropertyHolder(this.collection, StringHelper.qualify(this.collection.getRole(), "key"), null, null, propertyHolder, getBuildingContext());
        List list = (List) this.collection;
        if (!list.isOneToMany())
            indexColumn.forceNotNull();
        indexColumn.setPropertyHolder(valueHolder);
        SimpleValueBinder value = new SimpleValueBinder();
        value.setColumns(new Ejb3Column[] { indexColumn });
        value.setExplicitType("integer");
        value.setBuildingContext(getBuildingContext());
        SimpleValue indexValue = value.make();
        indexColumn.linkWithValue(indexValue);
        list.setIndex(indexValue);
        list.setBaseIndex(indexColumn.getBase());
        if (list.isOneToMany() && !list.getKey().isNullable() && !list.isInverse()) {
            String entityName = ((OneToMany) list.getElement()).getReferencedEntityName();
            PersistentClass referenced = buildingContext.getMetadataCollector().getEntityBinding(entityName);
            IndexBackref ib = new IndexBackref();
            ib.setName('_' + propertyName + "IndexBackref");
            ib.setUpdateable(false);
            ib.setSelectable(false);
            ib.setCollectionRole(list.getRole());
            ib.setEntityName(list.getOwner().getEntityName());
            ib.setValue(list.getIndex());
            referenced.addProperty(ib);
        }
    } else {
        Collection coll = this.collection;
        throw new AnnotationException("List/array has to be annotated with an @OrderColumn (or @IndexColumn): " + coll.getRole());
    }
}
Also used : PropertyHolder(org.hibernate.cfg.PropertyHolder) Collection(org.hibernate.mapping.Collection) AnnotationException(org.hibernate.AnnotationException) List(org.hibernate.mapping.List) IndexBackref(org.hibernate.mapping.IndexBackref) OneToMany(org.hibernate.mapping.OneToMany) SimpleValue(org.hibernate.mapping.SimpleValue) PersistentClass(org.hibernate.mapping.PersistentClass)

Example 3 with OneToMany

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

the class MapBinder method createFormulatedValue.

protected Value createFormulatedValue(Value value, Collection collection, String targetPropertyName, PersistentClass associatedClass, PersistentClass targetPropertyPersistentClass, MetadataBuildingContext buildingContext) {
    Value element = collection.getElement();
    String fromAndWhere = null;
    if (!(element instanceof OneToMany)) {
        String referencedPropertyName = null;
        if (element instanceof ToOne) {
            referencedPropertyName = ((ToOne) element).getReferencedPropertyName();
        } else if (element instanceof DependantValue) {
            //TODO this never happen I think
            if (propertyName != null) {
                referencedPropertyName = collection.getReferencedPropertyName();
            } else {
                throw new AnnotationException("SecondaryTable JoinColumn cannot reference a non primary key");
            }
        }
        Iterator<Selectable> referencedEntityColumns;
        if (referencedPropertyName == null) {
            referencedEntityColumns = associatedClass.getIdentifier().getColumnIterator();
        } else {
            Property referencedProperty = associatedClass.getRecursiveProperty(referencedPropertyName);
            referencedEntityColumns = referencedProperty.getColumnIterator();
        }
        fromAndWhere = getFromAndWhereFormula(associatedClass.getTable().getName(), element.getColumnIterator(), referencedEntityColumns);
    } else {
        // HHH-11005 - only if we are OneToMany and location of map key property is at a different level, need to add a select
        if (!associatedClass.equals(targetPropertyPersistentClass)) {
            fromAndWhere = getFromAndWhereFormula(targetPropertyPersistentClass.getTable().getQualifiedTableName().toString(), element.getColumnIterator(), associatedClass.getIdentifier().getColumnIterator());
        }
    }
    if (value instanceof Component) {
        Component component = (Component) value;
        Iterator properties = component.getPropertyIterator();
        Component indexComponent = new Component(getBuildingContext().getMetadataCollector(), collection);
        indexComponent.setComponentClassName(component.getComponentClassName());
        while (properties.hasNext()) {
            Property current = (Property) properties.next();
            Property newProperty = new Property();
            newProperty.setCascade(current.getCascade());
            newProperty.setValueGenerationStrategy(current.getValueGenerationStrategy());
            newProperty.setInsertable(false);
            newProperty.setUpdateable(false);
            newProperty.setMetaAttributes(current.getMetaAttributes());
            newProperty.setName(current.getName());
            newProperty.setNaturalIdentifier(false);
            //newProperty.setOptimisticLocked( false );
            newProperty.setOptional(false);
            newProperty.setPersistentClass(current.getPersistentClass());
            newProperty.setPropertyAccessorName(current.getPropertyAccessorName());
            newProperty.setSelectable(current.isSelectable());
            newProperty.setValue(createFormulatedValue(current.getValue(), collection, targetPropertyName, associatedClass, associatedClass, buildingContext));
            indexComponent.addProperty(newProperty);
        }
        return indexComponent;
    } else if (value instanceof SimpleValue) {
        SimpleValue sourceValue = (SimpleValue) value;
        SimpleValue targetValue;
        if (value instanceof ManyToOne) {
            ManyToOne sourceManyToOne = (ManyToOne) sourceValue;
            ManyToOne targetManyToOne = new ManyToOne(getBuildingContext().getMetadataCollector(), collection.getCollectionTable());
            targetManyToOne.setFetchMode(FetchMode.DEFAULT);
            targetManyToOne.setLazy(true);
            //targetValue.setIgnoreNotFound( ); does not make sense for a map key
            targetManyToOne.setReferencedEntityName(sourceManyToOne.getReferencedEntityName());
            targetValue = targetManyToOne;
        } else {
            targetValue = new SimpleValue(getBuildingContext().getMetadataCollector(), collection.getCollectionTable());
            targetValue.copyTypeFrom(sourceValue);
        }
        Iterator columns = sourceValue.getColumnIterator();
        Random random = new Random();
        while (columns.hasNext()) {
            Object current = columns.next();
            Formula formula = new Formula();
            String formulaString;
            if (current instanceof Column) {
                formulaString = ((Column) current).getQuotedName();
            } else if (current instanceof Formula) {
                formulaString = ((Formula) current).getFormula();
            } else {
                throw new AssertionFailure("Unknown element in column iterator: " + current.getClass());
            }
            if (fromAndWhere != null) {
                formulaString = Template.renderWhereStringTemplate(formulaString, "$alias$", new HSQLDialect());
                formulaString = "(select " + formulaString + fromAndWhere + ")";
                formulaString = StringHelper.replace(formulaString, "$alias$", "a" + random.nextInt(16));
            }
            formula.setFormula(formulaString);
            targetValue.addFormula(formula);
        }
        return targetValue;
    } else {
        throw new AssertionFailure("Unknown type encounters for map key: " + value.getClass());
    }
}
Also used : HSQLDialect(org.hibernate.dialect.HSQLDialect) AssertionFailure(org.hibernate.AssertionFailure) DependantValue(org.hibernate.mapping.DependantValue) OneToMany(org.hibernate.mapping.OneToMany) ManyToOne(org.hibernate.mapping.ManyToOne) SimpleValue(org.hibernate.mapping.SimpleValue) Formula(org.hibernate.mapping.Formula) Random(java.util.Random) Selectable(org.hibernate.mapping.Selectable) Ejb3Column(org.hibernate.cfg.Ejb3Column) MapKeyJoinColumn(javax.persistence.MapKeyJoinColumn) Column(org.hibernate.mapping.Column) Ejb3JoinColumn(org.hibernate.cfg.Ejb3JoinColumn) SimpleValue(org.hibernate.mapping.SimpleValue) DependantValue(org.hibernate.mapping.DependantValue) Value(org.hibernate.mapping.Value) ToOne(org.hibernate.mapping.ToOne) ManyToOne(org.hibernate.mapping.ManyToOne) Iterator(java.util.Iterator) AnnotationException(org.hibernate.AnnotationException) Component(org.hibernate.mapping.Component) Property(org.hibernate.mapping.Property) XProperty(org.hibernate.annotations.common.reflection.XProperty)

Example 4 with OneToMany

use of org.hibernate.mapping.OneToMany 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 5 with OneToMany

use of org.hibernate.mapping.OneToMany 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

OneToMany (org.hibernate.mapping.OneToMany)7 ManyToOne (org.hibernate.mapping.ManyToOne)4 Component (org.hibernate.mapping.Component)3 PersistentClass (org.hibernate.mapping.PersistentClass)3 SimpleValue (org.hibernate.mapping.SimpleValue)3 Value (org.hibernate.mapping.Value)3 AnnotationException (org.hibernate.AnnotationException)2 Collection (org.hibernate.mapping.Collection)2 DependantValue (org.hibernate.mapping.DependantValue)2 IndexBackref (org.hibernate.mapping.IndexBackref)2 List (org.hibernate.mapping.List)2 Map (org.hibernate.mapping.Map)2 ComponentType (org.hibernate.type.ComponentType)2 Member (java.lang.reflect.Member)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Iterator (java.util.Iterator)1 Random (java.util.Random)1 MapKeyJoinColumn (javax.persistence.MapKeyJoinColumn)1 Type (javax.persistence.metamodel.Type)1 AssertionFailure (org.hibernate.AssertionFailure)1