在QueryDsl中选择构造函数OneToMany关系

时间:2017-08-22 13:58:02

标签: java hibernate querydsl

我想使用QueryDsl从数据库中选择元素。

    final QUserEntity user = QUserEntity.userEntity;
    new JPAQuery<UserResponse>(em)
                    .select(Projections.constructor(User.class, user.id, user.name, 
                            Projections.constructor(AddressResponse.class, user.address.id, user.address.name), 
                            user.pets))
                    .from(user).fetch();

我想让它在User中使用构造函数:

public UserResponse(final String id, final String name, final AddressResponse address, Set<pets> pets) {
    this.id = id;
    this.name = name;
    this.address = address;
    this.pets = pets;
}

用户和地址工作正常,但我不知道如何处理宠物,这是一个OneToMany实体,因此是一组实体。

'Pets'在用户中具有以下关系:

@OneToMany(fetch = FetchType.LAZY)
@JoinColumn(name = 'petId', referencedColumnName = 'petId')
private Set<Pet> pets;

但是,当我尝试运行此操作时,我收到以下错误:

com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near the keyword 'as'.

生成的SQL有语法错误:

SELECT
  chargepoin0_.userid    AS col_0_0_,
  chargepoin0_.username  AS col_1_0_,
  chargepoin0_.addressid   AS col_2_0_,
  addressent5_.addressname AS col_3_0_,.AS col_4_0_, pets6_.itemid AS itemid1_4_, pets6_.connected AS connecte2_4_, pets6_.objectstatecode AS objectst3_4_ FROM user user0_

提前致谢, 马库斯

0 个答案:

没有答案