Search in sources :

Example 1 with PrimaryKeyJoinColumn

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

the class AnnotationBinder method handleForeignKeys.

private static void handleForeignKeys(XClass clazzToProcess, MetadataBuildingContext context, DependantValue key) {
    ForeignKey foreignKey = clazzToProcess.getAnnotation(ForeignKey.class);
    if (foreignKey != null && !BinderHelper.isEmptyAnnotationValue(foreignKey.name())) {
        key.setForeignKeyName(foreignKey.name());
    } else {
        PrimaryKeyJoinColumn pkJoinColumn = clazzToProcess.getAnnotation(PrimaryKeyJoinColumn.class);
        PrimaryKeyJoinColumns pkJoinColumns = clazzToProcess.getAnnotation(PrimaryKeyJoinColumns.class);
        final boolean noConstraintByDefault = context.getBuildingOptions().isNoConstraintByDefault();
        if (pkJoinColumns != null && (pkJoinColumns.foreignKey().value() == ConstraintMode.NO_CONSTRAINT || pkJoinColumns.foreignKey().value() == ConstraintMode.PROVIDER_DEFAULT && noConstraintByDefault)) {
            // don't apply a constraint based on ConstraintMode
            key.disableForeignKey();
        } else if (pkJoinColumns != null && !StringHelper.isEmpty(pkJoinColumns.foreignKey().name())) {
            key.setForeignKeyName(pkJoinColumns.foreignKey().name());
            if (!BinderHelper.isEmptyAnnotationValue(pkJoinColumns.foreignKey().foreignKeyDefinition())) {
                key.setForeignKeyDefinition(pkJoinColumns.foreignKey().foreignKeyDefinition());
            }
        } else if (pkJoinColumn != null && (pkJoinColumn.foreignKey().value() == ConstraintMode.NO_CONSTRAINT || pkJoinColumn.foreignKey().value() == ConstraintMode.PROVIDER_DEFAULT && noConstraintByDefault)) {
            // don't apply a constraint based on ConstraintMode
            key.disableForeignKey();
        } else if (pkJoinColumn != null && !StringHelper.isEmpty(pkJoinColumn.foreignKey().name())) {
            key.setForeignKeyName(pkJoinColumn.foreignKey().name());
            if (!BinderHelper.isEmptyAnnotationValue(pkJoinColumn.foreignKey().foreignKeyDefinition())) {
                key.setForeignKeyDefinition(pkJoinColumn.foreignKey().foreignKeyDefinition());
            }
        }
    }
}
Also used : PrimaryKeyJoinColumn(jakarta.persistence.PrimaryKeyJoinColumn) PrimaryKeyJoinColumns(jakarta.persistence.PrimaryKeyJoinColumns) ForeignKey(org.hibernate.annotations.ForeignKey)

Example 2 with PrimaryKeyJoinColumn

use of jakarta.persistence.PrimaryKeyJoinColumn 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)

Example 3 with PrimaryKeyJoinColumn

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

the class Ejb3XmlOneToOneTest method testSinglePrimaryKeyJoinColumn.

/**
 * When there's a single primary key join column, we still wrap it with
 * a PrimaryKeyJoinColumns annotation.
 */
@Test
public void testSinglePrimaryKeyJoinColumn() throws Exception {
    reader = getReader(Entity1.class, "field1", "one-to-one.orm2.xml");
    assertAnnotationPresent(OneToOne.class);
    assertAnnotationNotPresent(PrimaryKeyJoinColumn.class);
    assertAnnotationPresent(PrimaryKeyJoinColumns.class);
    PrimaryKeyJoinColumns joinColumnsAnno = reader.getAnnotation(PrimaryKeyJoinColumns.class);
    assertAnnotationNotPresent(JoinColumns.class);
    assertAnnotationNotPresent(JoinColumn.class);
    assertAnnotationNotPresent(JoinTable.class);
    PrimaryKeyJoinColumn[] joinColumns = joinColumnsAnno.value();
    assertEquals(1, joinColumns.length);
    assertEquals("col1", joinColumns[0].name());
    assertEquals("col2", joinColumns[0].referencedColumnName());
    assertEquals("int", joinColumns[0].columnDefinition());
}
Also used : PrimaryKeyJoinColumn(jakarta.persistence.PrimaryKeyJoinColumn) PrimaryKeyJoinColumns(jakarta.persistence.PrimaryKeyJoinColumns) Test(org.junit.Test)

Example 4 with PrimaryKeyJoinColumn

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

the class JPAXMLOverriddenAnnotationReader method getPrimaryKeyJoinColumns.

/**
 * @param mergeWithAnnotations Whether to use Java annotations for this
 * element, if present and not disabled by the XMLContext defaults.
 * In some contexts (such as an association mapping) merging with
 */
