HHH-6381 Adding a actual assertion which tests the optional part. Also applying formatting styles.

This commit is contained in:
Hardy Ferentschik 2011-07-06 14:46:07 +02:00
parent 20559966b3
commit d1eec59c08
3 changed files with 218 additions and 204 deletions

View File

@ -112,7 +112,7 @@ public class JoinedSubclassEntityPersister extends AbstractEntityPersister {
// Span of the tables directly mapped by this entity and super-classes, if any
private final int coreTableSpan;
// only contains values for SecondaryTables ie. not tables part of the "coreTableSpan"
// only contains values for SecondaryTables, ie. not tables part of the "coreTableSpan"
private final boolean[] isNullableTable;
//INITIALIZATION:
@ -187,21 +187,21 @@ public class JoinedSubclassEntityPersister extends AbstractEntityPersister {
isNullableTable = new boolean[persistentClass.getJoinClosureSpan()];
int tabIndex = 0;
int tableIndex = 0;
Iterator joinIter = persistentClass.getJoinClosureIterator();
while ( joinIter.hasNext() ) {
Join join = (Join) joinIter.next();
isNullableTable[tabIndex++] = join.isOptional();
isNullableTable[tableIndex++] = join.isOptional();
Table tab = join.getTable();
Table table = join.getTable();
String tabname = tab.getQualifiedName(
String tableName = table.getQualifiedName(
factory.getDialect(),
factory.getSettings().getDefaultCatalogName(),
factory.getSettings().getDefaultSchemaName()
);
tables.add(tabname);
tables.add( tableName );
KeyValue key = join.getKey();
int joinIdColumnSpan = key.getColumnSpan();
@ -279,7 +279,6 @@ public class JoinedSubclassEntityPersister extends AbstractEntityPersister {
key[k] = ( (Column) citer.next() ).getQuotedName( factory.getDialect() );
}
keyColumns.add( key );
}
String[] naturalOrderSubclassTableNameClosure = ArrayHelper.toStringArray( subtables );
@ -543,8 +542,9 @@ public class JoinedSubclassEntityPersister extends AbstractEntityPersister {
}
protected boolean isNullableTable(int j) {
if (j < coreTableSpan)
if ( j < coreTableSpan ) {
return false;
}
return isNullableTable[j - coreTableSpan];
}
@ -552,7 +552,6 @@ public class JoinedSubclassEntityPersister extends AbstractEntityPersister {
return subclassTableSequentialSelect[j] && !isClassOrSuperclassTable[j];
}
/*public void postInstantiate() throws MappingException {
super.postInstantiate();
//TODO: other lock modes?
@ -622,7 +621,6 @@ public class JoinedSubclassEntityPersister extends AbstractEntityPersister {
throw new JDBCException( "could not load by id: " + MessageHelper.infoString(this, id), sqle );
}
}*/
private static final void reverse(Object[] objects, int len) {
Object[] temp = new Object[len];
for ( int i = 0; i < len; i++ ) {
@ -636,11 +634,13 @@ public class JoinedSubclassEntityPersister extends AbstractEntityPersister {
/**
* Reverse the first n elements of the incoming array
*
* @param objects
* @param n
*
* @return New array with the first n elements in reversed order
*/
private static final String[] reverse(String [] objects, int n) {
private static String[] reverse(String[] objects, int n) {
int size = objects.length;
String[] temp = new String[size];
@ -658,11 +658,13 @@ public class JoinedSubclassEntityPersister extends AbstractEntityPersister {
/**
* Reverse the first n elements of the incoming array
*
* @param objects
* @param n
*
* @return New array with the first n elements in reversed order
*/
private static final String[][] reverse(String[][] objects, int n) {
private static String[][] reverse(String[][] objects, int n) {
int size = objects.length;
String[][] temp = new String[size][];
for ( int i = 0; i < n; i++ ) {
@ -677,7 +679,6 @@ public class JoinedSubclassEntityPersister extends AbstractEntityPersister {
}
public String fromTableFragment(String alias) {
return getTableName() + ' ' + alias;
}
@ -738,7 +739,6 @@ public class JoinedSubclassEntityPersister extends AbstractEntityPersister {
}
public String[] toColumns(String alias, String propertyName) throws QueryException {
if ( ENTITY_CLASS.equals( propertyName ) ) {
// This doesn't actually seem to work but it *might*
// work on some dbs. Also it doesn't work if there
@ -751,7 +751,6 @@ public class JoinedSubclassEntityPersister extends AbstractEntityPersister {
else {
return super.toColumns( alias, propertyName );
}
}
protected int[] getPropertyTableNumbersInSelect() {

View File

@ -23,11 +23,12 @@
*/
package org.hibernate.test.annotations.inheritance.joined;
import org.hibernate.Session;
import org.hibernate.Transaction;
import java.math.BigInteger;
import org.junit.Test;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import static org.junit.Assert.assertEquals;
@ -42,22 +43,34 @@ public class JoinedSubclassAndSecondaryTable extends BaseCoreFunctionalTestCase
Session s = openSession();
Transaction tx = s.beginTransaction();
SwimmingPool sp = new SwimmingPool();
//sp.setAddress( "Park Avenue" );
s.persist( sp );
s.flush();
s.clear();
BigInteger rowCount = getTableRowCount( s );
assertEquals(
"The address table is marked as optional. For null values no database row should be created",
BigInteger.valueOf( 0 ),
rowCount
);
SwimmingPool sp2 = (SwimmingPool) s.get( SwimmingPool.class, sp.getId() );
assertEquals( sp.getAddress(), null );
PoolAddress addr = new PoolAddress();
addr.setAddress("Park Avenue");
sp2.setAddress(addr);
PoolAddress address = new PoolAddress();
address.setAddress( "Park Avenue" );
sp2.setAddress( address );
s.flush();
s.clear();
sp2 = (SwimmingPool) s.get( SwimmingPool.class, sp.getId() );
rowCount = getTableRowCount( s );
assertEquals(
"Now we should have a row in the pool address table ",
BigInteger.valueOf( 1 ),
rowCount
);
assertFalse( sp2.getAddress() == null );
assertEquals( sp2.getAddress().getAddress(), "Park Avenue" );
@ -65,9 +78,12 @@ public class JoinedSubclassAndSecondaryTable extends BaseCoreFunctionalTestCase
s.close();
}
private BigInteger getTableRowCount(Session s) {
return (BigInteger) s.createSQLQuery( "select count(*) from POOL_ADDRESS" ).uniqueResult();
}
@Override
protected Class[] getAnnotatedClasses() {
return new Class[] { Pool.class, SwimmingPool.class };
}
}

View File

@ -409,5 +409,4 @@ public abstract class BaseCoreFunctionalTestCase extends BaseUnitTestCase {
return true;
}
}
}