Search in sources :

Example 6 with Join

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

the class CollectionBinder method bindOneToManySecondPass.

protected void bindOneToManySecondPass(Collection collection, Map persistentClasses, Ejb3JoinColumn[] fkJoinColumns, XClass collectionType, boolean cascadeDeleteEnabled, boolean ignoreNotFound, MetadataBuildingContext buildingContext, Map<XClass, InheritanceState> inheritanceStatePerClass) {
    final boolean debugEnabled = LOG.isDebugEnabled();
    if (debugEnabled) {
        LOG.debugf("Binding a OneToMany: %s.%s through a foreign key", propertyHolder.getEntityName(), propertyName);
    }
    if (buildingContext == null) {
        throw new AssertionFailure("CollectionSecondPass for oneToMany should not be called with null mappings");
    }
    org.hibernate.mapping.OneToMany oneToMany = new org.hibernate.mapping.OneToMany(buildingContext.getMetadataCollector(), collection.getOwner());
    collection.setElement(oneToMany);
    oneToMany.setReferencedEntityName(collectionType.getName());
    oneToMany.setIgnoreNotFound(ignoreNotFound);
    String assocClass = oneToMany.getReferencedEntityName();
    PersistentClass associatedClass = (PersistentClass) persistentClasses.get(assocClass);
    if (jpaOrderBy != null) {
        final String orderByFragment = buildOrderByClauseFromHql(jpaOrderBy.value(), associatedClass, collection.getRole());
        if (StringHelper.isNotEmpty(orderByFragment)) {
            collection.setOrderBy(orderByFragment);
        }
    }
    Map<String, Join> joins = buildingContext.getMetadataCollector().getJoins(assocClass);
    if (associatedClass == null) {
        throw new MappingException(String.format("Association [%s] for entity [%s] references unmapped class [%s]", propertyName, propertyHolder.getClassName(), assocClass));
    }
    oneToMany.setAssociatedClass(associatedClass);
    for (Ejb3JoinColumn column : fkJoinColumns) {
        column.setPersistentClass(associatedClass, joins, inheritanceStatePerClass);
        column.setJoins(joins);
        collection.setCollectionTable(column.getTable());
    }
    if (debugEnabled) {
        LOG.debugf("Mapping collection: %s -> %s", collection.getRole(), collection.getCollectionTable().getName());
    }
    bindFilters(false);
    bindCollectionSecondPass(collection, null, fkJoinColumns, cascadeDeleteEnabled, property, propertyHolder, buildingContext);
    if (!collection.isInverse() && !collection.getKey().isNullable()) {
        // for non-inverse one-to-many, with a not-null fk, add a backref!
        String entityName = oneToMany.getReferencedEntityName();
        PersistentClass referenced = buildingContext.getMetadataCollector().getEntityBinding(entityName);
        Backref prop = new Backref();
        prop.setName('_' + fkJoinColumns[0].getPropertyName() + '_' + fkJoinColumns[0].getLogicalColumnName() + "Backref");
        prop.setUpdateable(false);
        prop.setSelectable(false);
        prop.setCollectionRole(collection.getRole());
        prop.setEntityName(collection.getOwner().getEntityName());
        prop.setValue(collection.getKey());
        referenced.addProperty(prop);
    }
}
Also used : AssertionFailure(org.hibernate.annotations.common.AssertionFailure) Join(org.hibernate.mapping.Join) Backref(org.hibernate.mapping.Backref) OneToMany(javax.persistence.OneToMany) MappingException(org.hibernate.MappingException) Ejb3JoinColumn(org.hibernate.cfg.Ejb3JoinColumn) PersistentClass(org.hibernate.mapping.PersistentClass)

Example 7 with Join

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

the class CollectionBinder method bindManytoManyInverseFk.

/**
	 * bind the inverse FK of a ManyToMany
	 * If we are in a mappedBy case, read the columns from the associated
	 * collection element
	 * Otherwise delegates to the usual algorithm
	 */
