Skip to content

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

Open
@spring-projects-issues

Description

@spring-projects-issues

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

Metadata

Metadata

Assignees

Labels

in: coreIssues in core supporttype: bugA general bug

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions