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)