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) { List<RelationalValueSource> relationalValueSources) {
String tableName = null; String tableName = null;
for ( RelationalValueSource relationalValueSource : relationalValueSources ) { for ( RelationalValueSource relationalValueSource : relationalValueSources ) {
if ( ColumnSource.class.isInstance( relationalValueSource ) ) { // We need to get the containing table name for both columns and formulas,
final ColumnSource columnSource = (ColumnSource) relationalValueSource; // particularly when a column/formula is for a property on a secondary table.
if ( EqualsHelper.equals( tableName, columnSource.getContainingTableName() ) ) { if ( EqualsHelper.equals( tableName, relationalValueSource.getContainingTableName() ) ) {
continue; 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();
} }
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 ); return database.toIdentifier( tableName );

View File

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

View File

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

View File

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