Search in sources :

Example 6 with JoinColumn

use of jakarta.persistence.JoinColumn in project hibernate-orm by hibernate.

the class AnnotatedJoinColumn method buildJoinTableJoinColumns.

public static AnnotatedJoinColumn[] buildJoinTableJoinColumns(JoinColumn[] annJoins, Map<String, Join> secondaryTables, PropertyHolder propertyHolder, String propertyName, String mappedBy, MetadataBuildingContext buildingContext) {
    AnnotatedJoinColumn[] joinColumns;
    if (annJoins == null) {
        AnnotatedJoinColumn currentJoinColumn = new AnnotatedJoinColumn();
        currentJoinColumn.setImplicit(true);
        // I break the spec, but it's for good
        currentJoinColumn.setNullable(false);
        currentJoinColumn.setPropertyHolder(propertyHolder);
        currentJoinColumn.setJoins(secondaryTables);
        currentJoinColumn.setBuildingContext(buildingContext);
        currentJoinColumn.setPropertyName(BinderHelper.getRelativePath(propertyHolder, propertyName));
        currentJoinColumn.setMappedBy(mappedBy);
        currentJoinColumn.bind();
        joinColumns = new AnnotatedJoinColumn[] { currentJoinColumn };
    } else {
        joinColumns = new AnnotatedJoinColumn[annJoins.length];
        JoinColumn annJoin;
        int length = annJoins.length;
        for (int index = 0; index < length; index++) {
            annJoin = annJoins[index];
            AnnotatedJoinColumn currentJoinColumn = new AnnotatedJoinColumn();
            currentJoinColumn.setImplicit(true);
            currentJoinColumn.setPropertyHolder(propertyHolder);
            currentJoinColumn.setJoins(secondaryTables);
            currentJoinColumn.setBuildingContext(buildingContext);
            currentJoinColumn.setPropertyName(BinderHelper.getRelativePath(propertyHolder, propertyName));
            currentJoinColumn.setMappedBy(mappedBy);
            currentJoinColumn.setJoinAnnotation(annJoin, propertyName);
            // I break the spec, but it's for good
            currentJoinColumn.setNullable(false);
            // done after the annotation to override it
            currentJoinColumn.bind();
            joinColumns[index] = currentJoinColumn;
        }
    }
    return joinColumns;
}
Also used : PrimaryKeyJoinColumn(jakarta.persistence.PrimaryKeyJoinColumn) JoinColumn(jakarta.persistence.JoinColumn)

Example 7 with JoinColumn

use of jakarta.persistence.JoinColumn in project hibernate-orm by hibernate.

the class JPAXMLOverriddenAnnotationReader method getCollectionTable.

private void getCollectionTable(List<Annotation> annotationList, JaxbCollectionTable element, XMLContext.Default defaults) {
    if (element != null) {
        AnnotationDescriptor annotation = new AnnotationDescriptor(CollectionTable.class);
        copyAttribute(annotation, "name", element.getName(), false);
        copyAttribute(annotation, "catalog", element.getCatalog(), false);
        if (StringHelper.isNotEmpty(defaults.getCatalog()) && StringHelper.isEmpty((String) annotation.valueOf("catalog"))) {
            annotation.setValue("catalog", defaults.getCatalog());
        }
        copyAttribute(annotation, "schema", element.getSchema(), false);
        if (StringHelper.isNotEmpty(defaults.getSchema()) && StringHelper.isEmpty((String) annotation.valueOf("schema"))) {
            annotation.setValue("schema", defaults.getSchema());
        }
        JoinColumn[] joinColumns = getJoinColumns(element.getJoinColumn(), false);
        if (joinColumns.length > 0) {
            annotation.setValue("joinColumns", joinColumns);
        }
        buildUniqueConstraints(annotation, element.getUniqueConstraint());
        buildIndex(annotation, element.getIndex());
        annotationList.add(AnnotationFactory.create(annotation));
    }
}
Also used : AnnotationDescriptor(org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor) JaxbPrimaryKeyJoinColumn(org.hibernate.boot.jaxb.mapping.spi.JaxbPrimaryKeyJoinColumn) JaxbMapKeyJoinColumn(org.hibernate.boot.jaxb.mapping.spi.JaxbMapKeyJoinColumn) PrimaryKeyJoinColumn(jakarta.persistence.PrimaryKeyJoinColumn) MapKeyJoinColumn(jakarta.persistence.MapKeyJoinColumn) JoinColumn(jakarta.persistence.JoinColumn) JaxbJoinColumn(org.hibernate.boot.jaxb.mapping.spi.JaxbJoinColumn)

