spring-projects / spring-projects/spring-data-commons

Improve documentation for traversing nested properties in projections [DATACMNS-1030]

Open
#1,478 0 comments 0 reactions 1 assignee View on GitHub

@odrotbohm is already working on this.

Since Dec 30, 2020.

type: documentation type: enhancement
Dominant language
Java
Stars
838
Forks
730
PR merge metrics
No merged PRs in 30d

Description

Anthony Foulfoin opened DATACMNS-1030 and commented

Hi,

Given the following classes :

interface CarProjection {
    String getName();
    List<Color> getColors();
    }    
}

class Car{
    String name;

    @OneToMany(mappedBy = "car")
    List<Color> colors;
}

class Color{
     String label;

     @ManyToOne
     Car car;
}

I want my CarProjection to return only the color labels instead of the full objects. At first I used an @Value with a spel expression :

@Value("#{target.colors.![label]}")
List<String> getColors();

Which works, but the SQL queries are not fully optimized because of the open projection and become extremely slow. I figured out that it was possible to do this directly, i.e. traversing properties in the method name :

List<String> getColorsLabel();

And it works like a charm :) But I found this solution randomly in a jira bug report, and not in the projections documentation, which could help other users to do the same thing.

EDIT:

Finally it does not work for traversing collections, I get duplicated cars objects, each one with only one color. Same thing if I don't traverse the property :

List<Color> getColors

I get duplicated results.


Affects: 1.12.8 (Hopper SR8)

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.