Selecting a set of Doubles in a projection

33 views
Skip to first unread message

Jeroen Reijn

unread,
Dec 17, 2020, 5:23:40 AM12/17/20
to Querydsl
Hi all,

I'm a bit new to QueryDSL and it seems I'm a bit lost when it comes to projections.
What I'm trying to do is as follows. I have a project that uses Spring data JPA in combination with QueryDSL.

I have two entities that I'm trying to create a projection for:

@Entity
@Data
public class PowerRange implements CatalogAware, Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private Boolean onlineOrderPossible;

    @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER)
    @JoinColumn(name = "range_id")
    private Set<OpticalPower> opticalPowers;
}

@Entity
@Data
public class OpticalPower implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private Double cylinderFrom;

    private Double cylinderTo;

    @ElementCollection
    @CollectionTable(name = "optical_power_base_curves", joinColumns =     @JoinColumn(name = "optical_power_id"))
    private Set<Double> baseCurves;
}



The above entities are a bit more complex, but I'm trying to keep it simple for this question.

Now while trying to create a projection for the OpticalPower entity I've created a Projection class that also has a Set<Double>baseCurves and I'm trying to use the following code.

        AbstractGroupExpression<OpticalPowerData, Set<OpticalPowerData>> opProjection =
                GroupBy.set(Projections.fields(OpticalPowerData.class,
                        qOpticalPower.id,
                        qOpticalPower.cylinderFrom,
                        qOpticalPower.cylinderTo,
                        GroupBy.set(qOpticalPower.baseCurves).as("baseCurves")
                        )
                );

However, when I use that last line (marked bold) I get an exception while trying to execute my query.

org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.QueryException: not an entity

When I leave that last line (GroupBy.set) out of the code, the projection works like a charm, but missing the data of course.

I use the following code to run my query and apply the projection.

Collection<PowerRangeData> values = powerRangeJPAQuery
.transform(GroupBy.groupBy(powerRange.id).as(projection))
.values();

Does anybody have an idea what I might be doing wrong in the projection?
Reply all
Reply to author
Forward
0 new messages