Example 8 with JoinColumn

use of jakarta.persistence.JoinColumn in project hibernate-orm by hibernate.

the class JPAXMLOverriddenAnnotationReader method buildJoinColumns.

private void buildJoinColumns(List<Annotation> annotationList, List<JaxbJoinColumn> elements) {
    JoinColumn[] joinColumns = getJoinColumns(elements, false);
    if (joinColumns.length > 0) {
        AnnotationDescriptor ad = new AnnotationDescriptor(JoinColumns.class);
        ad.setValue("value", joinColumns);
        annotationList.add(AnnotationFactory.create(ad));
    }
}
Also used : AnnotationDescriptor(org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor) JaxbPrimaryKeyJoinColumn(org.hibernate.boot.jaxb.mapping.spi.JaxbPrimaryKeyJoinColumn) JaxbMapKeyJoinColumn(org.hibernate.boot.jaxb.mapping.spi.JaxbMapKeyJoinColumn) PrimaryKeyJoinColumn(jakarta.persistence.PrimaryKeyJoinColumn) MapKeyJoinColumn(jakarta.persistence.MapKeyJoinColumn) JoinColumn(jakarta.persistence.JoinColumn) JaxbJoinColumn(org.hibernate.boot.jaxb.mapping.spi.JaxbJoinColumn)

Example 9 with JoinColumn

use of jakarta.persistence.JoinColumn in project hibernate-orm by hibernate.

the class CollectionBinder method handleCollectionOfEntities.

private ManyToOne handleCollectionOfEntities(Collection collValue, XClass collType, boolean ignoreNotFound, XProperty property, MetadataBuildingContext buildingContext, PersistentClass collectionEntity, String hqlOrderBy) {
    ManyToOne element;
    element = new ManyToOne(buildingContext, collValue.getCollectionTable());
    collValue.setElement(element);
    element.setReferencedEntityName(collType.getName());
    // element.setFetchMode( fetchMode );
    // element.setLazy( fetchMode != FetchMode.JOIN );
    // make the second join non lazy
    element.setFetchMode(FetchMode.JOIN);
    element.setLazy(false);
    element.setIgnoreNotFound(ignoreNotFound);
    // as per 11.1.38 of JPA 2.0 spec, default to primary key if no column is specified by @OrderBy.
    if (hqlOrderBy != null) {
        collValue.setManyToManyOrdering(buildOrderByClauseFromHql(hqlOrderBy, collectionEntity));
    }
    final ForeignKey fk = property.getAnnotation(ForeignKey.class);
    if (fk != null && !isEmptyAnnotationValue(fk.name())) {
        element.setForeignKeyName(fk.name());
    } else {
        final JoinTable joinTableAnn = property.getAnnotation(JoinTable.class);
        if (joinTableAnn != null) {
            String foreignKeyName = joinTableAnn.inverseForeignKey().name();
            String foreignKeyDefinition = joinTableAnn.inverseForeignKey().foreignKeyDefinition();
            if (joinTableAnn.inverseJoinColumns().length != 0) {
                final JoinColumn joinColumnAnn = joinTableAnn.inverseJoinColumns()[0];
                if (foreignKeyName != null && foreignKeyName.isEmpty()) {
                    foreignKeyName = joinColumnAnn.foreignKey().name();
                    foreignKeyDefinition = joinColumnAnn.foreignKey().foreignKeyDefinition();
                }
            }
            if (joinTableAnn.inverseForeignKey().value() == ConstraintMode.NO_CONSTRAINT || joinTableAnn.inverseForeignKey().value() == ConstraintMode.PROVIDER_DEFAULT && buildingContext.getBuildingOptions().isNoConstraintByDefault()) {
                element.disableForeignKey();
            } else {
                element.setForeignKeyName(StringHelper.nullIfEmpty(foreignKeyName));
                element.setForeignKeyDefinition(StringHelper.nullIfEmpty(foreignKeyDefinition));
            }
        }
    }
    return element;
}
Also used : JoinColumn(jakarta.persistence.JoinColumn) AnnotatedJoinColumn(org.hibernate.cfg.AnnotatedJoinColumn) ForeignKey(org.hibernate.annotations.ForeignKey) ManyToOne(org.hibernate.mapping.ManyToOne) JoinTable(jakarta.persistence.JoinTable) WhereJoinTable(org.hibernate.annotations.WhereJoinTable) FilterJoinTable(org.hibernate.annotations.FilterJoinTable)

