diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/Configuration.java b/hibernate-core/src/main/java/org/hibernate/cfg/Configuration.java index 65db4ee943..6cdead5d34 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/Configuration.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/Configuration.java @@ -1537,7 +1537,9 @@ public class Configuration implements Serializable { Set unbound = new HashSet(); Set unboundNoLogical = new HashSet(); for ( int index = 0; index < size; index++ ) { - final String logicalColumnName = normalizer.normalizeIdentifierQuoting( columnNames[index] ); + String column = columnNames[index]; + boolean columnNameValid = !StringHelper.isEmpty(column); + final String logicalColumnName = columnNameValid ? normalizer.normalizeIdentifierQuoting( column ) : ""; try { final String columnName = createMappings().getPhysicalColumnName( logicalColumnName, table ); columns[index] = new Column( columnName ); @@ -1563,10 +1565,10 @@ public class Configuration implements Serializable { sb.setLength( sb.length() - 2 ); sb.append( ") on table " ).append( table.getName() ).append( ": database column " ); for ( Column column : unbound ) { - sb.append( column.getName() ).append( ", " ); + sb.append("'").append( column.getName() ).append( "', " ); } for ( Column column : unboundNoLogical ) { - sb.append( column.getName() ).append( ", " ); + 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)" ); diff --git a/hibernate-core/src/main/java/org/hibernate/mapping/Column.java b/hibernate-core/src/main/java/org/hibernate/mapping/Column.java index 569b08e30d..4e28ab573a 100644 --- a/hibernate-core/src/main/java/org/hibernate/mapping/Column.java +++ b/hibernate-core/src/main/java/org/hibernate/mapping/Column.java @@ -84,7 +84,7 @@ public class Column implements Selectable, Serializable, Cloneable { } public void setName(String name) { if ( - name.charAt(0)=='`' || + !name.isEmpty() && Dialect.QUOTE.indexOf( name.charAt(0) ) > -1 //TODO: deprecated, remove eventually ) { quoted=true; 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 new file mode 100644 index 0000000000..fd6693ddf8 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/uniqueconstraint/UniqueConstraintValidationTest.java @@ -0,0 +1,65 @@ +package org.hibernate.test.annotations.uniqueconstraint; + +import java.io.Serializable; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +import javax.persistence.UniqueConstraint; + +import org.hibernate.AnnotationException; +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; +import org.hibernate.cfg.Configuration; +import org.hibernate.service.spi.ServiceRegistryImplementor; +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.junit4.BaseUnitTestCase; +import org.junit.Test; + +public class UniqueConstraintValidationTest extends BaseUnitTestCase { + + @Test(expected = AnnotationException.class) + @TestForIssue(jiraKey = "HHH-4084") + public void testUniqueConstraintWithEmptyColumnName() { + buildSessionFactory(EmptyColumnNameEntity.class); + } + + @Test + public void testUniqueConstraintWithEmptyColumnNameList() { + buildSessionFactory(EmptyColumnNameListEntity.class); + } + + @Test(expected = AnnotationException.class) + public void testUniqueConstraintWithNotExistsColumnName() { + buildSessionFactory(NotExistsColumnEntity.class); + } + + private void buildSessionFactory(Class entity) { + Configuration cfg = new Configuration(); + cfg.addAnnotatedClass(entity); + cfg.buildMappings(); + ServiceRegistryImplementor serviceRegistry = (ServiceRegistryImplementor) new StandardServiceRegistryBuilder() + .applySettings(cfg.getProperties()).buildServiceRegistry(); + cfg.buildSessionFactory(serviceRegistry); + } + + @Entity + @Table(name = "tbl_emptycolumnnameentity", uniqueConstraints = @UniqueConstraint(columnNames = "")) + public static class EmptyColumnNameEntity implements Serializable { + @Id + protected Long id; + } + + @Entity + @Table(name = "tbl_emptycolumnnamelistentity", uniqueConstraints = @UniqueConstraint(columnNames = {})) + public static class EmptyColumnNameListEntity implements Serializable { + @Id + protected Long id; + } + + @Entity + @Table(name = "tbl_notexistscolumnentity", uniqueConstraints = @UniqueConstraint(columnNames = "notExists")) + public static class NotExistsColumnEntity implements Serializable { + @Id + protected Long id; + } +}