Querydsl Projection.bean 未找到设置器
Querydsl Projection.bean not finding setters
假设 City 和 CityDTO 为
@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 为空)并且使用 fetch 生成的 DTO 列表正确填充了 id 和 name。
但是,我想使用 setter 而不是字段,所以我尝试使用 Projections.bean
。使用此投影返回 QBean 得到一个空的字段列表和一个具有相同大小但所有元素均为空的列表设置器,由 fetch 生成的 DTO 列表带有 id 和名称 null(显然)。
两个投影都生成一个大小为 2 的绑定映射 {"id" -> "city.id", "name" -> "city.name";
无法弄清楚出了什么问题。 fieldAccessor 是否用于定义设置器,并且由于它为空,投影无法定义它们?
我正在使用 spring 最新的框架 4,查询是这样的:
...
QCity qCity = QCity.city;
return new JPAQueryFactory(sessionFactory.getCurrentSession())
.select(Projections.bean(CityDTO.class, qCity.id, qCity.name)
.from(qCity)
.fetch();
有什么想法吗?
已编辑:
事实上,即使 City.class 的投影结果也是相同的...无法使用 setter 绑定来自查询的值...
在 Robert Bain 评论要求显示 getters 和 setters 之后,我意识到我们使用了流畅的 setters 模式,所以我改变了它并再次使用 Projections.bean
测试查询并成功...
如果其他人遇到同样的情况,我会注册一个答案,并为 querydsl 发出一个问题,看看 API 上是否欢迎对 fluet setter 的支持。
假设 City 和 CityDTO 为
@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 为空)并且使用 fetch 生成的 DTO 列表正确填充了 id 和 name。
但是,我想使用 setter 而不是字段,所以我尝试使用 Projections.bean
。使用此投影返回 QBean 得到一个空的字段列表和一个具有相同大小但所有元素均为空的列表设置器,由 fetch 生成的 DTO 列表带有 id 和名称 null(显然)。
两个投影都生成一个大小为 2 的绑定映射 {"id" -> "city.id", "name" -> "city.name";
无法弄清楚出了什么问题。 fieldAccessor 是否用于定义设置器,并且由于它为空,投影无法定义它们?
我正在使用 spring 最新的框架 4,查询是这样的:
...
QCity qCity = QCity.city;
return new JPAQueryFactory(sessionFactory.getCurrentSession())
.select(Projections.bean(CityDTO.class, qCity.id, qCity.name)
.from(qCity)
.fetch();
有什么想法吗?
已编辑:
事实上,即使 City.class 的投影结果也是相同的...无法使用 setter 绑定来自查询的值...
在 Robert Bain 评论要求显示 getters 和 setters 之后,我意识到我们使用了流畅的 setters 模式,所以我改变了它并再次使用 Projections.bean
测试查询并成功...
如果其他人遇到同样的情况,我会注册一个答案,并为 querydsl 发出一个问题,看看 API 上是否欢迎对 fluet setter 的支持。