Search in sources :

Example 1 with SecondaryTable

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

the class EntityBinder method addJoin.

private Join addJoin(SecondaryTable secondaryTable, JoinTable joinTable, PropertyHolder propertyHolder, boolean noDelayInPkColumnCreation) {
    // A non null propertyHolder means than we process the Pk creation without delay
    Join join = new Join();
    join.setPersistentClass(persistentClass);
    final String schema;
    final String catalog;
    final Object joinColumns;
    final List<UniqueConstraintHolder> uniqueConstraintHolders;
    final QualifiedTableName logicalName;
    if (secondaryTable != null) {
        schema = secondaryTable.schema();
        catalog = secondaryTable.catalog();
        logicalName = new QualifiedTableName(Identifier.toIdentifier(catalog), Identifier.toIdentifier(schema), context.getMetadataCollector().getDatabase().getJdbcEnvironment().getIdentifierHelper().toIdentifier(secondaryTable.name()));
        joinColumns = secondaryTable.pkJoinColumns();
        uniqueConstraintHolders = TableBinder.buildUniqueConstraintHolders(secondaryTable.uniqueConstraints());
    } else if (joinTable != null) {
        schema = joinTable.schema();
        catalog = joinTable.catalog();
        logicalName = new QualifiedTableName(Identifier.toIdentifier(catalog), Identifier.toIdentifier(schema), context.getMetadataCollector().getDatabase().getJdbcEnvironment().getIdentifierHelper().toIdentifier(joinTable.name()));
        joinColumns = joinTable.joinColumns();
        uniqueConstraintHolders = TableBinder.buildUniqueConstraintHolders(joinTable.uniqueConstraints());
    } else {
        throw new AssertionFailure("Both JoinTable and SecondaryTable are null");
    }
    final Table table = TableBinder.buildAndFillTable(schema, catalog, logicalName.getTableName(), false, uniqueConstraintHolders, null, null, context, null, null);
    final InFlightMetadataCollector.EntityTableXref tableXref = context.getMetadataCollector().getEntityTableXref(persistentClass.getEntityName());
    assert tableXref != null : "Could not locate EntityTableXref for entity [" + persistentClass.getEntityName() + "]";
    tableXref.addSecondaryTable(logicalName, join);
    if (secondaryTable != null) {
        TableBinder.addIndexes(table, secondaryTable.indexes(), context);
    }
    // no check constraints available on joins
    join.setTable(table);
    // somehow keep joins() for later.
    // Has to do the work later because it needs persistentClass id!
    LOG.debugf("Adding secondary table to entity %s -> %s", persistentClass.getEntityName(), join.getTable().getName());
    org.hibernate.annotations.Table matchingTable = findMatchingComplimentTableAnnotation(join);
    if (matchingTable != null) {
        join.setSequentialSelect(FetchMode.JOIN != matchingTable.fetch());
        join.setInverse(matchingTable.inverse());
        join.setOptional(matchingTable.optional());
        if (!BinderHelper.isEmptyAnnotationValue(matchingTable.sqlInsert().sql())) {
            join.setCustomSQLInsert(matchingTable.sqlInsert().sql().trim(), matchingTable.sqlInsert().callable(), ExecuteUpdateResultCheckStyle.fromExternalName(matchingTable.sqlInsert().check().toString().toLowerCase(Locale.ROOT)));
        }
        if (!BinderHelper.isEmptyAnnotationValue(matchingTable.sqlUpdate().sql())) {
            join.setCustomSQLUpdate(matchingTable.sqlUpdate().sql().trim(), matchingTable.sqlUpdate().callable(), ExecuteUpdateResultCheckStyle.fromExternalName(matchingTable.sqlUpdate().check().toString().toLowerCase(Locale.ROOT)));
        }
        if (!BinderHelper.isEmptyAnnotationValue(matchingTable.sqlDelete().sql())) {
            join.setCustomSQLDelete(matchingTable.sqlDelete().sql().trim(), matchingTable.sqlDelete().callable(), ExecuteUpdateResultCheckStyle.fromExternalName(matchingTable.sqlDelete().check().toString().toLowerCase(Locale.ROOT)));
        }
    } else {
        // default
        join.setSequentialSelect(false);
        join.setInverse(false);
        // perhaps not quite per-spec, but a Good Thing anyway
        join.setOptional(true);
    }
    if (noDelayInPkColumnCreation) {
        createPrimaryColumnsToSecondaryTable(joinColumns, propertyHolder, join);
    } else {
        secondaryTables.put(table.getQuotedName(), join);
        secondaryTableJoins.put(table.getQuotedName(), joinColumns);
    }
    return join;
}
Also used : QualifiedTableName(org.hibernate.boot.model.relational.QualifiedTableName) UniqueConstraintHolder(org.hibernate.cfg.UniqueConstraintHolder) AssertionFailure(org.hibernate.AssertionFailure) SecondaryTable(jakarta.persistence.SecondaryTable) JoinTable(jakarta.persistence.JoinTable) Table(org.hibernate.mapping.Table) Join(org.hibernate.mapping.Join) InFlightMetadataCollector(org.hibernate.boot.spi.InFlightMetadataCollector)

Example 2 with SecondaryTable

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

the class EntityBinder method findMatchingSecondaryTable.

private SecondaryTable findMatchingSecondaryTable(Join join) {
    final String nameToMatch = join.getTable().getQuotedName();
    SecondaryTable secondaryTable = annotatedClass.getAnnotation(SecondaryTable.class);
    if (secondaryTable != null && nameToMatch.equals(secondaryTable.name())) {
        return secondaryTable;
    }
    SecondaryTables secondaryTables = annotatedClass.getAnnotation(SecondaryTables.class);
    if (secondaryTables != null) {
        for (SecondaryTable secondaryTablesEntry : secondaryTables.value()) {
            if (secondaryTablesEntry != null && nameToMatch.equals(secondaryTablesEntry.name())) {
                return secondaryTablesEntry;
            }
        }
    }
    return null;
}
Also used : SecondaryTables(jakarta.persistence.SecondaryTables) SecondaryTable(jakarta.persistence.SecondaryTable)

Example 3 with SecondaryTable

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

the class JPAXMLOverriddenAnnotationReader method getSecondaryTables.

private SecondaryTables getSecondaryTables(ManagedType root, XMLContext.Default defaults) {
    List<JaxbSecondaryTable> elements = root instanceof JaxbEntity ? ((JaxbEntity) root).getSecondaryTable() : Collections.emptyList();
    List<SecondaryTable> secondaryTables = new ArrayList<>(3);
    for (JaxbSecondaryTable element : elements) {
        AnnotationDescriptor annotation = new AnnotationDescriptor(SecondaryTable.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());
        }
        buildUniqueConstraints(annotation, element.getUniqueConstraint());
        buildIndex(annotation, element.getIndex());
        annotation.setValue("pkJoinColumns", buildPrimaryKeyJoinColumns(element.getPrimaryKeyJoinColumn()));
        secondaryTables.add(AnnotationFactory.create(annotation));
    }
    /*
		 * You can't have both secondary tables in XML and Java,
		 * since there would be no way to "remove" a secondary table
		 */
    if (secondaryTables.size() == 0 && defaults.canUseJavaAnnotations()) {
        SecondaryTable secTableAnn = getPhysicalAnnotation(SecondaryTable.class);
        overridesDefaultInSecondaryTable(secTableAnn, defaults, secondaryTables);
        SecondaryTables secTablesAnn = getPhysicalAnnotation(SecondaryTables.class);
        if (secTablesAnn != null) {
            for (SecondaryTable table : secTablesAnn.value()) {
                overridesDefaultInSecondaryTable(table, defaults, secondaryTables);
            }
        }
    }
    if (secondaryTables.size() > 0) {
        AnnotationDescriptor descriptor = new AnnotationDescriptor(SecondaryTables.class);
        descriptor.setValue("value", secondaryTables.toArray(new SecondaryTable[secondaryTables.size()]));
        return AnnotationFactory.create(descriptor);
    } else {
        return null;
    }
}
Also used : AnnotationDescriptor(org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor) SecondaryTables(jakarta.persistence.SecondaryTables) ArrayList(java.util.ArrayList) JaxbSecondaryTable(org.hibernate.boot.jaxb.mapping.spi.JaxbSecondaryTable) SecondaryTable(jakarta.persistence.SecondaryTable) JaxbSecondaryTable(org.hibernate.boot.jaxb.mapping.spi.JaxbSecondaryTable) JaxbEntity(org.hibernate.boot.jaxb.mapping.spi.JaxbEntity)

Aggregations

SecondaryTable (jakarta.persistence.SecondaryTable)3 SecondaryTables (jakarta.persistence.SecondaryTables)2 JoinTable (jakarta.persistence.JoinTable)1 ArrayList (java.util.ArrayList)1 AssertionFailure (org.hibernate.AssertionFailure)1 AnnotationDescriptor (org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor)1 JaxbEntity (org.hibernate.boot.jaxb.mapping.spi.JaxbEntity)1 JaxbSecondaryTable (org.hibernate.boot.jaxb.mapping.spi.JaxbSecondaryTable)1 QualifiedTableName (org.hibernate.boot.model.relational.QualifiedTableName)1 InFlightMetadataCollector (org.hibernate.boot.spi.InFlightMetadataCollector)1 UniqueConstraintHolder (org.hibernate.cfg.UniqueConstraintHolder)1 Join (org.hibernate.mapping.Join)1 Table (org.hibernate.mapping.Table)1