Search in sources :

Example 26 with KeyValue

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

the class ToOneFkSecondPass method isInPrimaryKey.

@Override
public boolean isInPrimaryKey() {
    if (entityClassName == null)
        return false;
    final PersistentClass persistentClass = buildingContext.getMetadataCollector().getEntityBinding(entityClassName);
    Property property = persistentClass.getIdentifierProperty();
    if (path == null) {
        return false;
    } else if (property != null) {
        // try explicit identifier property
        return path.startsWith(property.getName() + ".");
    } else {
        // embedded property starts their path with 'id.' See PropertyPreloadedData( ) use when idClass != null in AnnotationSourceProcessor
        if (path.startsWith("id.")) {
            KeyValue valueIdentifier = persistentClass.getIdentifier();
            String localPath = path.substring(3);
            if (valueIdentifier instanceof Component) {
                Iterator it = ((Component) valueIdentifier).getPropertyIterator();
                while (it.hasNext()) {
                    Property idProperty = (Property) it.next();
                    if (localPath.startsWith(idProperty.getName()))
                        return true;
                }
            }
        }
    }
    return false;
}
Also used : KeyValue(org.hibernate.mapping.KeyValue) Iterator(java.util.Iterator) Component(org.hibernate.mapping.Component) Property(org.hibernate.mapping.Property) PersistentClass(org.hibernate.mapping.PersistentClass)

Example 27 with KeyValue

use of org.hibernate.mapping.KeyValue 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 28 with KeyValue

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

the class CollectionBinder method buildCollectionKey.

