Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用列表字段休眠自定义DTO_Java_Hibernate_Hql - Fatal编程技术网

Java 使用列表字段休眠自定义DTO

Java 使用列表字段休眠自定义DTO,java,hibernate,hql,Java,Hibernate,Hql,我有两个实体 @Entity public class DeptEmployee { @Id @GeneratedValue(strategy = GenerationType.SEQUENCE) private long id; private String employeeNumber; private String designation; private String name; @ManyToOne privat

我有两个实体

@Entity
public class DeptEmployee {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    private long id;

    private String employeeNumber;

    private String designation;

    private String name;

    @ManyToOne
    private Department department;

    // constructor, getters and setters 
}
Query Query=session.createQuery(“选择new com.baeldung.hibernate.pojo.Result(m.name,m.department.name)”
+“来自com.baeldung.hibernate.entities.DeptEmployee m”);
List results=query.List();
(感谢本文中的示例:)



现在,我想提取一个DTO,其中包含一个部门名称和该部门员工的姓名列表

public class Result2 {
    private String departmentName;

    private List<String> employeeNames;

    // Constructor ???

    public Result2() {
    }

    // getters and setters 
}
公共类结果2{
私有字符串departmentName;
私人名单雇员姓名;
//建造师???
公开结果2(){
}
//接球手和接球手
}
我的问题是:

  • 可能吗
  • Result2
    中的构造函数是什么
  • 提取此
    Result2
    的hql查询是什么

    • 我认为在HQL中无法实现这一点。你可以使用你已经拥有的东西。重新映射
      列表
      列表
      。首先按
      departmentName
      分组,然后可以创建
      Result2
      对象。sql查询和传输的数据将完全相同

      List results=query.List().stream()
      .collect(分组依据(结果::getDepartmentName))
      .entrySet().stream()
      .map(e->新结果2(
      e、 getKey(),
      e、 getValue().stream().map(结果::getEmployeeName.collect(收集器.toList()))
      .collect(Collectors.toList());
      
      public class Result {
          private String employeeName;
      
          private String departmentName;
      
          public Result(String employeeName, String departmentName) {
              this.employeeName = employeeName;
              this.departmentName = departmentName;
          }
      
          public Result() {
          }
      
          // getters and setters 
      }
      
      Query<Result> query = session.createQuery("select new com.baeldung.hibernate.pojo.Result(m.name, m.department.name)"
        + " from com.baeldung.hibernate.entities.DeptEmployee m");
      List<Result> results = query.list();
      
      public class Result2 {
          private String departmentName;
      
          private List<String> employeeNames;
      
          // Constructor ???
      
          public Result2() {
          }
      
          // getters and setters 
      }