Search in sources :

Example 1 with UniqueConstraintHolder

use of org.hibernate.cfg.UniqueConstraintHolder in project hibernate-orm by hibernate.

the class InFlightMetadataCollectorImpl method addUniqueConstraints.

@Override
@SuppressWarnings({ "unchecked" })
public void addUniqueConstraints(Table table, List uniqueConstraints) {
    List<UniqueConstraintHolder> constraintHolders = new ArrayList<UniqueConstraintHolder>(CollectionHelper.determineProperSizing(uniqueConstraints.size()));
    int keyNameBase = determineCurrentNumberOfUniqueConstraintHolders(table);
    for (String[] columns : (List<String[]>) uniqueConstraints) {
        final String keyName = "key" + keyNameBase++;
        constraintHolders.add(new UniqueConstraintHolder().setName(keyName).setColumns(columns));
    }
    addUniqueConstraintHolders(table, constraintHolders);
}
Also used : UniqueConstraintHolder(org.hibernate.cfg.UniqueConstraintHolder) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList)

Example 2 with UniqueConstraintHolder

use of org.hibernate.cfg.UniqueConstraintHolder in project hibernate-orm by hibernate.

the class InFlightMetadataCollectorImpl method processUniqueConstraintHolders.

private void processUniqueConstraintHolders(MetadataBuildingContext buildingContext) {
    if (uniqueConstraintHoldersByTable == null) {
        return;
    }
    for (Map.Entry<Table, List<UniqueConstraintHolder>> tableListEntry : uniqueConstraintHoldersByTable.entrySet()) {
        final Table table = tableListEntry.getKey();
        final List<UniqueConstraintHolder> uniqueConstraints = tableListEntry.getValue();
        for (UniqueConstraintHolder holder : uniqueConstraints) {
            buildUniqueKeyFromColumnNames(table, holder.getName(), holder.getColumns(), buildingContext);
        }
    }
    uniqueConstraintHoldersByTable.clear();
}
Also used : UniqueConstraintHolder(org.hibernate.cfg.UniqueConstraintHolder) DenormalizedTable(org.hibernate.mapping.DenormalizedTable) Table(org.hibernate.mapping.Table) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap)

Example 3 with UniqueConstraintHolder

use of org.hibernate.cfg.UniqueConstraintHolder 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 SecondaryTableNameSource secondaryTableNameContext;
    final Object joinColumns;
    final List<UniqueConstraintHolder> uniqueConstraintHolders;
    final Identifier logicalName;
    if (secondaryTable != null) {
        schema = secondaryTable.schema();
        catalog = secondaryTable.catalog();
        logicalName = 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 = 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, 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 : UniqueConstraintHolder(org.hibernate.cfg.UniqueConstraintHolder) AssertionFailure(org.hibernate.AssertionFailure) JoinTable(javax.persistence.JoinTable) SecondaryTable(javax.persistence.SecondaryTable) Table(org.hibernate.mapping.Table) Join(org.hibernate.mapping.Join) Identifier(org.hibernate.boot.model.naming.Identifier) InFlightMetadataCollector(org.hibernate.boot.spi.InFlightMetadataCollector)

Aggregations

UniqueConstraintHolder (org.hibernate.cfg.UniqueConstraintHolder)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Table (org.hibernate.mapping.Table)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 JoinTable (javax.persistence.JoinTable)1 SecondaryTable (javax.persistence.SecondaryTable)1 AssertionFailure (org.hibernate.AssertionFailure)1 Identifier (org.hibernate.boot.model.naming.Identifier)1 InFlightMetadataCollector (org.hibernate.boot.spi.InFlightMetadataCollector)1 DenormalizedTable (org.hibernate.mapping.DenormalizedTable)1 Join (org.hibernate.mapping.Join)1