Search in sources :

Example 6 with Ejb3JoinColumn

use of org.hibernate.cfg.Ejb3JoinColumn in project hibernate-orm by hibernate.

the class TableBinder method bindFk.

public static void bindFk(PersistentClass referencedEntity, PersistentClass destinationEntity, Ejb3JoinColumn[] columns, SimpleValue value, boolean unique, MetadataBuildingContext buildingContext) {
    PersistentClass associatedClass;
    if (destinationEntity != null) {
        //overridden destination
        associatedClass = destinationEntity;
    } else {
        associatedClass = columns[0].getPropertyHolder() == null ? null : columns[0].getPropertyHolder().getPersistentClass();
    }
    final String mappedByProperty = columns[0].getMappedBy();
    if (StringHelper.isNotEmpty(mappedByProperty)) {
        /**
			 * Get the columns of the mapped-by property
			 * copy them and link the copy to the actual value
			 */
        LOG.debugf("Retrieving property %s.%s", associatedClass.getEntityName(), mappedByProperty);
        final Property property = associatedClass.getRecursiveProperty(columns[0].getMappedBy());
        Iterator mappedByColumns;
        if (property.getValue() instanceof Collection) {
            Collection collection = ((Collection) property.getValue());
            Value element = collection.getElement();
            if (element == null) {
                throw new AnnotationException("Illegal use of mappedBy on both sides of the relationship: " + associatedClass.getEntityName() + "." + mappedByProperty);
            }
            mappedByColumns = element.getColumnIterator();
        } else {
            mappedByColumns = property.getValue().getColumnIterator();
        }
        while (mappedByColumns.hasNext()) {
            Column column = (Column) mappedByColumns.next();
            columns[0].overrideFromReferencedColumnIfNecessary(column);
            columns[0].linkValueUsingAColumnCopy(column, value);
        }
    } else if (columns[0].isImplicit()) {
        /**
			 * if columns are implicit, then create the columns based on the
			 * referenced entity id columns
			 */
        Iterator idColumns;
        if (referencedEntity instanceof JoinedSubclass) {
            idColumns = referencedEntity.getKey().getColumnIterator();
        } else {
            idColumns = referencedEntity.getIdentifier().getColumnIterator();
        }
        while (idColumns.hasNext()) {
            Column column = (Column) idColumns.next();
            columns[0].linkValueUsingDefaultColumnNaming(column, referencedEntity, value);
            columns[0].overrideFromReferencedColumnIfNecessary(column);
        }
    } else {
        int fkEnum = Ejb3JoinColumn.checkReferencedColumnsType(columns, referencedEntity, buildingContext);
        if (Ejb3JoinColumn.NON_PK_REFERENCE == fkEnum) {
            String referencedPropertyName;
            if (value instanceof ToOne) {
                referencedPropertyName = ((ToOne) value).getReferencedPropertyName();
            } else if (value instanceof DependantValue) {
                String propertyName = columns[0].getPropertyName();
                if (propertyName != null) {
                    Collection collection = (Collection) referencedEntity.getRecursiveProperty(propertyName).getValue();
                    referencedPropertyName = collection.getReferencedPropertyName();
                } else {
                    throw new AnnotationException("SecondaryTable JoinColumn cannot reference a non primary key");
                }
            } else {
                throw new AssertionFailure("Do a property ref on an unexpected Value type: " + value.getClass().getName());
            }
            if (referencedPropertyName == null) {
                throw new AssertionFailure("No property ref found while expected");
            }
            Property synthProp = referencedEntity.getReferencedProperty(referencedPropertyName);
            if (synthProp == null) {
                throw new AssertionFailure("Cannot find synthProp: " + referencedEntity.getEntityName() + "." + referencedPropertyName);
            }
            linkJoinColumnWithValueOverridingNameIfImplicit(referencedEntity, synthProp.getColumnIterator(), columns, value);
        } else {
            if (Ejb3JoinColumn.NO_REFERENCE == fkEnum) {
                //implicit case, we hope PK and FK columns are in the same order
                if (columns.length != referencedEntity.getIdentifier().getColumnSpan()) {
                    throw new AnnotationException("A Foreign key refering " + referencedEntity.getEntityName() + " from " + associatedClass.getEntityName() + " has the wrong number of column. should be " + referencedEntity.getIdentifier().getColumnSpan());
                }
                linkJoinColumnWithValueOverridingNameIfImplicit(referencedEntity, referencedEntity.getIdentifier().getColumnIterator(), columns, value);
            } else {
                //explicit referencedColumnName
                Iterator idColItr = referencedEntity.getKey().getColumnIterator();
                org.hibernate.mapping.Column col;
                //works cause the pk has to be on the primary table
                Table table = referencedEntity.getTable();
                if (!idColItr.hasNext()) {
                    LOG.debug("No column in the identifier!");
                }
                while (idColItr.hasNext()) {
                    boolean match = false;
                    //for each PK column, find the associated FK column.
                    col = (org.hibernate.mapping.Column) idColItr.next();
                    for (Ejb3JoinColumn joinCol : columns) {
                        String referencedColumn = joinCol.getReferencedColumn();
                        referencedColumn = buildingContext.getMetadataCollector().getPhysicalColumnName(table, referencedColumn);
                        //In JPA 2 referencedColumnName is case insensitive
                        if (referencedColumn.equalsIgnoreCase(col.getQuotedName(buildingContext.getMetadataCollector().getDatabase().getJdbcEnvironment().getDialect()))) {
                            //proper join column
                            if (joinCol.isNameDeferred()) {
                                joinCol.linkValueUsingDefaultColumnNaming(col, referencedEntity, value);
                            } else {
                                joinCol.linkWithValue(value);
                            }
                            joinCol.overrideFromReferencedColumnIfNecessary(col);
                            match = true;
                            break;
                        }
                    }
                    if (!match) {
                        throw new AnnotationException("Column name " + col.getName() + " of " + referencedEntity.getEntityName() + " not found in JoinColumns.referencedColumnName");
                    }
                }
            }
        }
    }
    value.createForeignKey();
    if (unique) {
        createUniqueConstraint(value);
    }
}
Also used : AssertionFailure(org.hibernate.AssertionFailure) Table(org.hibernate.mapping.Table) DependantValue(org.hibernate.mapping.DependantValue) Column(org.hibernate.mapping.Column) Ejb3JoinColumn(org.hibernate.cfg.Ejb3JoinColumn) Iterator(java.util.Iterator) SimpleValue(org.hibernate.mapping.SimpleValue) DependantValue(org.hibernate.mapping.DependantValue) Value(org.hibernate.mapping.Value) ToOne(org.hibernate.mapping.ToOne) Collection(org.hibernate.mapping.Collection) AnnotationException(org.hibernate.AnnotationException) Ejb3JoinColumn(org.hibernate.cfg.Ejb3JoinColumn) JoinedSubclass(org.hibernate.mapping.JoinedSubclass) Property(org.hibernate.mapping.Property) PersistentClass(org.hibernate.mapping.PersistentClass)

