Hibernate Querydsl Projection.bean找不到setter
假设City和CityTo为Hibernate Querydsl Projection.bean找不到setter,hibernate,spring-mvc,querydsl,Hibernate,Spring Mvc,Querydsl,假设City和CityTo为 @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; }
@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 field name是id,type是Long,修饰符为2,但fieldAccessor为null,使用fetch生成的DTO列表正确填写了id和名称
但是,我想使用setter而不是字段,所以我尝试使用Projections.bean。使用该投影返回的QBean得到了一个空的字段列表和一个相同大小的列表设置器,但所有元素都为null,fetch生成的DTO列表的id和名称显然为null
两个投影都会生成一个大小为2的绑定地图{id->city.id,name->city.name
无法找出哪里出了问题。fieldAccessor是否用于定义setter?如果为空,则投影无法定义它们
我使用的是spring framework 4最新版本,查询如下:
...
QCity qCity = QCity.city;
return new JPAQueryFactory(sessionFactory.getCurrentSession())
.select(Projections.bean(CityDTO.class, qCity.id, qCity.name)
.from(qCity)
.fetch();
有什么想法吗
编辑:
事实上,甚至对City.class结果进行投影也是一样的…无法使用setters绑定查询中的值…在Robert Bain评论要求显示getter和setters之后,我意识到我们使用了fluent setters模式,所以我更改了它,并再次使用Projections.bean测试查询,结果成功
如果有其他人陷入同样的情况,我会注册一个答案,并向querydsl发出一个问题,看看API是否欢迎支持fluet setter。我知道这是一个超级光顾,但在没有任何更明显的问题的情况下,我们能看到getter和setter吗?当然@RobertBain,我已经编辑了一些带有get示例的片段ters/setters。我已经意识到我们使用的是fluent setters,希望这不是问题。我将尝试将其更改为经典setters,然后再试一次。感谢您的评论!您的setName方法错误,参数应被称为name。JavaBean命名约定确实会显示具有无效返回类型的setters…是的,再次感谢@RobertBain,我可以确认问题出在fluent setters上……我会发出一个问题,如果我有时间,还会发出一个pull请求。对fluent setters的支持对于一些编程模式(如builder等)来说很方便。。。