开发者

Criteria: search for two different entity classes

I have a "super entity" SuperEntity and three entities ChildEntity1, ..., ChildEntity3 which extends the super class.

It's easy to search for all entities in the database, i.e. we could use

session.createCriteria(SuperEntity.class);

It's no problem to search for one specific entity type, too, just replace the SuperEntity with any of the children to look for entities of that type.

But I have a problem when allowing 'multiple choice' for the types. I.e., it could be neccessary to search all entities of type 1 and 2, but not of type 3.

A first idea was to create two independent queries and join the results in a final list - but that would d开发者_如何学运维estroy the paging which uses offset and limit functionality of the database...

Is there a possibility in Criteria to join two different queries in one single result list?

Kind regards,

RoCMe


In HQL it may look like this (not sure if it works in all cases):

from SuperEntity e where e.class in [ChildEntity1, ChildEntity2]

In Criteria API you can just use IN resitriction for that.


One way to do this is to create a database view, that unions the result of the two tables, and then a model class for the view, and use hibernate on that. That way you can have paging, since the view will act like a table in that regard, and you can sort and limit the combined results.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