Search in sources :

Example 1 with CollectionTable

use of javax.persistence.CollectionTable in project CloudStack-archive by CloudStack-extras.

the class SqlGenerator method supportsElementCollection.

protected boolean supportsElementCollection(Field field) {
    ElementCollection otm = field.getAnnotation(ElementCollection.class);
    if (otm.fetch() == FetchType.LAZY) {
        assert (false) : "Doesn't support laz fetch: " + field.getName();
        return false;
    }
    CollectionTable ct = field.getAnnotation(CollectionTable.class);
    if (ct == null) {
        assert (false) : "No collection table sepcified for " + field.getName();
        return false;
    }
    return true;
}
Also used : CollectionTable(javax.persistence.CollectionTable) ElementCollection(javax.persistence.ElementCollection)

Example 2 with CollectionTable

use of javax.persistence.CollectionTable in project hibernate-orm by hibernate.

the class AnnotationBinder method bindJoinedTableAssociation.

// TODO move that to collection binder?
private static void bindJoinedTableAssociation(XProperty property, MetadataBuildingContext buildingContext, EntityBinder entityBinder, CollectionBinder collectionBinder, PropertyHolder propertyHolder, PropertyData inferredData, String mappedBy) {
    TableBinder associationTableBinder = new TableBinder();
    JoinColumn[] annJoins;
    JoinColumn[] annInverseJoins;
    JoinTable assocTable = propertyHolder.getJoinTable(property);
    CollectionTable collectionTable = property.getAnnotation(CollectionTable.class);
    if (assocTable != null || collectionTable != null) {
        final String catalog;
        final String schema;
        final String tableName;
        final UniqueConstraint[] uniqueConstraints;
        final JoinColumn[] joins;
        final JoinColumn[] inverseJoins;
        final javax.persistence.Index[] jpaIndexes;
        // JPA 2 has priority
        if (collectionTable != null) {
            catalog = collectionTable.catalog();
            schema = collectionTable.schema();
            tableName = collectionTable.name();
            uniqueConstraints = collectionTable.uniqueConstraints();
            joins = collectionTable.joinColumns();
            inverseJoins = null;
            jpaIndexes = collectionTable.indexes();
        } else {
            catalog = assocTable.catalog();
            schema = assocTable.schema();
            tableName = assocTable.name();
            uniqueConstraints = assocTable.uniqueConstraints();
            joins = assocTable.joinColumns();
            inverseJoins = assocTable.inverseJoinColumns();
            jpaIndexes = assocTable.indexes();
        }
        collectionBinder.setExplicitAssociationTable(true);
        if (jpaIndexes != null && jpaIndexes.length > 0) {
            associationTableBinder.setJpaIndex(jpaIndexes);
        }
        if (!BinderHelper.isEmptyAnnotationValue(schema)) {
            associationTableBinder.setSchema(schema);
        }
        if (!BinderHelper.isEmptyAnnotationValue(catalog)) {
            associationTableBinder.setCatalog(catalog);
        }
        if (!BinderHelper.isEmptyAnnotationValue(tableName)) {
            associationTableBinder.setName(tableName);
        }
        associationTableBinder.setUniqueConstraints(uniqueConstraints);
        associationTableBinder.setJpaIndex(jpaIndexes);
        // set check constaint in the second pass
        annJoins = joins.length == 0 ? null : joins;
        annInverseJoins = inverseJoins == null || inverseJoins.length == 0 ? null : inverseJoins;
    } else {
        annJoins = null;
        annInverseJoins = null;
    }
    Ejb3JoinColumn[] joinColumns = Ejb3JoinColumn.buildJoinTableJoinColumns(annJoins, entityBinder.getSecondaryTables(), propertyHolder, inferredData.getPropertyName(), mappedBy, buildingContext);
    Ejb3JoinColumn[] inverseJoinColumns = Ejb3JoinColumn.buildJoinTableJoinColumns(annInverseJoins, entityBinder.getSecondaryTables(), propertyHolder, inferredData.getPropertyName(), mappedBy, buildingContext);
    associationTableBinder.setBuildingContext(buildingContext);
    collectionBinder.setTableBinder(associationTableBinder);
    collectionBinder.setJoinColumns(joinColumns);
    collectionBinder.setInverseJoinColumns(inverseJoinColumns);
}
Also used : UniqueConstraint(javax.persistence.UniqueConstraint) Index(org.hibernate.annotations.Index) PrimaryKeyJoinColumn(javax.persistence.PrimaryKeyJoinColumn) MapKeyJoinColumn(javax.persistence.MapKeyJoinColumn) JoinColumn(javax.persistence.JoinColumn) CollectionTable(javax.persistence.CollectionTable) TableBinder(org.hibernate.cfg.annotations.TableBinder) JoinTable(javax.persistence.JoinTable)

Example 3 with CollectionTable

use of javax.persistence.CollectionTable 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 4 with CollectionTable

use of javax.persistence.CollectionTable in project cloudstack by apache.

the class SqlGenerator method supportsElementCollection.

protected boolean supportsElementCollection(Field field) {
    ElementCollection otm = field.getAnnotation(ElementCollection.class);
    if (otm.fetch() == FetchType.LAZY) {
        assert (false) : "Doesn't support laz fetch: " + field.getName();
        return false;
    }
    CollectionTable ct = field.getAnnotation(CollectionTable.class);
    if (ct == null) {
        assert (false) : "No collection table sepcified for " + field.getName();
        return false;
    }
    return true;
}
Also used : CollectionTable(javax.persistence.CollectionTable) ElementCollection(javax.persistence.ElementCollection)

Example 5 with CollectionTable

use of javax.persistence.CollectionTable in project cosmic by MissionCriticalCloud.

the class SqlGenerator method supportsElementCollection.

protected boolean supportsElementCollection(final Field field) {
    final ElementCollection otm = field.getAnnotation(ElementCollection.class);
    if (otm.fetch() == FetchType.LAZY) {
        assert (false) : "Doesn't support laz fetch: " + field.getName();
        return false;
    }
    final CollectionTable ct = field.getAnnotation(CollectionTable.class);
    if (ct == null) {
        assert (false) : "No collection table sepcified for " + field.getName();
        return false;
    }
    return true;
}
Also used : CollectionTable(javax.persistence.CollectionTable) ElementCollection(javax.persistence.ElementCollection)

Aggregations

CollectionTable (javax.persistence.CollectionTable)7 ElementCollection (javax.persistence.ElementCollection)3 JoinColumn (javax.persistence.JoinColumn)3 JoinTable (javax.persistence.JoinTable)2 MapKeyJoinColumn (javax.persistence.MapKeyJoinColumn)2 UniqueConstraint (javax.persistence.UniqueConstraint)2 Test (org.junit.Test)2 ConstraintMode (javax.persistence.ConstraintMode)1 PrimaryKeyJoinColumn (javax.persistence.PrimaryKeyJoinColumn)1 FilterJoinTable (org.hibernate.annotations.FilterJoinTable)1 ForeignKey (org.hibernate.annotations.ForeignKey)1 Index (org.hibernate.annotations.Index)1 WhereJoinTable (org.hibernate.annotations.WhereJoinTable)1 Ejb3JoinColumn (org.hibernate.cfg.Ejb3JoinColumn)1 TableBinder (org.hibernate.cfg.annotations.TableBinder)1 DependantValue (org.hibernate.mapping.DependantValue)1 KeyValue (org.hibernate.mapping.KeyValue)1