private static SimpleValue buildCollectionKey(Collection collValue, Ejb3JoinColumn[] joinColumns, boolean cascadeDeleteEnabled, XProperty property, PropertyHolder propertyHolder, MetadataBuildingContext buildingContext) {
    // binding key reference using column
    KeyValue keyVal;
    // has to do that here because the referencedProperty creation happens in a FKSecondPass for Many to one yuk!
    if (joinColumns.length > 0 && StringHelper.isNotEmpty(joinColumns[0].getMappedBy())) {
        String entityName = joinColumns[0].getManyToManyOwnerSideEntityName() != null ? "inverse__" + joinColumns[0].getManyToManyOwnerSideEntityName() : joinColumns[0].getPropertyHolder().getEntityName();
        String propRef = buildingContext.getMetadataCollector().getPropertyReferencedAssociation(entityName, joinColumns[0].getMappedBy());
        if (propRef != null) {
            collValue.setReferencedPropertyName(propRef);
            buildingContext.getMetadataCollector().addPropertyReference(collValue.getOwnerEntityName(), propRef);
        }
    }
    String propRef = collValue.getReferencedPropertyName();
    if (propRef == null) {
        keyVal = collValue.getOwner().getIdentifier();
    } else {
        keyVal = (KeyValue) collValue.getOwner().getReferencedProperty(propRef).getValue();
    }
    DependantValue key = new DependantValue(buildingContext, collValue.getCollectionTable(), keyVal);
    key.setTypeName(null);
    Ejb3Column.checkPropertyConsistency(joinColumns, collValue.getOwnerEntityName());
    key.setNullable(joinColumns.length == 0 || joinColumns[0].isNullable());
    key.setUpdateable(joinColumns.length == 0 || joinColumns[0].isUpdatable());
    key.setCascadeDeleteEnabled(cascadeDeleteEnabled);
    collValue.setKey(key);
    if (property != null) {
        final ForeignKey fk = property.getAnnotation(ForeignKey.class);
        if (fk != null && !BinderHelper.isEmptyAnnotationValue(fk.name())) {
            key.setForeignKeyName(fk.name());
        } else {
            final CollectionTable collectionTableAnn = property.getAnnotation(CollectionTable.class);
            if (collectionTableAnn != null) {
                if (collectionTableAnn.foreignKey().value() == ConstraintMode.NO_CONSTRAINT) {
                    key.setForeignKeyName("none");
                } else {
                    key.setForeignKeyName(StringHelper.nullIfEmpty(collectionTableAnn.foreignKey().name()));
                    key.setForeignKeyDefinition(StringHelper.nullIfEmpty(collectionTableAnn.foreignKey().foreignKeyDefinition()));
                    if (key.getForeignKeyName() == null && key.getForeignKeyDefinition() == null && collectionTableAnn.joinColumns().length == 1) {
                        JoinColumn joinColumn = collectionTableAnn.joinColumns()[0];
                        key.setForeignKeyName(StringHelper.nullIfEmpty(joinColumn.foreignKey().name()));
                        key.setForeignKeyDefinition(StringHelper.nullIfEmpty(joinColumn.foreignKey().foreignKeyDefinition()));
                    }
                }
            } else {
                final JoinTable joinTableAnn = property.getAnnotation(JoinTable.class);
                if (joinTableAnn != null) {
                    String foreignKeyName = joinTableAnn.foreignKey().name();
                    String foreignKeyDefinition = joinTableAnn.foreignKey().foreignKeyDefinition();
                    ConstraintMode foreignKeyValue = joinTableAnn.foreignKey().value();
                    if (joinTableAnn.joinColumns().length != 0) {
                        final JoinColumn joinColumnAnn = joinTableAnn.joinColumns()[0];
                        if ("".equals(foreignKeyName)) {
                            foreignKeyName = joinColumnAnn.foreignKey().name();
                            foreignKeyDefinition = joinColumnAnn.foreignKey().foreignKeyDefinition();
                        }
                        if (foreignKeyValue != ConstraintMode.NO_CONSTRAINT) {
                            foreignKeyValue = joinColumnAnn.foreignKey().value();
                        }
                    }
                    if (foreignKeyValue == ConstraintMode.NO_CONSTRAINT) {
                        key.setForeignKeyName("none");
                    } else {
                        key.setForeignKeyName(StringHelper.nullIfEmpty(foreignKeyName));
                        key.setForeignKeyDefinition(StringHelper.nullIfEmpty(foreignKeyDefinition));
                    }
                } else {
                    final javax.persistence.ForeignKey fkOverride = propertyHolder.getOverriddenForeignKey(StringHelper.qualify(propertyHolder.getPath(), property.getName()));
                    if (fkOverride != null && fkOverride.value() == ConstraintMode.NO_CONSTRAINT) {
                        key.setForeignKeyName("none");
                    } else if (fkOverride != null) {
                        key.setForeignKeyName(StringHelper.nullIfEmpty(fkOverride.name()));
                        key.setForeignKeyDefinition(StringHelper.nullIfEmpty(fkOverride.foreignKeyDefinition()));
                    } else {
                        final JoinColumn joinColumnAnn = property.getAnnotation(JoinColumn.class);
                        if (joinColumnAnn != null) {
                            if (joinColumnAnn.foreignKey().value() == ConstraintMode.NO_CONSTRAINT) {
                                key.setForeignKeyName("none");
                            } else {
                                key.setForeignKeyName(StringHelper.nullIfEmpty(joinColumnAnn.foreignKey().name()));
                                key.setForeignKeyDefinition(StringHelper.nullIfEmpty(joinColumnAnn.foreignKey().foreignKeyDefinition()));
                            }
                        }
                    }
                }
            }
        }
    }
    return key;
}
Also used : KeyValue(org.hibernate.mapping.KeyValue) JoinColumn(javax.persistence.JoinColumn) Ejb3JoinColumn(org.hibernate.cfg.Ejb3JoinColumn) CollectionTable(javax.persistence.CollectionTable) DependantValue(org.hibernate.mapping.DependantValue) ConstraintMode(javax.persistence.ConstraintMode) ForeignKey(org.hibernate.annotations.ForeignKey) JoinTable(javax.persistence.JoinTable) WhereJoinTable(org.hibernate.annotations.WhereJoinTable) FilterJoinTable(org.hibernate.annotations.FilterJoinTable)

