Skip to content

Avoid duplicate selection of columns. #1074

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>2.3.0-SNAPSHOT</version>
<version>2.3.0-1073-duplicate-select-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data Relational Parent</name>
Expand Down
2 changes: 1 addition & 1 deletion spring-data-jdbc-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>2.3.0-SNAPSHOT</version>
<version>2.3.0-1073-duplicate-select-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
4 changes: 2 additions & 2 deletions spring-data-jdbc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<modelVersion>4.0.0</modelVersion>

<artifactId>spring-data-jdbc</artifactId>
<version>2.3.0-SNAPSHOT</version>
<version>2.3.0-1073-duplicate-select-SNAPSHOT</version>

<name>Spring Data JDBC</name>
<description>Spring Data module for JDBC repositories.</description>
Expand All @@ -15,7 +15,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>2.3.0-SNAPSHOT</version>
<version>2.3.0-1073-duplicate-select-SNAPSHOT</version>
</parent>

<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
*/
package org.springframework.data.jdbc.core.convert;

import java.util.*;
import java.util.function.Function;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jdbc.repository.support.SimpleJdbcRepository;
Expand All @@ -33,11 +38,6 @@
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;

import java.util.*;
import java.util.function.Function;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
* Generates SQL statements to be used by {@link SimpleJdbcRepository}
*
Expand Down Expand Up @@ -381,11 +381,11 @@ private String createAcquireLockById(LockMode lockMode) {
Table table = this.getTable();

Select select = StatementBuilder //
.select(getIdColumn()) //
.from(table) //
.where(getIdColumn().isEqualTo(getBindMarker(ID_SQL_PARAMETER))) //
.lock(lockMode) //
.build();
.select(getIdColumn()) //
.from(table) //
.where(getIdColumn().isEqualTo(getBindMarker(ID_SQL_PARAMETER))) //
.lock(lockMode) //
.build();

return render(select);
}
Expand All @@ -395,10 +395,10 @@ private String createAcquireLockAll(LockMode lockMode) {
Table table = this.getTable();

Select select = StatementBuilder //
.select(getIdColumn()) //
.from(table) //
.lock(lockMode) //
.build();
.select(getIdColumn()) //
.from(table) //
.lock(lockMode) //
.build();

return render(select);
}
Expand Down Expand Up @@ -727,9 +727,9 @@ static final class Join {

Join(Table joinTable, Column joinColumn, Column parentId) {

Assert.notNull( joinTable,"JoinTable must not be null.");
Assert.notNull( joinColumn,"JoinColumn must not be null.");
Assert.notNull( parentId,"ParentId must not be null.");
Assert.notNull(joinTable, "JoinTable must not be null.");
Assert.notNull(joinColumn, "JoinColumn must not be null.");
Assert.notNull(parentId, "ParentId must not be null.");

this.joinTable = joinTable;
this.joinColumn = joinColumn;
Expand All @@ -751,12 +751,14 @@ Column getParentId() {
@Override
public boolean equals(Object o) {

if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Join join = (Join) o;
return joinTable.equals(join.joinTable) &&
joinColumn.equals(join.joinColumn) &&
parentId.equals(join.parentId);
return joinTable.equals(join.joinTable) && joinColumn.equals(join.joinColumn) && parentId.equals(join.parentId);
}

@Override
Expand All @@ -767,10 +769,10 @@ public int hashCode() {
@Override
public String toString() {

return "Join{" +
"joinTable=" + joinTable +
", joinColumn=" + joinColumn +
", parentId=" + parentId +
return "Join{" + //
"joinTable=" + joinTable + //
", joinColumn=" + joinColumn + //
", parentId=" + parentId + //
'}';
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.util.Map;
import java.util.Set;

import org.assertj.core.api.SoftAssertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.data.annotation.Id;
Expand Down Expand Up @@ -374,6 +373,23 @@ public void findAllByPropertyWithKeyOrdered() {
+ "WHERE dummy_entity.backref = :backref " + "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
public void updateWithVersion() {

Expand Down Expand Up @@ -709,6 +725,7 @@ static class DummyEntity {
Set<Element> elements;
Map<Integer, Element> mappedElements;
AggregateReference<OtherAggregate, Long> other;
Map<Long, ReferencedEntity> mappedReference;
}

static class VersionedEntity extends DummyEntity {
Expand Down
4 changes: 2 additions & 2 deletions spring-data-relational/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
<modelVersion>4.0.0</modelVersion>

<artifactId>spring-data-relational</artifactId>
<version>2.3.0-SNAPSHOT</version>
<version>2.3.0-1073-duplicate-select-SNAPSHOT</version>

<name>Spring Data Relational</name>
<description>Spring Data Relational support</description>

<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>2.3.0-SNAPSHOT</version>
<version>2.3.0-1073-duplicate-select-SNAPSHOT</version>
</parent>

<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@
*/
package org.springframework.data.relational.core.sql;

import java.util.Arrays;

import org.springframework.util.Assert;

/**
* Abstract implementation to support {@link Segment} implementations.
*
* @author Mark Paluch
* @author Jens Schauder
* @since 1.1
*/
abstract class AbstractSegment implements Segment {
Expand All @@ -47,21 +50,21 @@ public void visit(Visitor visitor) {
visitor.leave(this);
}

/*
* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
return toString().hashCode();
public boolean equals(Object o) {

if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
AbstractSegment that = (AbstractSegment) o;
return Arrays.equals(children, that.children);
}

/*
* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
return obj instanceof Segment && toString().equals(obj.toString());
public int hashCode() {
return Arrays.hashCode(children);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package org.springframework.data.relational.core.sql;

import java.util.Objects;

import org.springframework.lang.Nullable;
import org.springframework.util.Assert;

Expand Down Expand Up @@ -364,6 +366,27 @@ String getPrefix() {
return prefix;
}

@Override
public boolean equals(Object o) {

if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
if (!super.equals(o)) {
return false;
}
Column column = (Column) o;
return name.equals(column.name) && table.equals(column.table);
}

@Override
public int hashCode() {
return Objects.hash(super.hashCode(), name, table);
}

/**
* {@link Aliased} {@link Column} implementation.
*/
Expand Down Expand Up @@ -419,5 +442,26 @@ public Column from(Table table) {
public String toString() {
return getPrefix() + getName() + " AS " + getAlias();
}

@Override
public boolean equals(Object o) {

if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
if (!super.equals(o)) {
return false;
}
AliasedColumn that = (AliasedColumn) o;
return alias.equals(that.alias);
}

@Override
public int hashCode() {
return Objects.hash(super.hashCode(), alias);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,18 @@
*/
package org.springframework.data.relational.core.sql;

import java.util.Objects;

import org.springframework.util.Assert;

/**
* Represents a table reference within a SQL statement. Typically used to denote {@code FROM} or {@code JOIN} or to
* Represents a table reference within a SQL statement. Typically, used to denote {@code FROM} or {@code JOIN} or to
* prefix a {@link Column}.
* <p/>
* Renders to: {@code <name>} or {@code <name> AS <name>}.
*
* @author Mark Paluch
* @author Jens Schauder
* @since 1.1
*/
public class Table extends AbstractSegment implements TableLike {
Expand Down Expand Up @@ -107,7 +110,6 @@ public Table as(SqlIdentifier alias) {
return new AliasedTable(name, alias);
}


/*
* (non-Javadoc)
* @see org.springframework.data.relational.core.sql.Named#getName()
Expand Down Expand Up @@ -135,6 +137,26 @@ public String toString() {
return name.toString();
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
if (!super.equals(o)) {
return false;
}
Table table = (Table) o;
return name.equals(table.name);
}

@Override
public int hashCode() {
return Objects.hash(super.hashCode(), name);
}

/**
* {@link Aliased} {@link Table} implementation.
*/
Expand Down Expand Up @@ -184,5 +206,26 @@ public SqlIdentifier getReferenceName() {
public String toString() {
return getName() + " AS " + getAlias();
}

@Override
public boolean equals(Object o) {

if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
if (!super.equals(o)) {
return false;
}
AliasedTable that = (AliasedTable) o;
return alias.equals(that.alias);
}

@Override
public int hashCode() {
return Objects.hash(super.hashCode(), alias);
}
}
}