spring-projects / spring-projects/spring-data-jpa

Projections on collection properties are not distinct [DATAJPA-1786]

Open
#2,080 5 comments 0 reactions 1 assignee View on GitHub

@odrotbohm is already working on this.

Since Dec 30, 2020.

in: core type: bug
Dominant language
Java
Stars
3.3k
Forks
1.6k
PR merge metrics
No merged PRs in 30d

Description

Sebastian Staudt opened DATAJPA-1786 and commented

Given the following simple domain models:

public class Host {

    List<Alias> aliases;

    long id;

    String name;

    public List<Alias> getAliases() {
        return aliases;
    }

    public long getId() {
        return id;
    }

    public String getName() {
        return name;
    }
}
public class Alias {

    String name;

    public String getName() {
        return name;
    }
}

I'd expect the following projection to do "the right thing":

public interface HostProjection {

    List<String> getAliasesName();

    String getName();
}

Instead it loads a HostProjection proxy for each Alias. Using a distinct query method does not work here as the rows fetched are distinct. I'd expect some magic to happen before the data is transformed into the projection proxies. It seems I was wrong.

I also tried adding a @Id annotated getId() method to the projection, but this doesn't change the behavior.

Using an open projection with the following @Value annotation and a distinct query method works, though:

@Value("#{target.aliases.![name]}")
List<String> getAliases();

I understand that this is operating on a higher level, so this won't help much. It's just a workaround with the overhead of open projections


Issue Links:

  • DATAJPA-1785 Collections cannot be used in dto projections

1 votes, 6 watchers

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.