From e84ed199e36dc46a8693b85e0aec6bf6499b8f6d Mon Sep 17 00:00:00 2001 From: Brett Meyer Date: Mon, 17 Feb 2014 15:03:28 -0500 Subject: [PATCH] HHH-8737 throw MappingException for constraint with non-existent column --- .../hibernate/metamodel/internal/Binder.java | 34 ++++++++----------- .../ConstraintNamingStrategyHelper.java | 1 - .../metamodel/internal/TableHelper.java | 16 +++++++++ .../test/annotations/backquotes/Bug.java | 2 +- .../UniqueConstraintValidationTest.java | 9 ++--- 5 files changed, 35 insertions(+), 27 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/Binder.java b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/Binder.java index 9222d24366..bf6d47bcd1 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/Binder.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/Binder.java @@ -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 columns = new ArrayList(); - 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 columns = new ArrayList(); + 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 columns = new ArrayList(); - for ( final String columnName : indexConstraintSource.columnNames() ) { - columns.add( tableHelper.locateOrCreateColumn( table, columnName, - new ColumnNamingStrategyHelper( null, false ) ) ); - } tableHelper.createIndex( table, columns, indexConstraintSource.name(), indexConstraintSource.isUnique() ); } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/ConstraintNamingStrategyHelper.java b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/ConstraintNamingStrategyHelper.java index 9683d7731d..0513c778cc 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/ConstraintNamingStrategyHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/ConstraintNamingStrategyHelper.java @@ -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; diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/TableHelper.java b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/TableHelper.java index b2788b82ba..58379e5054 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/TableHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/TableHelper.java @@ -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, diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/backquotes/Bug.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/backquotes/Bug.java index 8f87065b0a..fbc21ae126 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/backquotes/Bug.java +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/backquotes/Bug.java @@ -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 diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/uniqueconstraint/UniqueConstraintValidationTest.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/uniqueconstraint/UniqueConstraintValidationTest.java index 35e0d9f02f..7cddc9283a 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/uniqueconstraint/UniqueConstraintValidationTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/uniqueconstraint/UniqueConstraintValidationTest.java @@ -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); }