use of org.hibernate.boot.model.naming.ImplicitUniqueKeyNameSource in project hibernate-orm by hibernate.
the class InFlightMetadataCollectorImpl method buildUniqueKeyFromColumnNames.
private void buildUniqueKeyFromColumnNames(final Table table, String keyName, final String[] columnNames, String[] orderings, boolean unique, final MetadataBuildingContext buildingContext) {
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 = columnNames[index];
try {
final String physicalColumnName = getPhysicalColumnName(table, logicalColumnName);
columns[index] = new Column(physicalColumnName);
unbound.add(columns[index]);
//column equals and hashcode is based on column name
} catch (MappingException e) {
// If at least 1 columnName does exist, 'columns' will contain a mix of Columns and nulls. In order
// to exhaustively report all of the unbound columns at once, w/o an NPE in
// Constraint#generateName's array sorting, simply create a fake Column.
columns[index] = new Column(logicalColumnName);
unboundNoLogical.add(columns[index]);
}
}
final String originalKeyName = keyName;
if (unique) {
final Identifier keyNameIdentifier = getMetadataBuildingOptions().getImplicitNamingStrategy().determineUniqueKeyName(new ImplicitUniqueKeyNameSource() {
@Override
public MetadataBuildingContext getBuildingContext() {
return buildingContext;
}
@Override
public Identifier getTableName() {
return table.getNameIdentifier();
}
private List<Identifier> columnNameIdentifiers;
@Override
public List<Identifier> getColumnNames() {
// be lazy about building these
if (columnNameIdentifiers == null) {
columnNameIdentifiers = toIdentifiers(columnNames);
}
return columnNameIdentifiers;
}
@Override
public Identifier getUserProvidedIdentifier() {
return originalKeyName != null ? Identifier.toIdentifier(originalKeyName) : null;
}
});
keyName = keyNameIdentifier.render(getDatabase().getJdbcEnvironment().getDialect());
UniqueKey uk = table.getOrCreateUniqueKey(keyName);
for (int i = 0; i < columns.length; i++) {
Column column = columns[i];
String order = orderings != null ? orderings[i] : null;
if (table.containsColumn(column)) {
uk.addColumn(column, order);
unbound.remove(column);
}
}
} else {
final Identifier keyNameIdentifier = getMetadataBuildingOptions().getImplicitNamingStrategy().determineIndexName(new ImplicitIndexNameSource() {
@Override
public MetadataBuildingContext getBuildingContext() {
return buildingContext;
}
@Override
public Identifier getTableName() {
return table.getNameIdentifier();
}
private List<Identifier> columnNameIdentifiers;
@Override
public List<Identifier> getColumnNames() {
// be lazy about building these
if (columnNameIdentifiers == null) {
columnNameIdentifiers = toIdentifiers(columnNames);
}
return columnNameIdentifiers;
}
@Override
public Identifier getUserProvidedIdentifier() {
return originalKeyName != null ? Identifier.toIdentifier(originalKeyName) : null;
}
});
keyName = keyNameIdentifier.render(getDatabase().getJdbcEnvironment().getDialect());
Index index = table.getOrCreateIndex(keyName);
for (int i = 0; i < columns.length; i++) {
Column column = columns[i];
String order = orderings != null ? orderings[i] : null;
if (table.containsColumn(column)) {
index.addColumn(column, order);
unbound.remove(column);
}
}
}
if (unbound.size() > 0 || unboundNoLogical.size() > 0) {
StringBuilder sb = new StringBuilder("Unable to create ");
if (unique) {
sb.append("unique key constraint (");
} else {
sb.append("index (");
}
for (String columnName : columnNames) {
sb.append(columnName).append(", ");
}
sb.setLength(sb.length() - 2);
sb.append(") on table ").append(table.getName()).append(": database column ");
for (Column column : unbound) {
sb.append("'").append(column.getName()).append("', ");
}
for (Column column : unboundNoLogical) {
sb.append("'").append(column.getName()).append("', ");
}
sb.setLength(sb.length() - 2);
sb.append(" not found. Make sure that you use the correct column name which depends on the naming strategy in use (it may not be the same as the property name in the entity, especially for relational types)");
throw new AnnotationException(sb.toString());
}
}
Aggregations