Search in sources :

Example 6 with PrimaryKeyJoinColumn

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

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

the class AnnotatedJoinColumn method buildJoinColumn.

/**
 * Build JoinColumn for a JOINED hierarchy
 */
public static AnnotatedJoinColumn buildJoinColumn(PrimaryKeyJoinColumn pkJoinAnn, JoinColumn joinAnn, Value identifier, Map<String, Join> joins, PropertyHolder propertyHolder, MetadataBuildingContext context) {
    final ObjectNameNormalizer normalizer = context.getObjectNameNormalizer();
    Column col = identifier.getColumns().get(0);
    String defaultName = context.getMetadataCollector().getLogicalColumnName(identifier.getTable(), col.getQuotedName());
    if (pkJoinAnn != null || joinAnn != null) {
        String colName;
        String columnDefinition;
        String referencedColumnName;
        if (pkJoinAnn != null) {
            colName = pkJoinAnn.name();
            columnDefinition = pkJoinAnn.columnDefinition();
            referencedColumnName = pkJoinAnn.referencedColumnName();
        } else {
            colName = joinAnn.name();
            columnDefinition = joinAnn.columnDefinition();
            referencedColumnName = joinAnn.referencedColumnName();
        }
        final String sqlType;
        if (columnDefinition.isEmpty()) {
            sqlType = null;
        } else {
            sqlType = normalizer.toDatabaseIdentifierText(columnDefinition);
        }
        final String name;
        if (colName != null && colName.isEmpty()) {
            name = normalizer.normalizeIdentifierQuotingAsString(defaultName);
        } else {
            name = context.getObjectNameNormalizer().normalizeIdentifierQuotingAsString(colName);
        }
        return new AnnotatedJoinColumn(sqlType, name, null, false, false, true, true, referencedColumnName, null, joins, propertyHolder, null, null, false, context);
    } else {
        defaultName = context.getObjectNameNormalizer().normalizeIdentifierQuotingAsString(defaultName);
        return new AnnotatedJoinColumn(null, defaultName, null, false, false, true, true, null, null, joins, propertyHolder, null, null, true, context);
    }
}
Also used : PrimaryKeyJoinColumn(jakarta.persistence.PrimaryKeyJoinColumn) Column(org.hibernate.mapping.Column) JoinColumn(jakarta.persistence.JoinColumn) ObjectNameNormalizer(org.hibernate.boot.model.naming.ObjectNameNormalizer)

Example 8 with PrimaryKeyJoinColumn

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