HHH-11815 - @org.hibernate.annotations.Index and NullPointerException in IndexOrUniqueKeySecondPass

This commit is contained in:
Andrea Boriero 2017-08-16 15:46:16 +02:00 committed by Vlad Mihalcea
parent cd7611e511
commit e1faed4b89
2 changed files with 99 additions and 4 deletions

View File

@ -62,6 +62,7 @@ public class IndexOrUniqueKeySecondPass implements SecondPass {
this.buildingContext = buildingContext;
this.unique = unique;
}
@Override
public void doSecondPass(Map persistentClasses) throws MappingException {
if ( columns != null ) {
@ -72,11 +73,17 @@ public class IndexOrUniqueKeySecondPass implements SecondPass {
if ( column != null ) {
this.table = column.getTable();
PersistentClass persistentClass = (PersistentClass) persistentClasses.get( column.getPropertyHolder().getEntityName() );
Property property = persistentClass.getProperty( column.getPropertyName() );
final PropertyHolder propertyHolder = column.getPropertyHolder();
String entityName = ( propertyHolder.isComponent() ) ?
propertyHolder.getPersistentClass().getEntityName() :
propertyHolder.getEntityName();
final PersistentClass persistentClass = (PersistentClass) persistentClasses.get( entityName );
final Property property = persistentClass.getProperty( column.getPropertyName() );
if ( property.getValue() instanceof Component ) {
Component component = (Component) property.getValue();
final Component component = (Component) property.getValue();
List<Column> columns = new ArrayList<>();
component.getColumnIterator().forEachRemaining( selectable -> {
@ -89,7 +96,7 @@ public class IndexOrUniqueKeySecondPass implements SecondPass {
else {
addConstraintToColumn(
buildingContext.getMetadataCollector()
.getLogicalColumnName( table, column.getMappingColumn().getQuotedName() )
.getLogicalColumnName( table, column.getMappingColumn().getQuotedName() )
);
}
}

View File

@ -0,0 +1,88 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.test.schemaupdate.index;
import java.util.List;
import javax.persistence.Embeddable;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.Id;
import org.hibernate.annotations.Index;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.tool.schema.internal.SchemaCreatorImpl;
import org.hibernate.testing.TestForIssue;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertTrue;
/**
* @author Andrea Boriero
*/
@TestForIssue( jiraKey = "HHH-11815")
public class ComponentIndexTest {
private StandardServiceRegistry ssr;
private Metadata metadata;
@Before
public void setUp(){
ssr = new StandardServiceRegistryBuilder().build();
metadata = new MetadataSources( ssr )
.addAnnotatedClass( User.class )
.buildMetadata();
}
@Test
public void testTheIndexIsGenerated() {
final List<String> commands = new SchemaCreatorImpl( ssr ).generateCreationCommands(
metadata,
false
);
assertThatCreateIndexCommandIsGenerated( commands );
}
private void assertThatCreateIndexCommandIsGenerated(List<String> commands) {
boolean createIndexCommandIsGenerated = false;
for ( String command : commands ) {
if ( command.toLowerCase().contains( "create index city_index" ) ) {
createIndexCommandIsGenerated = true;
}
}
assertTrue(
"Expected create index command not found",
createIndexCommandIsGenerated
);
}
@After
public void tearDown(){
StandardServiceRegistryBuilder.destroy( ssr );
}
@Entity(name = "user")
public class User {
@Id
private Long id;
@Embedded
private Address address;
}
@Embeddable
public class Address {
@Index( name = "city_index")
private String city;
private String street;
private String postalCode;
}
}