public static void bindManytoManyInverseFk(PersistentClass referencedEntity, Ejb3JoinColumn[] columns, SimpleValue value, boolean unique, MetadataBuildingContext buildingContext) {
    final String mappedBy = columns[0].getMappedBy();
    if (StringHelper.isNotEmpty(mappedBy)) {
        final Property property = referencedEntity.getRecursiveProperty(mappedBy);
        Iterator mappedByColumns;
        if (property.getValue() instanceof Collection) {
            mappedByColumns = ((Collection) property.getValue()).getKey().getColumnIterator();
        } else {
            //find the appropriate reference key, can be in a join
            Iterator joinsIt = referencedEntity.getJoinIterator();
            KeyValue key = null;
            while (joinsIt.hasNext()) {
                Join join = (Join) joinsIt.next();
                if (join.containsProperty(property)) {
                    key = join.getKey();
                    break;
                }
            }
            if (key == null)
                key = property.getPersistentClass().getIdentifier();
            mappedByColumns = key.getColumnIterator();
        }
        while (mappedByColumns.hasNext()) {
            Column column = (Column) mappedByColumns.next();
            columns[0].linkValueUsingAColumnCopy(column, value);
        }
        String referencedPropertyName = buildingContext.getMetadataCollector().getPropertyReferencedAssociation("inverse__" + referencedEntity.getEntityName(), mappedBy);
        if (referencedPropertyName != null) {
            //TODO always a many to one?
            ((ManyToOne) value).setReferencedPropertyName(referencedPropertyName);
            buildingContext.getMetadataCollector().addUniquePropertyReference(referencedEntity.getEntityName(), referencedPropertyName);
        }
        ((ManyToOne) value).setReferenceToPrimaryKey(referencedPropertyName == null);
        value.createForeignKey();
    } else {
        BinderHelper.createSyntheticPropertyReference(columns, referencedEntity, null, value, true, buildingContext);
        TableBinder.bindFk(referencedEntity, null, columns, value, unique, buildingContext);
    }
}
Also used : KeyValue(org.hibernate.mapping.KeyValue) Ejb3Column(org.hibernate.cfg.Ejb3Column) MapKeyColumn(javax.persistence.MapKeyColumn) Column(org.hibernate.mapping.Column) IndexColumn(org.hibernate.cfg.IndexColumn) JoinColumn(javax.persistence.JoinColumn) Ejb3JoinColumn(org.hibernate.cfg.Ejb3JoinColumn) Iterator(java.util.Iterator) LazyCollection(org.hibernate.annotations.LazyCollection) Collection(org.hibernate.mapping.Collection) ElementCollection(javax.persistence.ElementCollection) Join(org.hibernate.mapping.Join) Property(org.hibernate.mapping.Property) XProperty(org.hibernate.annotations.common.reflection.XProperty) ManyToOne(org.hibernate.mapping.ManyToOne)

Example 8 with Join

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

the class MapBinder method bindKeyFromAssociationTable.

