gpt4 book ai didi

java - 如何通过jpql查询获取列表到构造函数

转载 作者:太空宇宙 更新时间:2023-11-04 11:39:52 26 4
gpt4 key购买 nike

我需要通过 JPQL 查询将列表传递给 VO(值对象)。例如:

我的配音课:

public class FamilyVO {
private String lastName;
private List<Name> names;

public FamilyVO () {
}

public FamilyVO (List<Name> names, String lastName) {
this.names = names;
this.lastName = lastName;
}
}

我使用new运算符的查询:

public class FamilyRepositoryBean {

@Override
public List<FamilyVO> findFamilies(Long id) {

StringBuilder jpql = new StringBuilder("SELECT new "+ FamilyVO.class.getName());
jpql.append("(f.names, f.lastName)");
jpql.append(" FROM Family f ");
jpql.append(" WHERE f.id = :pId");

Query query = em.createQuery(jpql.toString());
query.setParameter("pId", id);

return query.getResultList();
}
}

我的实体家庭:

@Entity
public class Family {

@OneToMany(mappedBy = "family")
protected List<Name> names = new ArrayList<Name>();

private String lastName;

public Family() {
}

public List<Name> getNames(){
return names;
}

public String getLastName() {
return lastName;
}
}

我的实体名称:

@Entity
public class Name {

@NotNull
@ManyToOne(fetch = FetchType.LAZY, optional = false)
private Family family;

public Name() {
}

public Family getFamily(){
return family;
}

}

当我运行此查询时,出现以下错误:

RuntimeException on EJB call java.lang.IllegalArgumentException: org.hibernate.hql.ast.QuerySyntaxException: Unable to locate appropriate constructor on class

最佳答案

如果你创建一个带参数的构造函数;您应该明确地提供不带参数的构造函数;

public FamilyVO (){ }

关于java - 如何通过jpql查询获取列表到构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42929295/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com