我有类似于以下内容的实体:
ProductLine: id, name
ProductLineContents: content_id, product_line_id
Content: id, text, updated_time我想做的是:对于每个产品行,获取最新的内容(因此,如果有两个与一个产品线相关联的内容条目,则返回最新的updated_time,如果一个内容项与两个产品线相关联,则返回两次)。类似于:
select content.* from productline
inner join productlinecontents
inner join content;然而,我似乎无法弄清楚如何让Hibernate标准返回与最初创建的实体不同的实体。因此,如果我想在产品行开始使用createCriteria(ProductLine.class)以及适当的联接,那么它只返回ProductLine对象,但我需要Content对象。
做这件事最好的方法是什么?
实际的数据模型要复杂得多,不能修改
发布于 2010-04-07 09:31:27
或者你可以像这样做http://docs.jboss.org/hibernate/stable/core/reference/en/html_single/#querycriteria-associations
使用结果转换器别名映射实体
但Hql似乎是最适合使用的。
发布于 2010-04-09 14:19:40
ALIAS_TO_ENTITY_MAP地图起作用:
criteria.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);
List itemsList = criteria.list();
if (itemsList == null || itemsList.isEmpty()) {
throw new EntityNotFoundException();
}
List<Content> content = new ArrayList<Content>();
Iterator iter = itemsList.iterator();
while ( iter.hasNext() ) {
Map map = (Map) iter.next();
content.add((Content) map.get("contentAlias"));
}
return content;https://stackoverflow.com/questions/2589922
复制相似问题