HHH-8737 throw MappingException for constraint with non-existent

column
This commit is contained in:
Brett Meyer 2014-02-17 15:03:28 -05:00
parent ea4812b046
commit e84ed199e3
5 changed files with 35 additions and 27 deletions

View File

@ -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;
@ -37,6 +39,7 @@ import java.util.Set;
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;
@ -163,8 +166,6 @@ import org.hibernate.tuple.entity.EntityTuplizer;
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.
*
@ -1119,28 +1120,23 @@ public class Binder implements HelperContext {
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() );
}

View File

@ -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;

View File

@ -149,6 +149,22 @@ public class TableHelper {
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,

View File

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

View File

@ -7,11 +7,10 @@ import javax.persistence.Id;
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;
@ -22,9 +21,8 @@ import org.junit.Test;
*/
public class UniqueConstraintValidationTest extends BaseUnitTestCase {
@Test(expected = AnnotationException.class)
@Test(expected = MappingException.class)
@TestForIssue(jiraKey = "HHH-4084")
@FailureExpectedWithNewMetamodel
public void testUniqueConstraintWithEmptyColumnName() {
buildSessionFactory(EmptyColumnNameEntity.class);
}
@ -34,8 +32,7 @@ public class UniqueConstraintValidationTest extends BaseUnitTestCase {
buildSessionFactory(EmptyColumnNameListEntity.class);
}
@Test(expected = AnnotationException.class)
@FailureExpectedWithNewMetamodel
@Test(expected = MappingException.class)
public void testUniqueConstraintWithNotExistsColumnName() {
buildSessionFactory(NotExistsColumnEntity.class);
}