Example 29 with KeyValue

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

the class PropertyFactory method buildVersionProperty.

/**
 * Generates a VersionProperty representation for an entity mapping given its
 * version mapping Property.
 *
 * @param property The version mapping Property.
 * @param lazyAvailable Is property lazy loading currently available.
 *
 * @return The appropriate VersionProperty definition.
 */
public static VersionProperty buildVersionProperty(EntityPersister persister, SessionFactoryImplementor sessionFactory, int attributeNumber, Property property, boolean lazyAvailable) {
    String mappedUnsavedValue = ((KeyValue) property.getValue()).getNullValue();
    VersionValue unsavedValue = UnsavedValueFactory.getUnsavedVersionValue(mappedUnsavedValue, getGetter(property), (VersionType) property.getType(), getConstructor(property.getPersistentClass()));
    boolean lazy = lazyAvailable && property.isLazy();
    return new VersionProperty(persister, sessionFactory, attributeNumber, property.getName(), property.getValue().getType(), new BaselineAttributeInformation.Builder().setLazy(lazy).setInsertable(property.isInsertable()).setUpdateable(property.isUpdateable()).setValueGenerationStrategy(property.getValueGenerationStrategy()).setNullable(property.isOptional()).setDirtyCheckable(property.isUpdateable() && !lazy).setVersionable(property.isOptimisticLocked()).setCascadeStyle(property.getCascadeStyle()).createInformation(), unsavedValue);
}
Also used : VersionValue(org.hibernate.engine.spi.VersionValue) KeyValue(org.hibernate.mapping.KeyValue) VersionProperty(org.hibernate.tuple.entity.VersionProperty)

Example 30 with KeyValue

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

the class ValueFacadeTest method testSetKey.

@Test
public void testSetKey() {
    KeyValue keyValueTarget = new SimpleValue();
    IValue keyValueFacade = FACADE_FACTORY.createValue(keyValueTarget);
    Collection collectionTarget = new Bag(null);
    IValue collectionFacade = FACADE_FACTORY.createValue(collectionTarget);
    Assert.assertNull(collectionTarget.getKey());
    collectionFacade.setKey(keyValueFacade);
    Assert.assertSame(keyValueTarget, collectionTarget.getKey());
}
Also used : KeyValue(org.hibernate.mapping.KeyValue) IValue(org.jboss.tools.hibernate.runtime.spi.IValue) Collection(org.hibernate.mapping.Collection) Bag(org.hibernate.mapping.Bag) IdentifierBag(org.hibernate.mapping.IdentifierBag) SimpleValue(org.hibernate.mapping.SimpleValue) Test(org.junit.Test)

Aggregations

KeyValue (org.hibernate.mapping.KeyValue)53 Test (org.junit.jupiter.api.Test)44 IValue (org.jboss.tools.hibernate.runtime.spi.IValue)35 SimpleValue (org.hibernate.mapping.SimpleValue)33 Collection (org.hibernate.mapping.Collection)13 Bag (org.hibernate.mapping.Bag)12 IdentifierBag (org.hibernate.mapping.IdentifierBag)12 Map (org.hibernate.mapping.Map)12 Table (org.hibernate.mapping.Table)12 ITable (org.jboss.tools.hibernate.runtime.spi.ITable)12 Field (java.lang.reflect.Field)11 RootClass (org.hibernate.mapping.RootClass)11 Iterator (java.util.Iterator)3 BasicValue (org.hibernate.mapping.BasicValue)3 Test (org.junit.Test)3 JoinColumn (javax.persistence.JoinColumn)2 Ejb3JoinColumn (org.hibernate.cfg.Ejb3JoinColumn)2 Property (org.hibernate.mapping.Property)2 AbstractValueFacade (org.jboss.tools.hibernate.runtime.common.AbstractValueFacade)2 ArrayList (java.util.ArrayList)1