Skip to content

Commit

Permalink
HHH-10567 : Wrong table for formula if its property is mapped on a se…
Browse files Browse the repository at this point in the history
…condary table

(cherry picked from commit bdfe38b)
  • Loading branch information
gbadner committed Feb 26, 2016
1 parent b38741d commit 6420610
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 22 deletions.
Expand Up @@ -1694,27 +1694,26 @@ private Identifier determineTable(
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()
);
}
// 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;
}

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 );
Expand Down
Expand Up @@ -44,9 +44,10 @@ public void testUpdateNonNullOptionalJoinToDiffNonNull() throws Exception {
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();

Expand All @@ -56,6 +57,7 @@ public void testUpdateNonNullOptionalJoinToDiffNonNull() throws Exception {
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();
Expand All @@ -79,6 +81,7 @@ public void testUpdateNonNullOptionalJoinToDiffNonNullDetached() throws Exceptio
assertEquals(1, things.size());
thing = (Thing)things.get(0);
assertEquals("one", thing.getName());
assertEquals("ONE", thing.getNameUpper());
t.commit();
s.close();

Expand All @@ -97,6 +100,7 @@ public void testUpdateNonNullOptionalJoinToDiffNonNullDetached() throws Exceptio
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();
Expand All @@ -120,6 +124,7 @@ public void testMergeNonNullOptionalJoinToDiffNonNullDetached() throws Exception
assertEquals(1, things.size());
thing = (Thing)things.get(0);
assertEquals("one", thing.getName());
assertEquals("ONE", thing.getNameUpper());
t.commit();
s.close();

Expand All @@ -138,6 +143,7 @@ public void testMergeNonNullOptionalJoinToDiffNonNullDetached() throws Exception
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();
Expand All @@ -161,9 +167,10 @@ public void testUpdateNonNullOptionalJoinToNull() throws Exception {
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();

Expand All @@ -173,6 +180,7 @@ public void testUpdateNonNullOptionalJoinToNull() throws Exception {
assertEquals(1, things.size());
thing = (Thing)things.get(0);
assertNull(thing.getName());
assertNull(thing.getNameUpper());
s.delete(thing);
t.commit();
s.close();
Expand All @@ -196,6 +204,7 @@ public void testUpdateNonNullOptionalJoinToNullDetached() throws Exception {
assertEquals(1, things.size());
thing = (Thing)things.get(0);
assertEquals("one", thing.getName());
assertEquals("ONE", thing.getNameUpper());
t.commit();
s.close();

Expand All @@ -214,6 +223,7 @@ public void testUpdateNonNullOptionalJoinToNullDetached() throws Exception {
assertEquals(1, things.size());
thing = (Thing)things.get(0);
assertNull(thing.getName());
assertNull(thing.getNameUpper());
s.delete(thing);
t.commit();
s.close();
Expand All @@ -237,6 +247,7 @@ public void testMergeNonNullOptionalJoinToNullDetached() throws Exception {
assertEquals(1, things.size());
thing = (Thing)things.get(0);
assertEquals("one", thing.getName());
assertEquals("ONE", thing.getNameUpper());
t.commit();
s.close();

Expand All @@ -255,6 +266,7 @@ public void testMergeNonNullOptionalJoinToNullDetached() throws Exception {
assertEquals(1, things.size());
thing = (Thing)things.get(0);
assertNull(thing.getName());
assertNull(thing.getNameUpper());
s.delete(thing);
t.commit();
s.close();
Expand All @@ -279,7 +291,7 @@ public void testUpdateNullOptionalJoinToNonNull() throws Exception {
assertNull(thing.getName());
// change name to a non-null value
thing.setName("two");
s.update(thing);
s.update( thing );
t.commit();
s.close();

Expand All @@ -289,6 +301,7 @@ public void testUpdateNullOptionalJoinToNonNull() throws Exception {
assertEquals(1, things.size());
thing = ((Thing) things.get(0));
assertEquals("two", thing.getName());
assertEquals("TWO", thing.getNameUpper());
s.delete(thing);
t.commit();
s.close();
Expand All @@ -311,6 +324,7 @@ public void testUpdateNullOptionalJoinToNonNullDetached() throws Exception {
assertEquals(1, things.size());
thing = (Thing)things.get(0);
assertNull(thing.getName());
assertNull(thing.getNameUpper());
t.commit();
s.close();

Expand All @@ -329,6 +343,7 @@ public void testUpdateNullOptionalJoinToNonNullDetached() throws Exception {
assertEquals(1, things.size());
thing = ((Thing) things.get(0));
assertEquals("two", thing.getName());
assertEquals("TWO", thing.getNameUpper());
s.delete(thing);
t.commit();
s.close();
Expand All @@ -351,6 +366,7 @@ public void testMergeNullOptionalJoinToNonNullDetached() throws Exception {
assertEquals(1, things.size());
thing = (Thing)things.get(0);
assertNull(thing.getName());
assertNull(thing.getNameUpper());
t.commit();
s.close();

Expand All @@ -369,6 +385,7 @@ public void testMergeNullOptionalJoinToNonNullDetached() throws Exception {
assertEquals(1, things.size());
thing = ((Thing) things.get(0));
assertEquals("two", thing.getName());
assertEquals("TWO", thing.getNameUpper());
s.delete(thing);
t.commit();
s.close();
Expand Down
Expand Up @@ -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>

Expand Down
Expand Up @@ -43,6 +43,7 @@ public void setComments(String comments) {

Long id;
String name;
String nameUpper;

/**
* @return Returns the ID.
Expand All @@ -68,4 +69,12 @@ public String getName() {
public void setName(String name) {
this.name = name;
}

public String getNameUpper() {
return nameUpper;
}
public void setNameUpper(String nameUpper) {
this.nameUpper = nameUpper;
}

}

0 comments on commit 6420610

Please sign in to comment.