private void bindKeyFromAssociationTable(XClass collType, Map persistentClasses, String mapKeyPropertyName, XProperty property, boolean isEmbedded, MetadataBuildingContext buildingContext, Ejb3Column[] mapKeyColumns, Ejb3JoinColumn[] mapKeyManyToManyColumns, String targetPropertyName) {
    if (mapKeyPropertyName != null) {
        //this is an EJB3 @MapKey
        PersistentClass associatedClass = (PersistentClass) persistentClasses.get(collType.getName());
        if (associatedClass == null)
            throw new AnnotationException("Associated class not found: " + collType);
        Property mapProperty = BinderHelper.findPropertyByName(associatedClass, mapKeyPropertyName);
        if (mapProperty == null) {
            throw new AnnotationException("Map key property not found: " + collType + "." + mapKeyPropertyName);
        }
        org.hibernate.mapping.Map map = (org.hibernate.mapping.Map) this.collection;
        // HHH-11005 - if InheritanceType.JOINED then need to find class defining the column
        InheritanceState inheritanceState = inheritanceStatePerClass.get(collType);
        PersistentClass targetPropertyPersistentClass = InheritanceType.JOINED.equals(inheritanceState.getType()) ? mapProperty.getPersistentClass() : associatedClass;
        Value indexValue = createFormulatedValue(mapProperty.getValue(), map, targetPropertyName, associatedClass, targetPropertyPersistentClass, buildingContext);
        map.setIndex(indexValue);
    } else {
        //this is a true Map mapping
        //TODO ugly copy/pastle from CollectionBinder.bindManyToManySecondPass
        String mapKeyType;
        Class target = void.class;
        /*
			 * target has priority over reflection for the map key type
			 * JPA 2 has priority
			 */
        if (property.isAnnotationPresent(MapKeyClass.class)) {
            target = property.getAnnotation(MapKeyClass.class).value();
        }
        if (!void.class.equals(target)) {
            mapKeyType = target.getName();
        } else {
            mapKeyType = property.getMapKey().getName();
        }
        PersistentClass collectionEntity = (PersistentClass) persistentClasses.get(mapKeyType);
        boolean isIndexOfEntities = collectionEntity != null;
        ManyToOne element = null;
        org.hibernate.mapping.Map mapValue = (org.hibernate.mapping.Map) this.collection;
        if (isIndexOfEntities) {
            element = new ManyToOne(buildingContext.getMetadataCollector(), mapValue.getCollectionTable());
            mapValue.setIndex(element);
            element.setReferencedEntityName(mapKeyType);
            //element.setFetchMode( fetchMode );
            //element.setLazy( fetchMode != FetchMode.JOIN );
            //make the second join non lazy
            element.setFetchMode(FetchMode.JOIN);
            element.setLazy(false);
        //does not make sense for a map key element.setIgnoreNotFound( ignoreNotFound );
        } else {
            XClass keyXClass;
            AnnotatedClassType classType;
            if (BinderHelper.PRIMITIVE_NAMES.contains(mapKeyType)) {
                classType = AnnotatedClassType.NONE;
                keyXClass = null;
            } else {
                try {
                    keyXClass = buildingContext.getBuildingOptions().getReflectionManager().classForName(mapKeyType);
                } catch (ClassLoadingException e) {
                    throw new AnnotationException("Unable to find class: " + mapKeyType, e);
                }
                classType = buildingContext.getMetadataCollector().getClassType(keyXClass);
                // force in case of attribute override naming the key
                if (isEmbedded || mappingDefinedAttributeOverrideOnMapKey(property)) {
                    classType = AnnotatedClassType.EMBEDDABLE;
                }
            }
            CollectionPropertyHolder holder = PropertyHolderBuilder.buildPropertyHolder(mapValue, StringHelper.qualify(mapValue.getRole(), "mapkey"), keyXClass, property, propertyHolder, buildingContext);
            // 'propertyHolder' is the PropertyHolder for the owner of the collection
            // 'holder' is the CollectionPropertyHolder.
            // 'property' is the collection XProperty
            propertyHolder.startingProperty(property);
            holder.prepare(property);
            PersistentClass owner = mapValue.getOwner();
            AccessType accessType;
            // String accessType = access != null ? access.value() : null;
            if (owner.getIdentifierProperty() != null) {
                accessType = owner.getIdentifierProperty().getPropertyAccessorName().equals("property") ? AccessType.PROPERTY : AccessType.FIELD;
            } else if (owner.getIdentifierMapper() != null && owner.getIdentifierMapper().getPropertySpan() > 0) {
                Property prop = (Property) owner.getIdentifierMapper().getPropertyIterator().next();
                accessType = prop.getPropertyAccessorName().equals("property") ? AccessType.PROPERTY : AccessType.FIELD;
            } else {
                throw new AssertionFailure("Unable to guess collection property accessor name");
            }
            if (AnnotatedClassType.EMBEDDABLE.equals(classType)) {
                EntityBinder entityBinder = new EntityBinder();
                PropertyData inferredData;
                if (isHibernateExtensionMapping()) {
                    inferredData = new PropertyPreloadedData(AccessType.PROPERTY, "index", keyXClass);
                } else {
                    //"key" is the JPA 2 prefix for map keys
                    inferredData = new PropertyPreloadedData(AccessType.PROPERTY, "key", keyXClass);
                }
                //TODO be smart with isNullable
                Component component = AnnotationBinder.fillComponent(holder, inferredData, accessType, true, entityBinder, false, false, true, buildingContext, inheritanceStatePerClass);
                mapValue.setIndex(component);
            } else {
                SimpleValueBinder elementBinder = new SimpleValueBinder();
                elementBinder.setBuildingContext(buildingContext);
                elementBinder.setReturnedClassName(mapKeyType);
                Ejb3Column[] elementColumns = mapKeyColumns;
                if (elementColumns == null || elementColumns.length == 0) {
                    elementColumns = new Ejb3Column[1];
                    Ejb3Column column = new Ejb3Column();
                    column.setImplicit(false);
                    column.setNullable(true);
                    column.setLength(Ejb3Column.DEFAULT_COLUMN_LENGTH);
                    column.setLogicalColumnName(Collection.DEFAULT_KEY_COLUMN_NAME);
                    //TODO create an EMPTY_JOINS collection
                    column.setJoins(new HashMap<String, Join>());
                    column.setBuildingContext(buildingContext);
                    column.bind();
                    elementColumns[0] = column;
                }
                //override the table
                for (Ejb3Column column : elementColumns) {
                    column.setTable(mapValue.getCollectionTable());
                }
                elementBinder.setColumns(elementColumns);
                //do not call setType as it extract the type from @Type
                //the algorithm generally does not apply for map key anyway
                elementBinder.setKey(true);
                elementBinder.setType(property, keyXClass, this.collection.getOwnerEntityName(), holder.mapKeyAttributeConverterDescriptor(property, keyXClass));
                elementBinder.setPersistentClassName(propertyHolder.getEntityName());
                elementBinder.setAccessType(accessType);
                mapValue.setIndex(elementBinder.make());
            }
        }
        //FIXME pass the Index Entity JoinColumns
        if (!collection.isOneToMany()) {
            //index column shoud not be null
            for (Ejb3JoinColumn col : mapKeyManyToManyColumns) {
                col.forceNotNull();
            }
        }
        if (element != null) {
            final javax.persistence.ForeignKey foreignKey = getMapKeyForeignKey(property);
            if (foreignKey != null) {
                if (foreignKey.value() == ConstraintMode.NO_CONSTRAINT) {
                    element.setForeignKeyName("none");
                } else {
                    element.setForeignKeyName(StringHelper.nullIfEmpty(foreignKey.name()));
                    element.setForeignKeyDefinition(StringHelper.nullIfEmpty(foreignKey.foreignKeyDefinition()));
                }
            }
        }
        if (isIndexOfEntities) {
            bindManytoManyInverseFk(collectionEntity, mapKeyManyToManyColumns, element, //a map key column has no unique constraint
            false, buildingContext);
        }
    }
}
Also used : PropertyData(org.hibernate.cfg.PropertyData) PropertyPreloadedData(org.hibernate.cfg.PropertyPreloadedData) XClass(org.hibernate.annotations.common.reflection.XClass) ManyToOne(org.hibernate.mapping.ManyToOne) AnnotationException(org.hibernate.AnnotationException) Ejb3Column(org.hibernate.cfg.Ejb3Column) Component(org.hibernate.mapping.Component) Property(org.hibernate.mapping.Property) XProperty(org.hibernate.annotations.common.reflection.XProperty) AccessType(org.hibernate.cfg.AccessType) PersistentClass(org.hibernate.mapping.PersistentClass) CollectionPropertyHolder(org.hibernate.cfg.CollectionPropertyHolder) AssertionFailure(org.hibernate.AssertionFailure) ClassLoadingException(org.hibernate.annotations.common.reflection.ClassLoadingException) Join(org.hibernate.mapping.Join) InheritanceState(org.hibernate.cfg.InheritanceState) SimpleValue(org.hibernate.mapping.SimpleValue) DependantValue(org.hibernate.mapping.DependantValue) Value(org.hibernate.mapping.Value) MapKeyClass(javax.persistence.MapKeyClass) PersistentClass(org.hibernate.mapping.PersistentClass) XClass(org.hibernate.annotations.common.reflection.XClass) Ejb3JoinColumn(org.hibernate.cfg.Ejb3JoinColumn) Map(java.util.Map) HashMap(java.util.HashMap) AnnotatedClassType(org.hibernate.cfg.AnnotatedClassType)