Example 10 with JoinColumn

use of jakarta.persistence.JoinColumn in project hibernate-orm by hibernate.

the class EntityBinder method createPrimaryColumnsToSecondaryTable.

private void createPrimaryColumnsToSecondaryTable(Object uncastedColumn, PropertyHolder propertyHolder, Join join) {
    AnnotatedJoinColumn[] annotatedJoinColumns;
    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) {
        annotatedJoinColumns = new AnnotatedJoinColumn[1];
        annotatedJoinColumns[0] = AnnotatedJoinColumn.buildJoinColumn(null, null, persistentClass.getIdentifier(), secondaryTables, propertyHolder, context);
    } else {
        int nbrOfJoinColumns = pkColumnsAnn != null ? pkColumnsAnn.length : joinColumnsAnn.length;
        if (nbrOfJoinColumns == 0) {
            annotatedJoinColumns = new AnnotatedJoinColumn[1];
            annotatedJoinColumns[0] = AnnotatedJoinColumn.buildJoinColumn(null, null, persistentClass.getIdentifier(), secondaryTables, propertyHolder, context);
        } else {
            annotatedJoinColumns = new AnnotatedJoinColumn[nbrOfJoinColumns];
            if (pkColumnsAnn != null) {
                for (int colIndex = 0; colIndex < nbrOfJoinColumns; colIndex++) {
                    annotatedJoinColumns[colIndex] = AnnotatedJoinColumn.buildJoinColumn(pkColumnsAnn[colIndex], null, persistentClass.getIdentifier(), secondaryTables, propertyHolder, context);
                }
            } else {
                for (int colIndex = 0; colIndex < nbrOfJoinColumns; colIndex++) {
                    annotatedJoinColumns[colIndex] = AnnotatedJoinColumn.buildJoinColumn(null, joinColumnsAnn[colIndex], persistentClass.getIdentifier(), secondaryTables, propertyHolder, context);
                }
            }
        }
    }
    for (AnnotatedJoinColumn joinColumn : annotatedJoinColumns) {
        joinColumn.forceNotNull();
    }
    bindJoinToPersistentClass(join, annotatedJoinColumns, context);
}
Also used : PrimaryKeyJoinColumn(jakarta.persistence.PrimaryKeyJoinColumn) JoinColumn(jakarta.persistence.JoinColumn) AnnotatedJoinColumn(org.hibernate.cfg.AnnotatedJoinColumn) PrimaryKeyJoinColumn(jakarta.persistence.PrimaryKeyJoinColumn) AnnotatedJoinColumn(org.hibernate.cfg.AnnotatedJoinColumn)

Aggregations

JoinColumn (jakarta.persistence.JoinColumn)28 MapKeyJoinColumn (jakarta.persistence.MapKeyJoinColumn)16 PrimaryKeyJoinColumn (jakarta.persistence.PrimaryKeyJoinColumn)16 JoinColumns (jakarta.persistence.JoinColumns)12 Test (org.junit.Test)12 JoinTable (jakarta.persistence.JoinTable)10 UniqueConstraint (jakarta.persistence.UniqueConstraint)9 MapKeyJoinColumns (jakarta.persistence.MapKeyJoinColumns)8 PrimaryKeyJoinColumns (jakarta.persistence.PrimaryKeyJoinColumns)7 OrderColumn (jakarta.persistence.OrderColumn)6 Column (jakarta.persistence.Column)5 DiscriminatorColumn (jakarta.persistence.DiscriminatorColumn)5 MapKeyColumn (jakarta.persistence.MapKeyColumn)5 AnnotationException (org.hibernate.AnnotationException)5 AnnotatedDiscriminatorColumn.buildDiscriminatorColumn (org.hibernate.cfg.AnnotatedDiscriminatorColumn.buildDiscriminatorColumn)5 AnnotatedJoinColumn.buildJoinTableJoinColumns (org.hibernate.cfg.AnnotatedJoinColumn.buildJoinTableJoinColumns)5 OnDelete (org.hibernate.annotations.OnDelete)4 CollectionTable (jakarta.persistence.CollectionTable)3 Cascade (org.hibernate.annotations.Cascade)3 Columns (org.hibernate.annotations.Columns)3