【问题标题】:Querydsl Projection.bean not finding settersQuerydsl Projection.bean 找不到设置器
【发布时间】:2018-11-28 14:48:01
【问题描述】:

假设 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。 Usint 这个投影返回的 QBean 得到了一个空的字段列表和一个具有相同大小但所有元素为空的列表设置器,由 fetch 生成的 DTO 列表带有 id 和 name 空(显然)。

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

无法弄清楚出了什么问题。 fieldAccessor 是否用于定义 setter,并且由于它为 null,projection 无法定义它们?

我正在使用最新的 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 从查询中绑定值...

【问题讨论】:

  • 我知道这太傲慢了,但在没有更明显的情况下,我们可以看看 getter 和 setter 吗?
  • 当然@RobertBain,我已经用getter/setter的例子编辑了sn-ps。我已经意识到我们使用“流利的设置器”,希望这不是问题。将尝试将其更改为经典二传手并再试一次。感谢您的评论!
  • 你的setName方法不对,参数应该叫name。
  • JavaBean 命名约定确实显示了返回类型为 void 的 setter...
  • 是的,再次感谢@RobertBain,我可以确认问题出在 fluent setters... 我会提出一个问题,如果我有时间的话,还会提出一个拉取请求。对 fluent setter 的支持对于 builder 等一些编程模式很方便...

标签: hibernate spring-mvc querydsl


【解决方案1】:

在 Robert Bain 发表评论要求显示 getter 和 setter 之后,我意识到我们使用了流畅的 setter 模式,所以我对其进行了更改并再次使用 Projections.bean 测试查询并成功...

我会注册一个答案,以防其他人陷入同样的​​情况,并为 querydsl 发起一个问题,以查看 API 是否欢迎对 Fluet setter 的支持。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-10
    • 2023-03-17
    • 2013-09-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多