views:

86

answers:

2

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 content.* 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

+1  A: 

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.

cws
+1  A: 

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