Search in sources :

Example 21 with JoinedSubclass

use of org.hibernate.mapping.JoinedSubclass 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 22 with JoinedSubclass

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

the class ModelBinder method bindJoinedSubclassEntities.

private void bindJoinedSubclassEntities(AbstractEntitySourceImpl entitySource, PersistentClass superEntityDescriptor) {
    for (IdentifiableTypeSource subType : entitySource.getSubTypes()) {
        final JoinedSubclass subEntityDescriptor = new JoinedSubclass(superEntityDescriptor, metadataBuildingContext);
        bindJoinedSubclassEntity((JoinedSubclassEntitySourceImpl) subType, subEntityDescriptor);
        superEntityDescriptor.addSubclass(subEntityDescriptor);
        entitySource.getLocalMetadataBuildingContext().getMetadataCollector().addEntityBinding(subEntityDescriptor);
    }
}
Also used : IdentifiableTypeSource(org.hibernate.boot.model.source.spi.IdentifiableTypeSource) JoinedSubclass(org.hibernate.mapping.JoinedSubclass)

Example 23 with JoinedSubclass

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

the class IdTableHelper method needsIdTable.

public boolean needsIdTable(PersistentClass entityBinding) {
    // need id table if the entity has secondary tables (joins)
    if (entityBinding.getJoinClosureSpan() > 0) {
        return true;
    }
    // need an id table if the entity is part of either a JOINED or UNION inheritance
    // hierarchy.  We do not allow inheritance strategy mixing, so work on that assumption
    // here...
    final RootClass rootEntityBinding = entityBinding.getRootClass();
    final Iterator itr = rootEntityBinding.getSubclassIterator();
    if (itr.hasNext()) {
        final Subclass subclassEntityBinding = (Subclass) itr.next();
        if (subclassEntityBinding instanceof JoinedSubclass || subclassEntityBinding instanceof UnionSubclass) {
            return true;
        }
    }
    return false;
}
Also used : RootClass(org.hibernate.mapping.RootClass) JoinedSubclass(org.hibernate.mapping.JoinedSubclass) UnionSubclass(org.hibernate.mapping.UnionSubclass) Subclass(org.hibernate.mapping.Subclass) Iterator(java.util.Iterator) UnionSubclass(org.hibernate.mapping.UnionSubclass) JoinedSubclass(org.hibernate.mapping.JoinedSubclass)

Example 24 with JoinedSubclass

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

the class PersistentClassVisitorTest method testProperCallbacks.

@Test
public void testProperCallbacks() {
    PersistentClassVisitorValidator vv = new PersistentClassVisitorValidator();
    new RootClass(metadataBuildingContext).accept(vv);
    new Subclass(new RootClass(metadataBuildingContext), metadataBuildingContext).accept(vv);
    new JoinedSubclass(new RootClass(metadataBuildingContext), metadataBuildingContext).accept(vv);
    new SingleTableSubclass(new RootClass(metadataBuildingContext), metadataBuildingContext).accept(vv);
    new UnionSubclass(new RootClass(metadataBuildingContext), metadataBuildingContext).accept(vv);
}
Also used : RootClass(org.hibernate.mapping.RootClass) JoinedSubclass(org.hibernate.mapping.JoinedSubclass) SingleTableSubclass(org.hibernate.mapping.SingleTableSubclass) UnionSubclass(org.hibernate.mapping.UnionSubclass) Subclass(org.hibernate.mapping.Subclass) UnionSubclass(org.hibernate.mapping.UnionSubclass) JoinedSubclass(org.hibernate.mapping.JoinedSubclass) SingleTableSubclass(org.hibernate.mapping.SingleTableSubclass) Test(org.junit.Test)

Example 25 with JoinedSubclass

use of org.hibernate.mapping.JoinedSubclass in project jbosstools-hibernate by jbosstools.

the class ServiceImpl method newJoinedSubclass.

@Override
public IPersistentClass newJoinedSubclass(IPersistentClass persistentClass) {
    assert persistentClass instanceof IFacade;
    IPersistentClass result = facadeFactory.createPersistentClass(new JoinedSubclass((PersistentClass) ((IFacade) persistentClass).getTarget()));
    ((AbstractPersistentClassFacade) result).setSuperClass(persistentClass);
    return result;
}
Also used : AbstractPersistentClassFacade(org.jboss.tools.hibernate.runtime.common.AbstractPersistentClassFacade) IFacade(org.jboss.tools.hibernate.runtime.common.IFacade) JoinedSubclass(org.hibernate.mapping.JoinedSubclass) IPersistentClass(org.jboss.tools.hibernate.runtime.spi.IPersistentClass) PersistentClass(org.hibernate.mapping.PersistentClass) IPersistentClass(org.jboss.tools.hibernate.runtime.spi.IPersistentClass)

Aggregations

JoinedSubclass (org.hibernate.mapping.JoinedSubclass)49 IPersistentClass (org.jboss.tools.hibernate.runtime.spi.IPersistentClass)33 Test (org.junit.jupiter.api.Test)33 IFacade (org.jboss.tools.hibernate.runtime.common.IFacade)22 RootClass (org.hibernate.mapping.RootClass)14 PersistentClass (org.hibernate.mapping.PersistentClass)13 Table (org.hibernate.mapping.Table)12 AbstractPersistentClassFacade (org.jboss.tools.hibernate.runtime.common.AbstractPersistentClassFacade)11 ITable (org.jboss.tools.hibernate.runtime.spi.ITable)11 Iterator (java.util.Iterator)2 AnnotationException (org.hibernate.AnnotationException)2 DependantValue (org.hibernate.mapping.DependantValue)2 SimpleValue (org.hibernate.mapping.SimpleValue)2 Subclass (org.hibernate.mapping.Subclass)2 UnionSubclass (org.hibernate.mapping.UnionSubclass)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 CollectionTable (javax.persistence.CollectionTable)1 Entity (javax.persistence.Entity)1 JoinTable (javax.persistence.JoinTable)1