得到一個列表構造需要通過JPQL查詢列表傳遞給VO(值對象)。 例如:如何通過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;
}
}
當我運行此查詢,出現以下錯誤:org.hibernate.hql.ast.QuerySyntaxException:無法找到階級適當的構造
對EJB調用 java.lang.IllegalArgumentException異常的RuntimeException
嗨!你能否也顯示「家庭」代碼? – Dherik
添加defualt構造你的類像這樣FamilyVO(){ –
的代碼錯了,我忘了把構造函數的正確名稱。 –