Search in sources :

Example 1 with PrimaryKeyJoinColumns

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

use of jakarta.persistence.PrimaryKeyJoinColumns 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 3 with PrimaryKeyJoinColumns

use of jakarta.persistence.PrimaryKeyJoinColumns 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 4 with PrimaryKeyJoinColumns

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

the class AnnotationBinder method makeInheritanceJoinColumns.

private static AnnotatedJoinColumn[] makeInheritanceJoinColumns(XClass clazzToProcess, MetadataBuildingContext context, InheritanceState inheritanceState, PersistentClass superEntity) {
    AnnotatedJoinColumn[] inheritanceJoinedColumns = null;
    final boolean hasJoinedColumns = inheritanceState.hasParents() && InheritanceType.JOINED == inheritanceState.getType();
    if (hasJoinedColumns) {
        // @Inheritance(JOINED) subclass need to link back to the super entity
        PrimaryKeyJoinColumns jcsAnn = clazzToProcess.getAnnotation(PrimaryKeyJoinColumns.class);
        boolean explicitInheritanceJoinedColumns = jcsAnn != null && jcsAnn.value().length != 0;
        if (explicitInheritanceJoinedColumns) {
            int nbrOfInhJoinedColumns = jcsAnn.value().length;
            PrimaryKeyJoinColumn jcAnn;
            inheritanceJoinedColumns = new AnnotatedJoinColumn[nbrOfInhJoinedColumns];
            for (int colIndex = 0; colIndex < nbrOfInhJoinedColumns; colIndex++) {
                jcAnn = jcsAnn.value()[colIndex];
                inheritanceJoinedColumns[colIndex] = AnnotatedJoinColumn.buildJoinColumn(jcAnn, null, superEntity.getIdentifier(), null, null, context);
            }
        } else {
            PrimaryKeyJoinColumn jcAnn = clazzToProcess.getAnnotation(PrimaryKeyJoinColumn.class);
            inheritanceJoinedColumns = new AnnotatedJoinColumn[1];
            inheritanceJoinedColumns[0] = AnnotatedJoinColumn.buildJoinColumn(jcAnn, null, superEntity.getIdentifier(), null, null, context);
        }
        LOG.trace("Subclass joined column(s) created");
    } else {
        if (clazzToProcess.isAnnotationPresent(PrimaryKeyJoinColumns.class) || clazzToProcess.isAnnotationPresent(PrimaryKeyJoinColumn.class)) {
            LOG.invalidPrimaryKeyJoinColumnAnnotation(clazzToProcess.getName());
        }
    }
    return inheritanceJoinedColumns;
}
Also used : PrimaryKeyJoinColumn(jakarta.persistence.PrimaryKeyJoinColumn) PrimaryKeyJoinColumns(jakarta.persistence.PrimaryKeyJoinColumns) UniqueConstraint(jakarta.persistence.UniqueConstraint) Constraint(org.hibernate.mapping.Constraint)

Example 5 with PrimaryKeyJoinColumns

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

the class Ejb3XmlOneToOneTest method testMultiplePrimaryKeyJoinColumn.

@Test
public void testMultiplePrimaryKeyJoinColumn() throws Exception {
    reader = getReader(Entity1.class, "field1", "one-to-one.orm3.xml");
    assertAnnotationPresent(OneToOne.class);
    assertAnnotationNotPresent(PrimaryKeyJoinColumn.class);
    assertAnnotationPresent(PrimaryKeyJoinColumns.class);
    assertAnnotationNotPresent(JoinColumns.class);
    assertAnnotationNotPresent(JoinColumn.class);
    assertAnnotationNotPresent(JoinTable.class);
    PrimaryKeyJoinColumns joinColumnsAnno = reader.getAnnotation(PrimaryKeyJoinColumns.class);
    PrimaryKeyJoinColumn[] joinColumns = joinColumnsAnno.value();
    assertEquals(2, joinColumns.length);
    assertEquals("", joinColumns[0].name());
    assertEquals("", joinColumns[0].referencedColumnName());
    assertEquals("", joinColumns[0].columnDefinition());
    assertEquals("col1", joinColumns[1].name());
    assertEquals("col2", joinColumns[1].referencedColumnName());
    assertEquals("int", joinColumns[1].columnDefinition());
}
Also used : PrimaryKeyJoinColumn(jakarta.persistence.PrimaryKeyJoinColumn) PrimaryKeyJoinColumns(jakarta.persistence.PrimaryKeyJoinColumns) Test(org.junit.Test)

Aggregations

PrimaryKeyJoinColumn (jakarta.persistence.PrimaryKeyJoinColumn)5 PrimaryKeyJoinColumns (jakarta.persistence.PrimaryKeyJoinColumns)5 Test (org.junit.Test)2 UniqueConstraint (jakarta.persistence.UniqueConstraint)1 ForeignKey (org.hibernate.annotations.ForeignKey)1 AnnotationDescriptor (org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor)1 JaxbPrimaryKeyJoinColumn (org.hibernate.boot.jaxb.mapping.spi.JaxbPrimaryKeyJoinColumn)1 Constraint (org.hibernate.mapping.Constraint)1