Querydsl Projection.bean not finding setters



假设城市和城市DTO为

@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;
}
}

使用Projection.fields进行查询时一切正常,返回的 QBean 具有按预期大小的字段列表 (2(,具有预期字段引用的元素(最后我认为这是预期的,例如 id 字段名称为"id",类型为 Long,修饰符为 2,但 fieldAccessor 为 null(,使用 fetch 生成的 DTO 列表正确填充了 id 和名称。

但是,我想使用二传手而不是字段,所以我正在尝试使用Projections.bean.Usint 这个投影返回 QBean 得到了一个空的字段列表和一个大小相同但所有元素都为空的列表设置器,由 fetch 生成的 DTO 列表带有 id 和名称 null(显然(。

这两个投影都会生成大小为 2 的绑定映射,为 {"id" -> "city.id"、"name" -> "city.name";

无法弄清楚出了什么问题。字段访问器是否用于定义资源库,并且由于它为空,投影无法定义它们?

我正在使用最新的 spring 框架 4,查询是这样的:

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

有什么想法吗?

编辑:

事实上,即使是对曼城的预测.class结果也是一样的......无法使用资源库绑定查询中的值...

在罗伯特·贝恩评论要求显示 getter 和 setter 之后,我意识到我们使用流畅的 setter 模式,所以我更改了它并再次用Projections.bean测试查询并成功......

如果其他人陷入同样的情况,我将注册一个答案,并为 querydsl 触发一个问题,以查看 API 是否欢迎对烟道设置器的支持。

最新更新