使用(N)Hibernate混淆不同/聚合查询

时间:2010-04-08 19:05:39

标签: hibernate nhibernate

我不确定如何处理不将1:1映射到我的持久实体的查询 - 换句话说,不同的和聚合查询。例如,我需要检索一个不同的属性值列表,以填充下拉列表。

我应该为此查询返回的“实体”编写类和映射吗?或者我应该只使用本机数据库提供程序并使用本机数据集吗?

2 个答案:

答案 0 :(得分:2)

如果我理解正确,您的问题可以通过HQL中的标量查询来解决。例如:

Query q = session.createQuery("select i.id, i.description, i.initialPrice" +
    "from Item i where i.endDate > current_date()");

Iterator results = q.list().iterator();
while ( results.hasNext() ) {
  Object[] result = (Object[]) results.next();
  Long id = (Long) result[0];
  String description = (String) result[1];
  BigDecimal price = (BigDecimal) result[1];
}

您也可以在此类查询中使用distinct

这是another example

当然也可以使用native SQL from within Hibernate完成相同的工作。

答案 1 :(得分:0)

如果聚合查询非常复杂,请从聚合查询创建一个视图,并创建一个到视图的只读映射。

相关问题