Search in sources :

Example 11 with JoinColumns

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

the class AnnotationBinder method bindManyToOne.

private static void bindManyToOne(String cascadeStrategy, Ejb3JoinColumn[] columns, boolean optional, boolean ignoreNotFound, boolean cascadeOnDelete, XClass targetEntity, PropertyHolder propertyHolder, PropertyData inferredData, boolean unique, boolean isIdentifierMapper, boolean inSecondPass, PropertyBinder propertyBinder, MetadataBuildingContext context) {
    // All FK columns should be in the same table
    org.hibernate.mapping.ManyToOne value = new org.hibernate.mapping.ManyToOne(context, columns[0].getTable());
    // This is a @OneToOne mapped to a physical o.h.mapping.ManyToOne
    if (unique) {
        value.markAsLogicalOneToOne();
    }
    value.setReferencedEntityName(ToOneBinder.getReferenceEntityName(inferredData, targetEntity, context));
    final XProperty property = inferredData.getProperty();
    defineFetchingStrategy(value, property);
    // value.setFetchMode( fetchMode );
    value.setIgnoreNotFound(ignoreNotFound);
    value.setCascadeDeleteEnabled(cascadeOnDelete);
    // value.setLazy( fetchMode != FetchMode.JOIN );
    if (!optional) {
        for (Ejb3JoinColumn column : columns) {
            column.setNullable(false);
        }
    }
    if (property.isAnnotationPresent(MapsId.class)) {
        // read only
        for (Ejb3JoinColumn column : columns) {
            column.setInsertable(false);
            column.setUpdatable(false);
        }
    }
    final JoinColumn joinColumn = property.getAnnotation(JoinColumn.class);
    final JoinColumns joinColumns = property.getAnnotation(JoinColumns.class);
    // Make sure that JPA1 key-many-to-one columns are read only tooj
    boolean hasSpecjManyToOne = false;
    if (context.getBuildingOptions().isSpecjProprietarySyntaxEnabled()) {
        String columnName = "";
        for (XProperty prop : inferredData.getDeclaringClass().getDeclaredProperties(AccessType.FIELD.getType())) {
            if (prop.isAnnotationPresent(Id.class) && prop.isAnnotationPresent(Column.class)) {
                columnName = prop.getAnnotation(Column.class).name();
            }
            if (property.isAnnotationPresent(ManyToOne.class) && joinColumn != null && !BinderHelper.isEmptyAnnotationValue(joinColumn.name()) && joinColumn.name().equals(columnName) && !property.isAnnotationPresent(MapsId.class)) {
                hasSpecjManyToOne = true;
                for (Ejb3JoinColumn column : columns) {
                    column.setInsertable(false);
                    column.setUpdatable(false);
                }
            }
        }
    }
    value.setTypeName(inferredData.getClassOrElementName());
    final String propertyName = inferredData.getPropertyName();
    value.setTypeUsingReflection(propertyHolder.getClassName(), propertyName);
    if ((joinColumn != null && joinColumn.foreignKey().value() == ConstraintMode.NO_CONSTRAINT) || (joinColumns != null && joinColumns.foreignKey().value() == ConstraintMode.NO_CONSTRAINT)) {
        // not ideal...
        value.setForeignKeyName("none");
    } else {
        final ForeignKey fk = property.getAnnotation(ForeignKey.class);
        if (fk != null && StringHelper.isNotEmpty(fk.name())) {
            value.setForeignKeyName(fk.name());
        } else {
            final javax.persistence.ForeignKey fkOverride = propertyHolder.getOverriddenForeignKey(StringHelper.qualify(propertyHolder.getPath(), propertyName));
            if (fkOverride != null && fkOverride.value() == ConstraintMode.NO_CONSTRAINT) {
                value.setForeignKeyName("none");
            } else if (fkOverride != null) {
                value.setForeignKeyName(StringHelper.nullIfEmpty(fkOverride.name()));
                value.setForeignKeyDefinition(StringHelper.nullIfEmpty(fkOverride.foreignKeyDefinition()));
            } else if (joinColumns != null) {
                value.setForeignKeyName(StringHelper.nullIfEmpty(joinColumns.foreignKey().name()));
                value.setForeignKeyDefinition(StringHelper.nullIfEmpty(joinColumns.foreignKey().foreignKeyDefinition()));
            } else if (joinColumn != null) {
                value.setForeignKeyName(StringHelper.nullIfEmpty(joinColumn.foreignKey().name()));
                value.setForeignKeyDefinition(StringHelper.nullIfEmpty(joinColumn.foreignKey().foreignKeyDefinition()));
            }
        }
    }
    String path = propertyHolder.getPath() + "." + propertyName;
    FkSecondPass secondPass = new ToOneFkSecondPass(value, columns, // cannot have nullable and unique on certain DBs like Derby
    !optional && unique, propertyHolder.getEntityOwnerClassName(), path, context);
    if (inSecondPass) {
        secondPass.doSecondPass(context.getMetadataCollector().getEntityBindingMap());
    } else {
        context.getMetadataCollector().addSecondPass(secondPass);
    }
    Ejb3Column.checkPropertyConsistency(columns, propertyHolder.getEntityName() + "." + propertyName);
    // PropertyBinder binder = new PropertyBinder();
    propertyBinder.setName(propertyName);
    propertyBinder.setValue(value);
    // binder.setCascade(cascadeStrategy);
    if (isIdentifierMapper) {
        propertyBinder.setInsertable(false);
        propertyBinder.setUpdatable(false);
    } else if (hasSpecjManyToOne) {
        propertyBinder.setInsertable(false);
        propertyBinder.setUpdatable(false);
    } else {
        propertyBinder.setInsertable(columns[0].isInsertable());
        propertyBinder.setUpdatable(columns[0].isUpdatable());
    }
    propertyBinder.setColumns(columns);
    propertyBinder.setAccessType(inferredData.getDefaultAccess());
    propertyBinder.setCascade(cascadeStrategy);
    propertyBinder.setProperty(property);
    propertyBinder.setXToMany(true);
    propertyBinder.makePropertyAndBind();
}
Also used : XProperty(org.hibernate.annotations.common.reflection.XProperty) MapKeyJoinColumns(javax.persistence.MapKeyJoinColumns) JoinColumns(javax.persistence.JoinColumns) PrimaryKeyJoinColumns(javax.persistence.PrimaryKeyJoinColumns) ForeignKey(org.hibernate.annotations.ForeignKey) ManyToOne(javax.persistence.ManyToOne) PrimaryKeyJoinColumn(javax.persistence.PrimaryKeyJoinColumn) MapKeyJoinColumn(javax.persistence.MapKeyJoinColumn) JoinColumn(javax.persistence.JoinColumn) MapKeyColumn(javax.persistence.MapKeyColumn) OrderColumn(javax.persistence.OrderColumn) PrimaryKeyJoinColumn(javax.persistence.PrimaryKeyJoinColumn) MapKeyJoinColumn(javax.persistence.MapKeyJoinColumn) Column(javax.persistence.Column) DiscriminatorColumn(javax.persistence.DiscriminatorColumn) JoinColumn(javax.persistence.JoinColumn) MapsId(javax.persistence.MapsId) NaturalId(org.hibernate.annotations.NaturalId) EmbeddedId(javax.persistence.EmbeddedId) Id(javax.persistence.Id) CollectionId(org.hibernate.annotations.CollectionId)

