Hello there,
This is to provide further detail on a post I made earlier, since I think it warrants a new thread and I'll now be able to go into more detail.
I have a parent class that has a property, say of type 'Child'. The parent also has a collection of child objects associated with it. I am wanting to perform a query whereby I provide a collection of child objects and check whether any of the children appear in either the child property or the collection of childern on the parent object through the use of the criteria objects only. I can achieve the first requirement as follows:
Code:
final Criteria criteria = session.createCriteria(Parent.class);
criteria.add(Restrictions.in("child", getChildren()));
This would simply produce SQL akin to the following:
Code:
select * from parent p where p.childid in (x, y, z)
I cannot replace "child" (child object) with "children" (collection of child objects), though this is to be expected.
The SQL I would want to be generated in this case is something like:
Code:
select * from parent p where exists (select 1 from children c where c.parentid = p.parentid and c.childid in (x, y, z))
I realise I may be able to change my getChildren() function such that it returns, say the IDs of the child objects then perform the following:
Code:
criteria.createCriteria("children").add(Restrictions.in("id", getChildrenIDs()));
...however I have many of these types of queries to run and believe there must be a better way of doing this than by creating a collection/array of IDs for each collection I need to search against.
Has anybody experienced a similar issue? Am I barking up the wrong tree?!
Any help would be gratefully received. Many thanks in anticipation,
Carl