我试图填充List<>通过查询QueryDSL,但它会在bean填充时抛出错误。
这是我的方法;
@Override
public Page<UserDTO> findByTypeQuerydsl(String filters, Pageable pageable) {
JPAQuery query = new JPAQuery(entityManager);
query = (JPAQuery) super.getQuerydsl().applyPagination(pageable, query);
List<UserDTO> results = query.from(qUserDTO)
.innerJoin(qUser)
.on(qUserDTO.id.eq(qUser.id))
.where(qUserDTO.login.eq(filters))
.list(Projections.fields(UserDTO.class, qUser.id, qUser.login, qUser.firstname, qUser.lastname, qUser.dayofbirth,
qUserDTO.district, qUserDTO.city));
return new PageImpl<UserDTO>(results, pageable, results.size());
}
在.list
处抛出错误并说;
方法列表(Projections.fields(UserDTO.class,qUser.id, qUser.login,qUser.firstname,qUser.lastname,qUser.dayofbirth, qUserDTO.district,qUserDTO.city))未定义类型QueryBase
我的 UserDTO.class;
@Entity
@Table(name="USERDTO")
@QueryEntity
public class UserDTO implements Serializable {
private static final long serialVersionUID = -7860243025833384447L;
@Id
private Long id;
private String login;
private String firstname;
private String lastname;
private Date dayofbirth;
private String city;
private String district;
@OneToOne(fetch=FetchType.LAZY,cascade=CascadeType.ALL)
@JoinColumn(name="id")
User user;
//getters and setters
}
com-querydsl ;
的版本 <dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
<version>4.1.3</version>
</dependency>
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-jpa</artifactId>
<version>4.1.3</version>
</dependency>
答案 0 :(得分:0)
如果您使用的是querydsl 4.x,则应使用fetch()方法,这样:
QueryBase queryBase = new JPAQuery(qUserDTO)....
FetchableQueryBase fetchableQueryBase = (FetchableQueryBase) queryBase;
fetchableQueryBase.fetch();
如果你真的想维护你的synthax,你应该降级到3.x版本。
Here,发行说明。
答案 1 :(得分:0)
我使用queryDSL 4和Java 8流解决了这个问题:
List<UserDTO> results = new JPAQueryFactory(entityManager)
.select(qUser.id, qUser.login, qUser.firstname, qUser.lastname, qUser.dayofbirth, qUserDTO.district, qUserDTO.city)
.from(qUserDTO)
.innerJoin(qUserDTO.user(), qUser)
.where(qUserDTO.login.like(filters))
.fetch()
.stream()
.map(c -> new UserDTO(c.get(qUser.id), c.get(qUser.login), c.get(qUser.firstname), c.get(qUser.lastname),
c.get(qUser.dayofbirth), c.get(qUserDTO.district), c.get(qUserDTO.city)))
.collect(Collectors.toList());