Example 12 with JoinColumns

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

the class ColumnsBuilder method extractMetadata.

public ColumnsBuilder extractMetadata() {
    columns = null;
    joinColumns = buildExplicitJoinColumns(property, inferredData);
    if (property.isAnnotationPresent(Column.class) || property.isAnnotationPresent(Formula.class)) {
        Column ann = property.getAnnotation(Column.class);
        Formula formulaAnn = property.getAnnotation(Formula.class);
        columns = Ejb3Column.buildColumnFromAnnotation(new Column[] { ann }, formulaAnn, nullability, propertyHolder, inferredData, entityBinder.getSecondaryTables(), buildingContext);
    } else if (property.isAnnotationPresent(Columns.class)) {
        Columns anns = property.getAnnotation(Columns.class);
        columns = Ejb3Column.buildColumnFromAnnotation(anns.columns(), null, nullability, propertyHolder, inferredData, entityBinder.getSecondaryTables(), buildingContext);
    }
    // set default values if needed
    if (joinColumns == null && (property.isAnnotationPresent(ManyToOne.class) || property.isAnnotationPresent(OneToOne.class))) {
        joinColumns = buildDefaultJoinColumnsForXToOne(property, inferredData);
    } else if (joinColumns == null && (property.isAnnotationPresent(OneToMany.class) || property.isAnnotationPresent(ElementCollection.class))) {
        OneToMany oneToMany = property.getAnnotation(OneToMany.class);
        String mappedBy = oneToMany != null ? oneToMany.mappedBy() : "";
        joinColumns = Ejb3JoinColumn.buildJoinColumns(null, mappedBy, entityBinder.getSecondaryTables(), propertyHolder, inferredData.getPropertyName(), buildingContext);
    } else if (joinColumns == null && property.isAnnotationPresent(org.hibernate.annotations.Any.class)) {
        throw new AnnotationException("@Any requires an explicit @JoinColumn(s): " + BinderHelper.getPath(propertyHolder, inferredData));
    }
    if (columns == null && !property.isAnnotationPresent(ManyToMany.class)) {
        // useful for collection of embedded elements
        columns = Ejb3Column.buildColumnFromAnnotation(null, null, nullability, propertyHolder, inferredData, entityBinder.getSecondaryTables(), buildingContext);
    }
    if (nullability == Nullability.FORCED_NOT_NULL) {
        // force columns to not null
        for (Ejb3Column col : columns) {
            col.forceNotNull();
        }
    }
    return this;
}
Also used : Formula(org.hibernate.annotations.Formula) JoinColumnOrFormula(org.hibernate.annotations.JoinColumnOrFormula) JoinFormula(org.hibernate.annotations.JoinFormula) OneToOne(javax.persistence.OneToOne) JoinColumn(javax.persistence.JoinColumn) Column(javax.persistence.Column) JoinColumns(javax.persistence.JoinColumns) Columns(org.hibernate.annotations.Columns) AnnotationException(org.hibernate.AnnotationException) OneToMany(javax.persistence.OneToMany) ManyToOne(javax.persistence.ManyToOne)

Aggregations

JoinColumn (javax.persistence.JoinColumn)12 JoinColumns (javax.persistence.JoinColumns)12 MapKeyJoinColumn (javax.persistence.MapKeyJoinColumn)6 MapKeyJoinColumns (javax.persistence.MapKeyJoinColumns)6 PrimaryKeyJoinColumn (javax.persistence.PrimaryKeyJoinColumn)6 PrimaryKeyJoinColumns (javax.persistence.PrimaryKeyJoinColumns)6 Test (org.junit.Test)6 Column (javax.persistence.Column)4 ManyToOne (javax.persistence.ManyToOne)4 DiscriminatorColumn (javax.persistence.DiscriminatorColumn)3 EmbeddedId (javax.persistence.EmbeddedId)3 Id (javax.persistence.Id)3 MapKeyColumn (javax.persistence.MapKeyColumn)3 MapsId (javax.persistence.MapsId)3 OneToMany (javax.persistence.OneToMany)3 OneToOne (javax.persistence.OneToOne)3 OrderColumn (javax.persistence.OrderColumn)3 AnnotationException (org.hibernate.AnnotationException)3 CollectionId (org.hibernate.annotations.CollectionId)3 NaturalId (org.hibernate.annotations.NaturalId)3