Avoid duplicate selection of columns.

When the key column of a MappedCollection is also present in the contained entity that column got select twice.
We now check if the column already gets selected before adding it to the selection.

This only works properly if the column names derived for the entity property and the key column match exactly, which might require use of a `@Column` annotation depending on the used NamingStrategy.

Closes #1073
Original pull request #1074
This commit is contained in:
Jens Schauder
2021-10-21 15:39:36 +02:00
parent 0d1ba211c5
commit fd662bf9e6
3 changed files with 107 additions and 1 deletions

View File

@@ -423,6 +423,23 @@ class SqlGeneratorUnitTests {
+ "ORDER BY key-column");
}
@Test // GH-1073
public void findAllByPropertyAvoidsDuplicateColumns() {
final SqlGenerator sqlGenerator = createSqlGenerator(ReferencedEntity.class);
final String sql = sqlGenerator.getFindAllByProperty(
Identifier.of(quoted("id"), "parent-id-value", DummyEntity.class), //
quoted("X_L1ID"), // this key column collides with the name derived by the naming strategy for the id of
// ReferencedEntity.
false);
final String id = "referenced_entity.x_l1id AS x_l1id";
assertThat(sql.indexOf(id)) //
.describedAs(sql) //
.isEqualTo(sql.lastIndexOf(id));
}
@Test // DATAJDBC-219
void updateWithVersion() {
@@ -862,6 +879,7 @@ class SqlGeneratorUnitTests {
Set<Element> elements;
Map<Integer, Element> mappedElements;
AggregateReference<OtherAggregate, Long> other;
Map<Long, ReferencedEntity> mappedReference;
}
@SuppressWarnings("unused")