Package org.hibernate.mapping

Examples of org.hibernate.mapping.UniqueKey


  private void buildUniqueKeyFromColumnNames(Table table, String keyName, String[] columnNames) {
    ExtendedMappings mappings = createExtendedMappings();
    keyName = mappings.getObjectNameNormalizer().normalizeIdentifierQuoting( keyName );

    UniqueKey uc;
    int size = columnNames.length;
    Column[] columns = new Column[size];
    Set<Column> unbound = new HashSet<Column>();
    Set<Column> unboundNoLogical = new HashSet<Column>();
    for ( int index = 0; index < size; index++ ) {
      final String logicalColumnName = mappings.getObjectNameNormalizer()
          .normalizeIdentifierQuoting( columnNames[index] );
      try {
        final String columnName = mappings.getPhysicalColumnName( logicalColumnName, table );
        columns[index] = new Column( columnName );
        unbound.add( columns[index] );
        //column equals and hashcode is based on column name
      }
      catch ( MappingException e ) {
        unboundNoLogical.add( new Column( logicalColumnName ) );
      }
    }
    for ( Column column : columns ) {
      if ( table.containsColumn( column ) ) {
        uc = table.getOrCreateUniqueKey( keyName );
        uc.addColumn( table.getColumn( column ) );
        unbound.remove( column );
      }
    }
    if ( unbound.size() > 0 || unboundNoLogical.size() > 0 ) {
      StringBuilder sb = new StringBuilder( "Unable to create unique key constraint (" );
View Full Code Here


      if ( table.isPhysicalTable() ) {

        if ( !dialect.supportsUniqueConstraintInCreateAlterTable() ) {
          Iterator subIter = table.getUniqueKeyIterator();
          while ( subIter.hasNext() ) {
            UniqueKey uk = (UniqueKey) subIter.next();
            String constraintString = uk.sqlCreateString( dialect, mapping, defaultCatalog, defaultSchema );
            if (constraintString != null) script.add( constraintString );
          }
        }

View Full Code Here

  }

  private void buildUniqueKeyFromColumnNames(Table table, String keyName, String[] columnNames) {
    keyName = normalizer.normalizeIdentifierQuoting( keyName );

    UniqueKey uc;
    int size = columnNames.length;
    Column[] columns = new Column[size];
    Set<Column> unbound = new HashSet<Column>();
    Set<Column> unboundNoLogical = new HashSet<Column>();
    for ( int index = 0; index < size; index++ ) {
      final String logicalColumnName = normalizer.normalizeIdentifierQuoting( columnNames[index] );
      try {
        final String columnName = createMappings().getPhysicalColumnName( logicalColumnName, table );
        columns[index] = new Column( columnName );
        unbound.add( columns[index] );
        //column equals and hashcode is based on column name
      }
      catch ( MappingException e ) {
        unboundNoLogical.add( new Column( logicalColumnName ) );
      }
    }
    for ( Column column : columns ) {
      if ( table.containsColumn( column ) ) {
        uc = table.getOrCreateUniqueKey( keyName );
        uc.addColumn( table.getColumn( column ) );
        unbound.remove( column );
      }
    }
    if ( unbound.size() > 0 || unboundNoLogical.size() > 0 ) {
      StringBuilder sb = new StringBuilder( "Unable to create unique key constraint (" );
View Full Code Here

      if ( table.isPhysicalTable() ) {

        if ( !dialect.supportsUniqueConstraintInCreateAlterTable() ) {
          Iterator subIter = table.getUniqueKeyIterator();
          while ( subIter.hasNext() ) {
            UniqueKey uk = (UniqueKey) subIter.next();
            String constraintString = uk.sqlCreateString( dialect, mapping, defaultCatalog, defaultSchema );
            if (constraintString != null) script.add( constraintString );
          }
        }

View Full Code Here

      }
      else if ( "filter".equals( name ) ) {
        parseFilter( subnode, persistentClass, mappings );
      }
      else if ( "natural-id".equals( name ) ) {
        UniqueKey uk = new UniqueKey();
        uk.setName("_UniqueKey");
        uk.setTable(table);
        //by default, natural-ids are "immutable" (constant)
        boolean mutableId = "true".equals( subnode.attributeValue("mutable") );
        createClassProperties(
            subnode,
            persistentClass,
View Full Code Here

          boolean unique = !((Boolean)indexRs.get("NON_UNIQUE")).booleanValue();
         
          if (columnName != null || indexName != null) { // both can be non-null with statistical indexs which we don't have any use for.
           
            if(unique) {
              UniqueKey key = (UniqueKey) uniquekeys.get(indexName);
              if (key==null) {
                key = new UniqueKey();
                key.setName(indexName);
                key.setTable(table);
                table.addUniqueKey(key);             
                uniquekeys.put(indexName, key);
              }
         
              if(indexes.containsKey(indexName) ) {
                throw new JDBCBinderException("UniqueKey exists also as Index! ");
              }
              Column column = getColumn(table, columnName);
              key.addColumn(column);
             
              if (unique && key.getColumnSpan()==1) {
                // make list of columns that has the chance of being unique
                List l = (List) uniqueColumns.get(column);
                if (l == null) {
                  l = new ArrayList();
                  uniqueColumns.put(column, l);
                }
                l.add(key);
              }
            }
            else {
              Index index = (Index) indexes.get(indexName);
              if(index==null) {
                index = new Index();
                index.setName(indexName);
                index.setTable(table);
                table.addIndex(index);
                indexes.put(indexName, index);         
              }
             
              if(uniquekeys.containsKey(indexName) ) {
                throw new JDBCBinderException("Index exists also as Unique! ");
              }
              Column column = getColumn(table, columnName);
              index.addColumn(column);
            }
           
          }
          else {
            if(DatabaseMetaData.tableIndexStatistic != ((Short)indexRs.get("TYPE")).shortValue() ) {
              log.warn("Index was not statistical, but no column name was found in " + indexName);
            }
             
          }               
        }
      }
      catch (JDBCException t) {
        log.warn("Exception while trying to get indexinfo on " + Table.qualify(table.getCatalog(), table.getSchema(), table.getName() ) "=" + t.getMessage() );
        // Bug #604761 Oracle getIndexInfo() needs major grants And other dbs sucks too ;)
        // http://sourceforge.net/tracker/index.php?func=detail&aid=604761&group_id=36044&atid=415990       
      }
      finally {
        if (indexIterator != null) {
          try {
            getMetaDataDialect().close(indexIterator);
          } catch(JDBCException se) {
            log.warn("Exception while trying to close resultset for index meta data",se);
          }
        }
      }
     
      // mark columns that are unique TODO: multiple columns are not unique on their own.
      Iterator iterator = uniqueColumns.entrySet().iterator();
      while (iterator.hasNext() ) {
        Map.Entry entry = (Map.Entry) iterator.next();
        Column col = (Column) entry.getKey();
        Iterator keys = ( (List)entry.getValue() ).iterator();
         while (keys.hasNext() ) {
          UniqueKey key = (UniqueKey) keys.next();
       
          if(key.getColumnSpan()==1) {
            col.setUnique(true);
          }
        }
      }
     
View Full Code Here

  protected String generateAnnTableUniqueConstraint(Table table) {
    Iterator uniqueKeys = table.getUniqueKeyIterator();
    List cons = new ArrayList();
    while ( uniqueKeys.hasNext() ) {
      UniqueKey key = (UniqueKey) uniqueKeys.next();
      if (table.hasPrimaryKey() && table.getPrimaryKey().getColumns().equals(key.getColumns())) {
        continue;
      }
      AnnotationBuilder constraint = AnnotationBuilder.createAnnotation( importType("javax.persistence.UniqueConstraint") );
      constraint.addQuotedAttributes( "columnNames", new IteratorTransformer(key.getColumnIterator()) {
        public Object transform(Object object) {
          return ((Column)object).getName();
        }
      });
      cons.add( constraint.getResult() );
View Full Code Here

      if ( table.isPhysicalTable() ) {

        if ( !dialect.supportsUniqueConstraintInCreateAlterTable() ) {
          Iterator subIter = table.getUniqueKeyIterator();
          while ( subIter.hasNext() ) {
            UniqueKey uk = (UniqueKey) subIter.next();
            String constraintString = uk.sqlCreateString( dialect, mapping, defaultCatalog, defaultSchema );
            if (constraintString != null) script.add( constraintString );
          }
        }

View Full Code Here

      if ( table.isPhysicalTable() ) {

        if ( !dialect.supportsUniqueConstraintInCreateAlterTable() ) {
          Iterator subIter = table.getUniqueKeyIterator();
          while ( subIter.hasNext() ) {
            UniqueKey uk = (UniqueKey) subIter.next();
            String constraintString = uk.sqlCreateString( dialect, mapping, defaultCatalog, defaultSchema );
            if (constraintString != null) script.add( constraintString );
          }
        }

View Full Code Here

      if ( table.isPhysicalTable() ) {

        if ( !dialect.supportsUniqueConstraintInCreateAlterTable() ) {
          Iterator subIter = table.getUniqueKeyIterator();
          while ( subIter.hasNext() ) {
            UniqueKey uk = (UniqueKey) subIter.next();
            String constraintString = uk.sqlCreateString( dialect, mapping, defaultCatalog, defaultSchema );
            if (constraintString != null) script.add( constraintString );
          }
        }

View Full Code Here

TOP

Related Classes of org.hibernate.mapping.UniqueKey

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.