Skip to content

Commit

Permalink
HHH-8537 @UniqueConstraint naming non-existent column leads to NPE
Browse files Browse the repository at this point in the history
Conflicts:
	hibernate-core/src/test/java/org/hibernate/test/annotations/uniqueconstraint/UniqueConstraintTest.java
  • Loading branch information
brmeyer committed Sep 27, 2013
1 parent 1a3be54 commit d42f66d
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 5 deletions.
Expand Up @@ -1585,7 +1585,11 @@ private void buildUniqueKeyFromColumnNames(Table table, String keyName, String[]
//column equals and hashcode is based on column name
}
catch ( MappingException e ) {
unboundNoLogical.add( new Column( column ) );
// If at least 1 columnName does exist, 'columns' will contain a mix of Columns and nulls. In order
// to exhaustively report all of the unbound columns at once, w/o an NPE in
// Constraint#generateName's array sorting, simply create a fake Column.
columns[index] = new Column( column );
unboundNoLogical.add( columns[index] );
}
}

Expand Down
Expand Up @@ -3,34 +3,42 @@
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.fail;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

import org.hibernate.AnnotationException;
import org.hibernate.JDBCException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.mapping.UniqueKey;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test;

/**
* @author Manuel Bernhardt <bernhardt.manuel@gmail.com>
* @author Brett Meyer
*/
public class UniqueConstraintTest extends BaseCoreFunctionalTestCase {

protected Class[] getAnnotatedClasses() {
return new Class[]{
Room.class,
Building.class,
House.class,
UniqueNoNameA.class,
UniqueNoNameB.class
House.class
};
}

Expand Down Expand Up @@ -69,7 +77,11 @@ public void testUniquenessConstraintWithSuperclassProperty() throws Exception {
@Test
@TestForIssue( jiraKey = "HHH-8026" )
public void testUnNamedConstraints() {
Iterator<org.hibernate.mapping.Table> iterator = configuration().getTableMappings();
Configuration cfg = new Configuration();
cfg.addAnnotatedClass( UniqueNoNameA.class );
cfg.addAnnotatedClass( UniqueNoNameB.class );
cfg.buildMappings();
Iterator<org.hibernate.mapping.Table> iterator = cfg.getTableMappings();
org.hibernate.mapping.Table tableA = null;
org.hibernate.mapping.Table tableB = null;
while( iterator.hasNext() ) {
Expand All @@ -91,6 +103,22 @@ else if ( table.getName().equals( "UniqueNoNameB" ) ) {
assertFalse( ukA.getName().equals( ukB.getName() ) );
}

@Test
@TestForIssue( jiraKey = "HHH-8537" )
public void testNonExistentColumn() {
Configuration cfg = new Configuration();
cfg.addAnnotatedClass( UniqueColumnDoesNotExist.class );
try {
cfg.buildMappings();
}
catch (NullPointerException e) {
fail( "The @UniqueConstraint with a non-existent column name should have resulted in an AnnotationException" );
}
catch (AnnotationException e) {
// expected
}
}

@Entity
@Table( name = "UniqueNoNameA",
uniqueConstraints = {@UniqueConstraint(columnNames={"name"})})
Expand All @@ -112,5 +140,21 @@ public static class UniqueNoNameB {

public String name;
}

@Entity
public static class UniqueColumnDoesNotExist {
@Id
public Integer id;

@ElementCollection(fetch = FetchType.EAGER)
@CollectionTable(
name = "tbl_strings",
joinColumns = @JoinColumn(name = "fk", nullable = false),
// the failure required at least 1 columnName to be correct -- all incorrect wouldn't reproduce
uniqueConstraints = @UniqueConstraint(columnNames = { "fk", "doesnotexist" })
)
@Column(name = "string", nullable = false)
public Set<String> strings = new HashSet<String>();
}

}

0 comments on commit d42f66d

Please sign in to comment.