Hibernate Criteria查询仅获取特定列

时间:2016-04-30 12:27:20

标签: hibernate criteria projection

我有两个表:ReportRows和DataRows

表ReportRows:

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

另外有吸气剂和制定者;

表DataRows:

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

另外的getter和setter

从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

这只是我写的例子。关键是我需要每个查询不同的列。有一次它将是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);
}

方法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();
  }

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

甚至可以做我想做的事吗?仅根据Projection和table存储列索引的表列获取表dataRows中的特定列?

更新1:
我添加了定制变压器。一切正常。这样做是为了提高sql的速度。现在它正在使用少量列进行查询。真的很棒。我在hibernate中启用了show sql。在自定义转换器之后,它使用以下内容对每行新查询进行数据库处理:

where reportRowId = ?

你知道为什么吗?

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

2 个答案:

答案 0 :(得分:2)

  

我有问题,因为当我使用setResultTransformer时它会返回对象   使用空变量。例。 getId()为null(我添加了   projectionList.add(Projections.property(&#34; ID&#34));)

您需要指定投影的别名

projectionList.add(Projections.property("id").as("id"));
  

是的,它有效。谢谢。我还有一个问题。如果我想要回归   也在这个查询实体Report和列的value0,value1是什么我   必须做什么?

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

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

How to transform a flat result set using Hibernate

答案 1 :(得分:1)

session = sessionFactory.openSession();
Criteria criteria = session.createCriteria(DlrStatus.class);
criteria.add(Restrictions.eq("userId",userId)).add(Restrictions.eq("jobId",jobId))
        .add(Restrictions.eq("status", status)).setFirstResult(start).setMaxResults(max).addOrder(Order.desc("id"));
ProjectionList projectionList = Projections.projectionList();
projectionList.add(Projections.property("mobile").as("mobile"));
projectionList.add(Projections.property("Sender").as("sender"));
projectionList.add(Projections.property("message").as("message"));
projectionList.add(Projections.property("loggedAt").as("loggedAt"));
projectionList.add(Projections.property("dlrTime").as("dlrTime"));
criteria.setProjection(projectionList);
criteria.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);
List<DlrStatus> list = criteria.list();
相关问题