开发者

Hibernate Criteria: Return different entity type than rooted entity?

I have entities similar to:

ProductLine: id, name
ProductLineContents: content_id, product_line_id
Content: id, text, updated_time

What I'd like to do is: for each product line, get the latest content (so if theres two content entries associated to one product line, the latest updated_time is rturned, and if one content item is associated to two product lines, it is returned twice). Something similar to:

select con开发者_开发百科tent.* from productline 
inner join productlinecontents
inner join content;

However I can't seem to figure out how to have Hibernate Criteria return a different entity than the original one it was created with. So if I wanted to start the criteria at the product line with createCriteria(ProductLine.class) along with the proper joins, then it only returns ProductLine objects, but I need Content objects.

What's the best way to accomplish this?

The actual data model is much more complex and can't be modified


ALIAS_TO_ENTITY_MAP map worked:

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;


Or you could do like this http://docs.jboss.org/hibernate/stable/core/reference/en/html_single/#querycriteria-associations

Use a resulttransformer Alias to entitymap

But Hql seems to be the most appropiate to use.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