tags:

views:

67

answers:

2

Hi!

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 destroy 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

A: 

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.

Superfilin
The way I read the API also suggests the Criteria API is not the way to go here. The HQL API as shown here is more flexible for this kind of queries.
extraneon
A: 

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.

Andrei Fierbinteanu