Skip to content

Commit

Permalink
HHH-8737 throw MappingException for constraint with non-existent
Browse files Browse the repository at this point in the history
column
  • Loading branch information
brmeyer committed Feb 17, 2014
1 parent ea4812b commit e84ed19
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 27 deletions.
Expand Up @@ -23,6 +23,8 @@
*/
package org.hibernate.metamodel.internal;

import static org.hibernate.engine.spi.SyntheticAttributeHelper.SYNTHETIC_COMPOSITE_ID_ATTRIBUTE_NAME;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
Expand All @@ -37,6 +39,7 @@
import org.hibernate.AssertionFailure;
import org.hibernate.EntityMode;
import org.hibernate.FetchMode;
import org.hibernate.MappingException;
import org.hibernate.MultiTenancyStrategy;
import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
import org.hibernate.cfg.AvailableSettings;
Expand Down Expand Up @@ -163,8 +166,6 @@
import org.hibernate.type.ForeignKeyDirection;
import org.hibernate.type.Type;

import static org.hibernate.engine.spi.SyntheticAttributeHelper.SYNTHETIC_COMPOSITE_ID_ATTRIBUTE_NAME;

/**
* The common binder shared between annotations and {@code hbm.xml} processing.
*
Expand Down Expand Up @@ -1119,28 +1120,23 @@ public void execute(LocalBindingContextExecutionContext bindingContextContext) {
final EntityBinding entityBinding = bindingContextContext.getEntityBinding();
final EntitySource entitySource = bindingContextContext.getEntitySource();
for ( final ConstraintSource constraintSource : entitySource.getConstraints() ) {
if ( UniqueConstraintSource.class.isInstance( constraintSource ) ) {
final UniqueConstraintSource uniqueConstraintSource = (UniqueConstraintSource) constraintSource;

final TableSpecification table = findConstraintTable( entityBinding, constraintSource.getTableName() );

final List<Column> columns = new ArrayList<Column>();
for ( final String columnName : uniqueConstraintSource.columnNames() ) {
columns.add( tableHelper.locateOrCreateColumn( table, columnName,
new ColumnNamingStrategyHelper( null, false ) ) );
final TableSpecification table = findConstraintTable( entityBinding, constraintSource.getTableName() );
final List<Column> columns = new ArrayList<Column>();
for ( final String columnName : constraintSource.columnNames() ) {
final Column column = tableHelper.locateColumn( table, columnName,
new ColumnNamingStrategyHelper( null, false ) );
if (column == null) {
throw new MappingException( "While creating a constraint, could not find column "
+ columnName + " on table "+ table.getLogicalName().getText() );
}
columns.add( column );
}

if ( UniqueConstraintSource.class.isInstance( constraintSource ) ) {
tableHelper.createUniqueKey( table, columns, constraintSource.name() );
}
else if ( IndexConstraintSource.class.isInstance( constraintSource ) ) {
final IndexConstraintSource indexConstraintSource = (IndexConstraintSource) constraintSource;

final TableSpecification table = findConstraintTable( entityBinding, constraintSource.getTableName() );

final List<Column> columns = new ArrayList<Column>();
for ( final String columnName : indexConstraintSource.columnNames() ) {
columns.add( tableHelper.locateOrCreateColumn( table, columnName,
new ColumnNamingStrategyHelper( null, false ) ) );
}
tableHelper.createIndex( table, columns, indexConstraintSource.name(),
indexConstraintSource.isUnique() );
}
Expand Down
Expand Up @@ -24,7 +24,6 @@
package org.hibernate.metamodel.internal;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.hibernate.cfg.NamingStrategy;
Expand Down
Expand Up @@ -149,6 +149,22 @@ public Column locateOrCreateColumn(
return table.locateOrCreateColumn( resolvedColumnName );
}

public Column locateColumn(
final TableSpecification table,
final String columnName,
final ObjectNameNormalizer.NamingStrategyHelper namingStrategyHelper) {
if ( columnName == null && namingStrategyHelper == null ) {
throw bindingContext().makeMappingException(
"Cannot resolve name for column because no name was specified and namingStrategyHelper is null."
);
}
final String resolvedColumnName = normalizeDatabaseIdentifier(
columnName,
namingStrategyHelper
);
return table.locateColumn( resolvedColumnName );
}

public Column locateOrCreateColumn(
final TableSpecification table,
final ColumnSource columnSource,
Expand Down
Expand Up @@ -11,7 +11,7 @@
import javax.persistence.Table;

@Entity
@Table(indexes = @Index(name="`titleindex`", columnList = "title"))
@Table(indexes = @Index(name="`titleindex`", columnList = "`title`"))
public class Bug
{
@Id
Expand Down
Expand Up @@ -7,11 +7,10 @@
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

import org.hibernate.AnnotationException;
import org.hibernate.MappingException;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.spi.ServiceRegistryImplementor;
import org.hibernate.testing.FailureExpectedWithNewMetamodel;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseUnitTestCase;
import org.junit.Test;
Expand All @@ -22,9 +21,8 @@
*/
public class UniqueConstraintValidationTest extends BaseUnitTestCase {

@Test(expected = AnnotationException.class)
@Test(expected = MappingException.class)
@TestForIssue(jiraKey = "HHH-4084")
@FailureExpectedWithNewMetamodel
public void testUniqueConstraintWithEmptyColumnName() {
buildSessionFactory(EmptyColumnNameEntity.class);
}
Expand All @@ -34,8 +32,7 @@ public void testUniqueConstraintWithEmptyColumnNameList() {
buildSessionFactory(EmptyColumnNameListEntity.class);
}

@Test(expected = AnnotationException.class)
@FailureExpectedWithNewMetamodel
@Test(expected = MappingException.class)
public void testUniqueConstraintWithNotExistsColumnName() {
buildSessionFactory(NotExistsColumnEntity.class);
}
Expand Down

0 comments on commit e84ed19

Please sign in to comment.