private boolean isSameAsPrimaryKeyColumns(UniqueKey uniqueKey) { if ( primaryKey == null || ! primaryKey.columnIterator().hasNext() ) { // happens for many-to-many tables return false; } return primaryKey.getColumns().containsAll( uniqueKey.getColumns() ) && uniqueKey.getColumns().containsAll( primaryKey.getColumns() ); }
@Override public Iterator getUniqueKeyIterator() { Iterator iter = includedTable.getUniqueKeyIterator(); while ( iter.hasNext() ) { UniqueKey uk = (UniqueKey) iter.next(); createUniqueKey( uk.getColumns() ); } return getUniqueKeys().values().iterator(); }
continue; if ( otherUniqueKey.getColumns().containsAll( uniqueKey.getColumns() ) && uniqueKey.getColumns().containsAll( otherUniqueKey.getColumns() ) ) { removeIt = true; break;
@Test @TestForIssue(jiraKey = "HHH-7890") public void testQuotedUniqueConstraint() { Iterator<UniqueKey> itr = metadata().getEntityBinding( Person.class.getName() ) .getTable().getUniqueKeyIterator(); while ( itr.hasNext() ) { UniqueKey uk = itr.next(); assertEquals( uk.getColumns().size(), 1 ); assertEquals( uk.getColumn( 0 ).getName(), "name"); return; } fail( "GLOBALLY_QUOTED_IDENTIFIERS caused the unique key creation to fail." ); }
@Test public void testTableIndex() { PersistentClass entity = metadata().getEntityBinding( Car.class.getName() ); Iterator itr = entity.getTable().getUniqueKeyIterator(); assertTrue( itr.hasNext() ); UniqueKey uk = (UniqueKey) itr.next(); assertFalse( itr.hasNext() ); assertTrue( StringHelper.isNotEmpty( uk.getName() ) ); assertEquals( 2, uk.getColumnSpan() ); Column column = (Column) uk.getColumns().get( 0 ); assertEquals( "brand", column.getName() ); column = (Column) uk.getColumns().get( 1 ); assertEquals( "producer", column.getName() ); assertSame( entity.getTable(), uk.getTable() ); itr = entity.getTable().getIndexIterator(); assertTrue( itr.hasNext() ); Index index = (Index)itr.next(); assertFalse( itr.hasNext() ); assertEquals( "Car_idx", index.getName() ); assertEquals( 1, index.getColumnSpan() ); column = index.getColumnIterator().next(); assertEquals( "since", column.getName() ); assertSame( entity.getTable(), index.getTable() ); }
Map getUniqueKeys() { if ( uniqueKeys.size() > 1 ) { //deduplicate unique constraints sharing the same columns //this is needed by Hibernate Annotations since it creates automagically // unique constraints for the user Iterator it = uniqueKeys.entrySet().iterator(); Map finalUniqueKeys = new HashMap( uniqueKeys.size() ); while ( it.hasNext() ) { Map.Entry entry = (Map.Entry) it.next(); UniqueKey uk = (UniqueKey) entry.getValue(); List columns = uk.getColumns(); int size = finalUniqueKeys.size(); boolean skip = false; Iterator tempUks = finalUniqueKeys.entrySet().iterator(); while ( tempUks.hasNext() ) { final UniqueKey currentUk = (UniqueKey) ( (Map.Entry) tempUks.next() ).getValue(); if ( currentUk.getColumns().containsAll( columns ) && columns .containsAll( currentUk.getColumns() ) ) { skip = true; break; } } if ( !skip ) finalUniqueKeys.put( entry.getKey(), uk ); } return finalUniqueKeys; } else { return uniqueKeys; } }
Map getUniqueKeys() { if ( uniqueKeys.size() > 1 ) { //deduplicate unique constraints sharing the same columns //this is needed by Hibernate Annotations since it creates automagically // unique constraints for the user Iterator it = uniqueKeys.entrySet().iterator(); Map finalUniqueKeys = new HashMap( uniqueKeys.size() ); while ( it.hasNext() ) { Map.Entry entry = (Map.Entry) it.next(); UniqueKey uk = (UniqueKey) entry.getValue(); List columns = uk.getColumns(); int size = finalUniqueKeys.size(); boolean skip = false; Iterator tempUks = finalUniqueKeys.entrySet().iterator(); while ( tempUks.hasNext() ) { final UniqueKey currentUk = (UniqueKey) ( (Map.Entry) tempUks.next() ).getValue(); if ( currentUk.getColumns().containsAll( columns ) && columns .containsAll( currentUk.getColumns() ) ) { skip = true; break; } } if ( !skip ) finalUniqueKeys.put( entry.getKey(), uk ); } return finalUniqueKeys; } else { return uniqueKeys; } }
Map getUniqueKeys() { if ( uniqueKeys.size() > 1 ) { //deduplicate unique constraints sharing the same columns //this is needed by Hibernate Annotations since it creates automagically // unique constraints for the user Iterator it = uniqueKeys.entrySet().iterator(); Map finalUniqueKeys = new HashMap( uniqueKeys.size() ); while ( it.hasNext() ) { Map.Entry entry = (Map.Entry) it.next(); UniqueKey uk = (UniqueKey) entry.getValue(); List columns = uk.getColumns(); int size = finalUniqueKeys.size(); boolean skip = false; Iterator tempUks = finalUniqueKeys.entrySet().iterator(); while ( tempUks.hasNext() ) { final UniqueKey currentUk = (UniqueKey) ( (Map.Entry) tempUks.next() ).getValue(); if ( currentUk.getColumns().containsAll( columns ) && columns .containsAll( currentUk.getColumns() ) ) { skip = true; break; } } if ( !skip ) finalUniqueKeys.put( entry.getKey(), uk ); } return finalUniqueKeys; } else { return uniqueKeys; } }
private static void createUniqueKeyForColumns(Table table, String columnName, List<Column> keyList) { Collections.reverse(keyList); UniqueKey key = table.getOrCreateUniqueKey("unique-" + columnName); List<?> columns = key.getColumns(); if (columns.isEmpty()) { LOG.debug("create unique key for " + table.getName() + " columns = " + keyList); key.addColumns(keyList.iterator()); } }
protected void setGeneratedUniqueName(UniqueKey uk) { StringBuilder sb = new StringBuilder(uk.getTable().getName()).append('_'); for (Object col : uk.getColumns()) { sb.append(((Column) col).getName()).append('_'); } MessageDigest md; try { md = MessageDigest.getInstance("MD5"); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } try { md.update(sb.toString().getBytes("UTF-8")); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } String name = "UK" + new BigInteger(1, md.digest()).toString(16); if (name.length() > 30) { // Oracle has a 30-char limit name = name.substring(0, 30); } uk.setName(name); }
protected String generateAnnTableUniqueConstraint(Table table) { Iterator<UniqueKey> uniqueKeys = table.getUniqueKeyIterator(); List<String> cons = new ArrayList<String>(); 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<Column>(key.getColumnIterator()) { public String transform(Column column) { return column.getName(); } }); cons.add( constraint.getResult() ); } AnnotationBuilder builder = AnnotationBuilder.createAnnotation( "dummyAnnotation" ); builder.addAttributes( "dummyAttribute", cons.iterator() ); String attributeAsString = builder.getAttributeAsString( "dummyAttribute" ); return attributeAsString==null?"":attributeAsString; }
protected String generateAnnTableUniqueConstraint(Table table) { Iterator<UniqueKey> uniqueKeys = table.getUniqueKeyIterator(); List<String> cons = new ArrayList<String>(); 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<Column>(key.getColumnIterator()) { public String transform(Column column) { return column.getName(); } }); cons.add( constraint.getResult() ); } AnnotationBuilder builder = AnnotationBuilder.createAnnotation( "dummyAnnotation" ); builder.addAttributes( "dummyAttribute", cons.iterator() ); String attributeAsString = builder.getAttributeAsString( "dummyAttribute" ); return attributeAsString==null?"":attributeAsString; }