private PrimaryKeyJoinColumns getPrimaryKeyJoinColumns(List<JaxbPrimaryKeyJoinColumn> elements, XMLContext.Default defaults, boolean mergeWithAnnotations) {
    PrimaryKeyJoinColumn[] columns = buildPrimaryKeyJoinColumns(elements);
    if (mergeWithAnnotations) {
        if (columns.length == 0 && defaults.canUseJavaAnnotations()) {
            PrimaryKeyJoinColumn annotation = getPhysicalAnnotation(PrimaryKeyJoinColumn.class);
            if (annotation != null) {
                columns = new PrimaryKeyJoinColumn[] { annotation };
            } else {
                PrimaryKeyJoinColumns annotations = getPhysicalAnnotation(PrimaryKeyJoinColumns.class);
                columns = annotations != null ? annotations.value() : columns;
            }
        }
    }
    if (columns.length > 0) {
        AnnotationDescriptor ad = new AnnotationDescriptor(PrimaryKeyJoinColumns.class);
        ad.setValue("value", columns);
        return AnnotationFactory.create(ad);
    } else {
        return null;
    }
}
Also used : AnnotationDescriptor(org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor) JaxbPrimaryKeyJoinColumn(org.hibernate.boot.jaxb.mapping.spi.JaxbPrimaryKeyJoinColumn) PrimaryKeyJoinColumn(jakarta.persistence.PrimaryKeyJoinColumn) PrimaryKeyJoinColumns(jakarta.persistence.PrimaryKeyJoinColumns)

Example 5 with PrimaryKeyJoinColumn

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

the class JPAXMLOverriddenAnnotationReader method buildPrimaryKeyJoinColumns.

private PrimaryKeyJoinColumn[] buildPrimaryKeyJoinColumns(List<JaxbPrimaryKeyJoinColumn> elements) {
    PrimaryKeyJoinColumn[] pkJoinColumns = new PrimaryKeyJoinColumn[elements.size()];
    int i = 0;
    for (JaxbPrimaryKeyJoinColumn element : elements) {
        AnnotationDescriptor pkAnn = new AnnotationDescriptor(PrimaryKeyJoinColumn.class);
        copyAttribute(pkAnn, "name", element.getName(), false);
        copyAttribute(pkAnn, "referenced-column-name", element.getReferencedColumnName(), false);
        copyAttribute(pkAnn, "column-definition", element.getColumnDefinition(), false);
        pkJoinColumns[i++] = AnnotationFactory.create(pkAnn);
    }
    return pkJoinColumns;
}
Also used : AnnotationDescriptor(org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor) JaxbPrimaryKeyJoinColumn(org.hibernate.boot.jaxb.mapping.spi.JaxbPrimaryKeyJoinColumn) PrimaryKeyJoinColumn(jakarta.persistence.PrimaryKeyJoinColumn) JaxbPrimaryKeyJoinColumn(org.hibernate.boot.jaxb.mapping.spi.JaxbPrimaryKeyJoinColumn) JaxbUniqueConstraint(org.hibernate.boot.jaxb.mapping.spi.JaxbUniqueConstraint) QueryHint(jakarta.persistence.QueryHint) JaxbQueryHint(org.hibernate.boot.jaxb.mapping.spi.JaxbQueryHint) UniqueConstraint(jakarta.persistence.UniqueConstraint)

Aggregations

PrimaryKeyJoinColumn (jakarta.persistence.PrimaryKeyJoinColumn)8 PrimaryKeyJoinColumns (jakarta.persistence.PrimaryKeyJoinColumns)5 JoinColumn (jakarta.persistence.JoinColumn)2 UniqueConstraint (jakarta.persistence.UniqueConstraint)2 AnnotationDescriptor (org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor)2 JaxbPrimaryKeyJoinColumn (org.hibernate.boot.jaxb.mapping.spi.JaxbPrimaryKeyJoinColumn)2 Test (org.junit.Test)2 QueryHint (jakarta.persistence.QueryHint)1 ForeignKey (org.hibernate.annotations.ForeignKey)1 JaxbQueryHint (org.hibernate.boot.jaxb.mapping.spi.JaxbQueryHint)1 JaxbUniqueConstraint (org.hibernate.boot.jaxb.mapping.spi.JaxbUniqueConstraint)1 ObjectNameNormalizer (org.hibernate.boot.model.naming.ObjectNameNormalizer)1 AnnotatedJoinColumn (org.hibernate.cfg.AnnotatedJoinColumn)1 Column (org.hibernate.mapping.Column)1 Constraint (org.hibernate.mapping.Constraint)1