Frohlich
Frohlich

Reputation: 963

Querydsl Projection.bean not finding setters

Assuming City and CityDTO as

@Entity
public class City {
    private Long id;
    private String name;
    @Column(name="id")
    public Long getId(){ 
        return this.id;
    }
    public City setId(Long id) {
        this.id = id;
        return this;
    }
    @Column(name="name")
    public String getName(){ 
        return this.name;
    }
    public City setName(String name) {
        this.name = name;
        return this;
    }
    @Transient
    public String anotherInformationToSerializeInJsonButNotPersist() {
        return "this is an example of some functions that we have inside entities";
}

public class CityDTO {
    private Long id;
    private String name;
    private String anotherMuchRelevantInformationDifferentFromEntityTransientOne;
    public Long getId(){ 
        return this.id;
    }
    public CityDTO setId(Long id) {
        this.id = id;
        return this;
    }
    public String getName(){ 
        return this.name;
    }
    public CityDTO setName(String id) {
        this.name = name;
        return this;
    }
    public String getAnotherMuchRelevantInformationDifferentFromEntityTransientOne(){ 
        return this.anotherMuchRelevantInformationDifferentFromEntityTransientOne;
    }
    public CityDTO setAnotherMuchRelevantInformationDifferentFromEntityTransientOne(String anotherMuchRelevantInformationDifferentFromEntityTransientOne) {
        this.anotherMuchRelevantInformationDifferentFromEntityTransientOne = anotherMuchRelevantInformationDifferentFromEntityTransientOne;
        return this;
    }
}

when querying with Projection.fields everything is fine, the returned QBean have fields list with size as expected (2), elements with field references as expected (at last I think it is as expected, for example id field name is "id", type is Long, modifiers is 2 but fieldAccessor is null) and DTO list generated with fetch is filled with id and name correctly.

but, I want to use setters instead of fields so I'm trying with Projections.bean. Usint this projection returned QBean got an empty list of fields and a list setters with same size but all elements are null, DTO list generated by fetch came with id and name null (obviously).

both projections generates a bindings map with size 2 as {"id" -> "city.id", "name" -> "city.name";

Can't figure out what is going wrong. Is the fieldAccessor used to define setters and, as it is null, projection is unable to define them?

I'm using spring framework 4 latest and query is something like this:

...
QCity qCity = QCity.city;
return new JPAQueryFactory(sessionFactory.getCurrentSession())
    .select(Projections.bean(CityDTO.class, qCity.id, qCity.name)
    .from(qCity)
    .fetch();

any ideas?

edited:

In fact even projecting for City.class result is the same... cant bind values from query using setters...

Upvotes: 0

Views: 1580

Answers (1)

Frohlich
Frohlich

Reputation: 963

After Robert Bain comment asking to show getters and setters I realized that we use fluent setters pattern so I change it and test querying with Projections.bean again with sucess...

I'll register an answer in case of someone else stuck into this same situation and fire an issue for querydsl to see if support for fluet setters is welcome on API.

Upvotes: 1

Related Questions