休眠条件查询以仅获取特定列

Car*_*ine 6 hibernate criteria projection

我有两个表:ReportRows 和 DataRows

表格报告行:

@Id
@Column(name = "ID")
private Long id;

@ManyToOne
@JoinColumn(name = "T_REPORT_CODE")
  private Report report;

@OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinColumn(name = "T_DATA_ROWS_ID")
  private DataRow dataRow;
Run Code Online (Sandbox Code Playgroud)

此外还有 getter 和 setter;

表数据行:

@Id
@Column(name = "ID")
    private Long id;

@Column(name = "VALUE_0")
    private String value0;

@Column(name = "VALUE_1")
    private String value1;

@Column(name = "VALUE_2")
    private String value2;

...

@Column(name = "VALUE_30")
    private String value30;
Run Code Online (Sandbox Code Playgroud)

另外getter和setter

Select from ReportRows 和 DataRows 在列出数据时从两个表中获取所有列。我只需要 DataRows 中的几列(我指定了它)。

选择我想要的看起来像这样:

SELECT 
dataRows.value0,
dataRows.value1
from
t_report_rows this_
inner join
t_reports report_ on report_.id = this_.report_id
inner join
t_data_rows dataRows_ on dataRows_.id = this_t_data_rows_id
where report_.id = 1
Run Code Online (Sandbox Code Playgroud)

这只是我写的例子。关键是我每个查询都需要不同的列。第一次是 value0、value1,第二次是 value0、value2。

当我进行查询时,我知道我需要哪些列。我创建这样的标准(列是我从中检索列名称的类):

public List<ReportRow> list(Criteria criteria, Long reportId) {
    criteria.add(Restrictions.eq("report.id", reportId));
    ProjectionList projectionList = Projections.projectionList();
    for (Column col : columnList) {
        String columnName = getColumnId(col);
        projectionList.add(Projections.property(columnName));
        //I also tried like this:
        // projectionList.add(Projections.property(columnName), columnName);
        //or like this:
        //projectionList.add(Projections.property(columnName), getColumnId2(col));
        //also added this line with each of above:
        //criteria.setResultTransformer(Transformers.aliasToBean(ReportRow.class));
    }
    criteria.setProjection(projectionList);

    return list(criteria);
}
Run Code Online (Sandbox Code Playgroud)

方法 getColumnId 和 getColumnId2:

protected String getColumnId(Column col) {
    StringBuilder b = new StringBuilder();
    b.append("dataRow.value");
    b.append(col.getRowIndex());
    return b.toString();
  }

  protected String getColumnId2(Column col) {
    StringBuilder b = new StringBuilder();
    b.append("value");
    b.append(col.getRowIndex());
    return b.toString();
  }
Run Code Online (Sandbox Code Playgroud)

我收到错误:org.hibernate.QueryException:无法解析属性:dataRow.value0

甚至有可能做我想做的事吗?仅从基于投影的表数据行中获取特定列和存储列索引的表列?

更新 1:
我添加了自定义转换器。一切正常。这样做是为了提高sql的速度。现在它正在使用少量列进行查询。真的很棒。我在休眠中打开了 show sql 。在自定义转换器之后,它正在对每一行的数据库进行新查询:

where reportRowId = ?
Run Code Online (Sandbox Code Playgroud)

你知道为什么吗?

自定义 Transformer transformTuple 方法现在看起来像这样:

@Override
  public Object transformTuple(Object[] tuple, String[] aliases) {
    ReportRow row = new ReportRow();
    DataRow dataRow = new DataRow();
    row.setGridRow(dataRow);

    for (int i = 0 ; i < aliases.length; i++) {
      if ("id".equals(aliases[i])) {
        row.setId((Long) tuple[i]);
      } else if ("entityVersion".equals(aliases[i])) {
        row.setEntityVersion((Long) tuple[i]);
      } else if ("dataRowId".equals(aliases[i])) {
        dataRow.setId((Long) tuple[i]);
      } else if ("rowEntityVersion".equals(aliases[i])) {
        dataRow.setEntityVersion((Long) tuple[i]);
      } else if ("value0".equals(aliases[i])) {
        dataRow.setValue0((String) tuple[i]);
      } else if ("value1".equals(aliases[i])) {
        dataRow.setValue1((String) tuple[i]);
      }
    }

    return row;
  }
Run Code Online (Sandbox Code Playgroud)

v.l*_*nev 4

我有问题,因为当我 setResultTransformer 时它返回带有 null 变量的对象。例子。getId() 为 null (我添加了projectionList.add(Projections.property("id"));)

您需要指定投影的别名

projectionList.add(Projections.property("id").as("id"));
Run Code Online (Sandbox Code Playgroud)

是的,它起作用了。谢谢。我还有一个问题。如果我想在此查询实体 Report 和列 value0、value1 中也返回,我必须做什么?

Report您可以通过指定其列的投影来获取其中的一部分。您需要获取ReportRowsroot 和属性ReportDataRow使用嵌套投影。

对于嵌套投影,您可以使用自定义转换器

如何使用 Hibernate 转换平面结果集