HHH-10567 : Wrong table for formula if its property is mapped on a secondary table

This commit is contained in:
Gail Badner 2016-02-25 19:48:53 -08:00
parent 19e035c788
commit bdfe38b0c6
4 changed files with 49 additions and 23 deletions

View File

@ -1701,27 +1701,26 @@ public class ModelBinder {
List<RelationalValueSource> relationalValueSources) {
String tableName = null;
for ( RelationalValueSource relationalValueSource : relationalValueSources ) {
if ( ColumnSource.class.isInstance( relationalValueSource ) ) {
final ColumnSource columnSource = (ColumnSource) relationalValueSource;
if ( EqualsHelper.equals( tableName, columnSource.getContainingTableName() ) ) {
continue;
}
if ( tableName != null ) {
throw new MappingException(
String.format(
Locale.ENGLISH,
"Attribute [%s] referenced columns from multiple tables: %s, %s",
attributeName,
tableName,
columnSource.getContainingTableName()
),
mappingDocument.getOrigin()
);
}
tableName = columnSource.getContainingTableName();
// We need to get the containing table name for both columns and formulas,
// particularly when a column/formula is for a property on a secondary table.
if ( EqualsHelper.equals( tableName, relationalValueSource.getContainingTableName() ) ) {
continue;
}
if ( tableName != null ) {
throw new MappingException(
String.format(
Locale.ENGLISH,
"Attribute [%s] referenced columns from multiple tables: %s, %s",
attributeName,
tableName,
relationalValueSource.getContainingTableName()
),
mappingDocument.getOrigin()
);
}
tableName = relationalValueSource.getContainingTableName();
}
return database.toIdentifier( tableName );

View File

@ -44,9 +44,10 @@ public class OptionalJoinTest extends BaseCoreFunctionalTestCase {
assertEquals(1, things.size());
thing = (Thing)things.get(0);
assertEquals("one", thing.getName());
assertEquals( "ONE", thing.getNameUpper() );
// give it a new non-null name and save it
thing.setName("one_changed");
s.update(thing);
s.update( thing );
t.commit();
s.close();
@ -56,6 +57,7 @@ public class OptionalJoinTest extends BaseCoreFunctionalTestCase {
assertEquals(1, things.size());
thing = (Thing)things.get(0);
assertEquals("one_changed", thing.getName());
assertEquals("ONE_CHANGED", thing.getNameUpper());
s.delete(thing);
t.commit();
s.close();
@ -79,6 +81,7 @@ public class OptionalJoinTest extends BaseCoreFunctionalTestCase {
assertEquals(1, things.size());
thing = (Thing)things.get(0);
assertEquals("one", thing.getName());
assertEquals("ONE", thing.getNameUpper());
t.commit();
s.close();
@ -97,6 +100,7 @@ public class OptionalJoinTest extends BaseCoreFunctionalTestCase {
assertEquals(1, things.size());
thing = (Thing)things.get(0);
assertEquals("one_changed", thing.getName());
assertEquals("ONE_CHANGED", thing.getNameUpper());
s.delete(thing);
t.commit();
s.close();
@ -120,6 +124,7 @@ public class OptionalJoinTest extends BaseCoreFunctionalTestCase {
assertEquals(1, things.size());
thing = (Thing)things.get(0);
assertEquals("one", thing.getName());
assertEquals("ONE", thing.getNameUpper());
t.commit();
s.close();
@ -138,6 +143,7 @@ public class OptionalJoinTest extends BaseCoreFunctionalTestCase {
assertEquals(1, things.size());
thing = (Thing)things.get(0);
assertEquals("one_changed", thing.getName());
assertEquals("ONE_CHANGED", thing.getNameUpper());
s.delete(thing);
t.commit();
s.close();
@ -161,9 +167,10 @@ public class OptionalJoinTest extends BaseCoreFunctionalTestCase {
assertEquals(1, things.size());
thing = (Thing)things.get(0);
assertEquals("one", thing.getName());
assertEquals("ONE", thing.getNameUpper());
// give it a null name and save it
thing.setName(null);
s.update(thing);
s.update( thing );
t.commit();
s.close();
@ -173,6 +180,7 @@ public class OptionalJoinTest extends BaseCoreFunctionalTestCase {
assertEquals(1, things.size());
thing = (Thing)things.get(0);
assertNull(thing.getName());
assertNull(thing.getNameUpper());
s.delete(thing);
t.commit();
s.close();
@ -196,6 +204,7 @@ public class OptionalJoinTest extends BaseCoreFunctionalTestCase {
assertEquals(1, things.size());
thing = (Thing)things.get(0);
assertEquals("one", thing.getName());
assertEquals("ONE", thing.getNameUpper());
t.commit();
s.close();
@ -214,6 +223,7 @@ public class OptionalJoinTest extends BaseCoreFunctionalTestCase {
assertEquals(1, things.size());
thing = (Thing)things.get(0);
assertNull(thing.getName());
assertNull(thing.getNameUpper());
s.delete(thing);
t.commit();
s.close();
@ -237,6 +247,7 @@ public class OptionalJoinTest extends BaseCoreFunctionalTestCase {
assertEquals(1, things.size());
thing = (Thing)things.get(0);
assertEquals("one", thing.getName());
assertEquals("ONE", thing.getNameUpper());
t.commit();
s.close();
@ -255,6 +266,7 @@ public class OptionalJoinTest extends BaseCoreFunctionalTestCase {
assertEquals(1, things.size());
thing = (Thing)things.get(0);
assertNull(thing.getName());
assertNull(thing.getNameUpper());
s.delete(thing);
t.commit();
s.close();
@ -279,7 +291,7 @@ public class OptionalJoinTest extends BaseCoreFunctionalTestCase {
assertNull(thing.getName());
// change name to a non-null value
thing.setName("two");
s.update(thing);
s.update( thing );
t.commit();
s.close();
@ -289,6 +301,7 @@ public class OptionalJoinTest extends BaseCoreFunctionalTestCase {
assertEquals(1, things.size());
thing = ((Thing) things.get(0));
assertEquals("two", thing.getName());
assertEquals("TWO", thing.getNameUpper());
s.delete(thing);
t.commit();
s.close();
@ -311,6 +324,7 @@ public class OptionalJoinTest extends BaseCoreFunctionalTestCase {
assertEquals(1, things.size());
thing = (Thing)things.get(0);
assertNull(thing.getName());
assertNull(thing.getNameUpper());
t.commit();
s.close();
@ -329,6 +343,7 @@ public class OptionalJoinTest extends BaseCoreFunctionalTestCase {
assertEquals(1, things.size());
thing = ((Thing) things.get(0));
assertEquals("two", thing.getName());
assertEquals("TWO", thing.getNameUpper());
s.delete(thing);
t.commit();
s.close();
@ -351,6 +366,7 @@ public class OptionalJoinTest extends BaseCoreFunctionalTestCase {
assertEquals(1, things.size());
thing = (Thing)things.get(0);
assertNull(thing.getName());
assertNull(thing.getNameUpper());
t.commit();
s.close();
@ -369,6 +385,7 @@ public class OptionalJoinTest extends BaseCoreFunctionalTestCase {
assertEquals(1, things.size());
thing = ((Thing) things.get(0));
assertEquals("two", thing.getName());
assertEquals("TWO", thing.getNameUpper());
s.delete(thing);
t.commit();
s.close();

View File

@ -24,6 +24,7 @@
<join table="thing_name" optional="true">
<key column="thing_id"/>
<property name="name"/>
<property name="nameUpper" formula="upper(name)"/>
</join>
</class>

View File

@ -43,6 +43,7 @@ public class Thing {
Long id;
String name;
String nameUpper;
/**
* @return Returns the ID.
@ -68,4 +69,12 @@ public class Thing {
public void setName(String name) {
this.name = name;
}
public String getNameUpper() {
return nameUpper;
}
public void setNameUpper(String nameUpper) {
this.nameUpper = nameUpper;
}
}