Example 7 with Ejb3JoinColumn

use of org.hibernate.cfg.Ejb3JoinColumn in project hibernate-orm by hibernate.

the class EntityBinder method createPrimaryColumnsToSecondaryTable.

private void createPrimaryColumnsToSecondaryTable(Object uncastedColumn, PropertyHolder propertyHolder, Join join) {
    Ejb3JoinColumn[] ejb3JoinColumns;
    PrimaryKeyJoinColumn[] pkColumnsAnn = null;
    JoinColumn[] joinColumnsAnn = null;
    if (uncastedColumn instanceof PrimaryKeyJoinColumn[]) {
        pkColumnsAnn = (PrimaryKeyJoinColumn[]) uncastedColumn;
    }
    if (uncastedColumn instanceof JoinColumn[]) {
        joinColumnsAnn = (JoinColumn[]) uncastedColumn;
    }
    if (pkColumnsAnn == null && joinColumnsAnn == null) {
        ejb3JoinColumns = new Ejb3JoinColumn[1];
        ejb3JoinColumns[0] = Ejb3JoinColumn.buildJoinColumn(null, null, persistentClass.getIdentifier(), secondaryTables, propertyHolder, context);
    } else {
        int nbrOfJoinColumns = pkColumnsAnn != null ? pkColumnsAnn.length : joinColumnsAnn.length;
        if (nbrOfJoinColumns == 0) {
            ejb3JoinColumns = new Ejb3JoinColumn[1];
            ejb3JoinColumns[0] = Ejb3JoinColumn.buildJoinColumn(null, null, persistentClass.getIdentifier(), secondaryTables, propertyHolder, context);
        } else {
            ejb3JoinColumns = new Ejb3JoinColumn[nbrOfJoinColumns];
            if (pkColumnsAnn != null) {
                for (int colIndex = 0; colIndex < nbrOfJoinColumns; colIndex++) {
                    ejb3JoinColumns[colIndex] = Ejb3JoinColumn.buildJoinColumn(pkColumnsAnn[colIndex], null, persistentClass.getIdentifier(), secondaryTables, propertyHolder, context);
                }
            } else {
                for (int colIndex = 0; colIndex < nbrOfJoinColumns; colIndex++) {
                    ejb3JoinColumns[colIndex] = Ejb3JoinColumn.buildJoinColumn(null, joinColumnsAnn[colIndex], persistentClass.getIdentifier(), secondaryTables, propertyHolder, context);
                }
            }
        }
    }
    for (Ejb3JoinColumn joinColumn : ejb3JoinColumns) {
        joinColumn.forceNotNull();
    }
    bindJoinToPersistentClass(join, ejb3JoinColumns, context);
}
Also used : PrimaryKeyJoinColumn(javax.persistence.PrimaryKeyJoinColumn) JoinColumn(javax.persistence.JoinColumn) Ejb3JoinColumn(org.hibernate.cfg.Ejb3JoinColumn) PrimaryKeyJoinColumn(javax.persistence.PrimaryKeyJoinColumn) Ejb3JoinColumn(org.hibernate.cfg.Ejb3JoinColumn)

Example 8 with Ejb3JoinColumn

use of org.hibernate.cfg.Ejb3JoinColumn in project hibernate-orm by hibernate.

the class TableBinder method linkJoinColumnWithValueOverridingNameIfImplicit.

public static void linkJoinColumnWithValueOverridingNameIfImplicit(PersistentClass referencedEntity, Iterator columnIterator, Ejb3JoinColumn[] columns, SimpleValue value) {
    for (Ejb3JoinColumn joinCol : columns) {
        Column synthCol = (Column) columnIterator.next();
        if (joinCol.isNameDeferred()) {
            //this has to be the default value
            joinCol.linkValueUsingDefaultColumnNaming(synthCol, referencedEntity, value);
        } else {
            joinCol.linkWithValue(value);
            joinCol.overrideFromReferencedColumnIfNecessary(synthCol);
        }
    }
}
Also used : Column(org.hibernate.mapping.Column) Ejb3JoinColumn(org.hibernate.cfg.Ejb3JoinColumn) Ejb3JoinColumn(org.hibernate.cfg.Ejb3JoinColumn)

Aggregations

Ejb3JoinColumn (org.hibernate.cfg.Ejb3JoinColumn)8 JoinColumn (javax.persistence.JoinColumn)4 PersistentClass (org.hibernate.mapping.PersistentClass)4 Property (org.hibernate.mapping.Property)4 AnnotationException (org.hibernate.AnnotationException)3 XProperty (org.hibernate.annotations.common.reflection.XProperty)3 Ejb3Column (org.hibernate.cfg.Ejb3Column)3 Collection (org.hibernate.mapping.Collection)3 Column (org.hibernate.mapping.Column)3 DependantValue (org.hibernate.mapping.DependantValue)3 Join (org.hibernate.mapping.Join)3 ManyToOne (org.hibernate.mapping.ManyToOne)3 HashMap (java.util.HashMap)2 Iterator (java.util.Iterator)2 CollectionTable (javax.persistence.CollectionTable)2 ConstraintMode (javax.persistence.ConstraintMode)2 ElementCollection (javax.persistence.ElementCollection)2 JoinTable (javax.persistence.JoinTable)2 OneToMany (javax.persistence.OneToMany)2 AssertionFailure (org.hibernate.AssertionFailure)2