Example 9 with Join

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

the class ClassPropertyHolder method addProperty.

public void addProperty(Property prop, XClass declaringClass) {
    if (prop.getValue() instanceof Component) {
        //TODO handle quote and non quote table comparison
        String tableName = prop.getValue().getTable().getName();
        if (getJoinsPerRealTableName().containsKey(tableName)) {
            final Join join = getJoinsPerRealTableName().get(tableName);
            addPropertyToJoin(prop, declaringClass, join);
        } else {
            addPropertyToPersistentClass(prop, declaringClass);
        }
    } else {
        addPropertyToPersistentClass(prop, declaringClass);
    }
}
Also used : Join(org.hibernate.mapping.Join) Component(org.hibernate.mapping.Component)

Example 10 with Join

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

the class ClassPropertyHolder method addJoin.

public Join addJoin(JoinTable joinTableAnn, boolean noDelayInPkColumnCreation) {
    Join join = entityBinder.addJoin(joinTableAnn, this, noDelayInPkColumnCreation);
    this.joins = entityBinder.getSecondaryTables();
    return join;
}
Also used : Join(org.hibernate.mapping.Join)

Aggregations

Join (org.hibernate.mapping.Join)22 Iterator (java.util.Iterator)8 PersistentClass (org.hibernate.mapping.PersistentClass)7 Property (org.hibernate.mapping.Property)7 Column (org.hibernate.mapping.Column)6 AnnotationException (org.hibernate.AnnotationException)5 AssertionFailure (org.hibernate.AssertionFailure)5 Component (org.hibernate.mapping.Component)5 Table (org.hibernate.mapping.Table)5 HashMap (java.util.HashMap)4 ManyToOne (org.hibernate.mapping.ManyToOne)4 SimpleValue (org.hibernate.mapping.SimpleValue)4 JoinTable (javax.persistence.JoinTable)3 XProperty (org.hibernate.annotations.common.reflection.XProperty)3 Ejb3JoinColumn (org.hibernate.cfg.Ejb3JoinColumn)3 SyntheticProperty (org.hibernate.mapping.SyntheticProperty)3 Test (org.junit.Test)3 ElementCollection (javax.persistence.ElementCollection)2 JoinColumn (javax.persistence.JoinColumn)2 MapKeyColumn (javax.persistence.MapKeyColumn)2