简体   繁体   English

如何通过jpql查询获取构造函数的列表

[英]How to get a list to a constructor through jpql query

I need to pass a list to a VO (Value Object) through a JPQL query. 我需要通过JPQL查询将列表传递给VO(值对象)。 For example: 例如:

My VO class: 我的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;
  }
}

My query using the new operator: 我的查询使用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();
    }
}    

My entity Family: 我的实体家庭:

@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;
    }
}

My entity Name: 我的实体名称:

@Entity
public class Name  {

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

   public Name() {
   }

   public Family getFamily(){
        return family;
   }

}

When I run this query, the following error occurs: 当我运行此查询时,会发生以下错误:

RuntimeException on EJB call java.lang.IllegalArgumentException: org.hibernate.hql.ast.QuerySyntaxException: Unable to locate appropriate constructor on class EJB调用java.lang.IllegalArgumentException上的RuntimeException:org.hibernate.hql.ast.QuerySyntaxException:无法在类上找到合适的构造函数

If you make a constructor with parameters; 如果你创建一个带参数的构造函数; you should provide the constructor with no parameters, explicity; 你应该为构造函数提供没有参数,明确的;

public FamilyVO (){ }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM