HHH-4084 @UniqueConstraint(columnNames="") causes StringIndexOutOfBoundsException

This commit is contained in:
Nikolay Shestakov 2013-01-19 19:32:15 +06:00 committed by Brett Meyer
parent 328986f392
commit 5cc0864562
3 changed files with 71 additions and 4 deletions

View File

@ -1537,7 +1537,9 @@ public class Configuration implements Serializable {
Set<Column> unbound = new HashSet<Column>();
Set<Column> unboundNoLogical = new HashSet<Column>();
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)" );

View File

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

View File

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