HHH-6212 Adding a secondary table test
This commit is contained in:
parent
3ff5c13058
commit
350984a181
|
@ -28,6 +28,7 @@ import java.util.HashSet;
|
|||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.hibernate.AssertionFailure;
|
||||
import org.hibernate.EntityMode;
|
||||
import org.hibernate.engine.spi.FilterDefinition;
|
||||
import org.hibernate.internal.util.Value;
|
||||
|
@ -142,14 +143,14 @@ public class EntityBinding {
|
|||
this.baseTable = baseTable;
|
||||
}
|
||||
|
||||
public TableSpecification getTable(String containingTableName) {
|
||||
if ( containingTableName == null ) {
|
||||
public TableSpecification getTable(String tableName) {
|
||||
if ( tableName == null ) {
|
||||
return baseTable;
|
||||
}
|
||||
|
||||
TableSpecification tableSpec = secondaryTables.get( containingTableName );
|
||||
TableSpecification tableSpec = secondaryTables.get( tableName );
|
||||
if ( tableSpec == null ) {
|
||||
// todo throw exception !? (HF)
|
||||
throw new AssertionFailure( String.format("Unable to find table %s amongst tables %s", tableName, secondaryTables.keySet()) );
|
||||
}
|
||||
return tableSpec;
|
||||
}
|
||||
|
|
|
@ -1,16 +1,22 @@
|
|||
package org.hibernate.metamodel.source.annotations.entity;
|
||||
|
||||
import java.util.Iterator;
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.SecondaryTable;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.hibernate.AssertionFailure;
|
||||
import org.hibernate.metamodel.binding.EntityBinding;
|
||||
import org.hibernate.metamodel.relational.SimpleValue;
|
||||
import org.hibernate.metamodel.relational.Table;
|
||||
|
||||
import static junit.framework.Assert.assertEquals;
|
||||
import static junit.framework.Assert.assertNull;
|
||||
import static junit.framework.Assert.assertFalse;
|
||||
import static junit.framework.Assert.assertTrue;
|
||||
import static junit.framework.Assert.fail;
|
||||
|
||||
/**
|
||||
* @author Hardy Ferentschik
|
||||
|
@ -21,6 +27,9 @@ public class SecondaryTableTest extends BaseAnnotationBindingTestCase {
|
|||
class EntityWithSecondaryTable {
|
||||
@Id
|
||||
private long id;
|
||||
|
||||
@Column(table = "SECOND_TABLE")
|
||||
private String name;
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -29,14 +38,25 @@ public class SecondaryTableTest extends BaseAnnotationBindingTestCase {
|
|||
EntityBinding binding = getEntityBinding( EntityWithSecondaryTable.class );
|
||||
Table table = (Table) binding.getTable( "SECOND_TABLE" );
|
||||
assertEquals( "The secondary table should exist", "SECOND_TABLE", table.getTableName().getName() );
|
||||
|
||||
Iterator<SimpleValue> valueIterator = table.values().iterator();
|
||||
assertTrue( valueIterator.hasNext() );
|
||||
org.hibernate.metamodel.relational.Column column = (org.hibernate.metamodel.relational.Column) valueIterator.next();
|
||||
assertEquals( "Wrong column name", "name", column.getColumnName().getName() );
|
||||
assertFalse( valueIterator.hasNext() );
|
||||
}
|
||||
|
||||
@Test
|
||||
@Resources(annotatedClasses = EntityWithSecondaryTable.class)
|
||||
public void testRetrievingUnknownTable() {
|
||||
EntityBinding binding = getEntityBinding( EntityWithSecondaryTable.class );
|
||||
Table table = (Table) binding.getTable( "FOO" );
|
||||
assertNull( table ); // todo - what should really happen for a non existing table
|
||||
try {
|
||||
binding.getTable( "FOO" );
|
||||
fail();
|
||||
}
|
||||
catch ( AssertionFailure e ) {
|
||||
assertTrue( e.getMessage().startsWith( "Unable to find table" ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue