File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Object Relational Mapping and the fly likes Querying on Child Objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Querying on Child Objects" Watch "Querying on Child Objects" New topic

Querying on Child Objects

Salil Surendran
Ranch Hand

Joined: Jan 16, 2004
Posts: 40
Hello Friends,
I have a problem. I have a parent class named 'Trade' and each trade and many child objects named 'TradeEvents'. I want to use a Criteria object and create a query such that I can add restrictions on both objects. For eg. I want to get trades who has status as 'O' and get it's corresponding trade events that has notional greater than 100.

so i use a critiia like this:

Criteria criteria = HibernateSessionFactory.getSession().createCriteria(Trades.class);
criteria.createCriteria("tradeEvents").add("notional", new Double(100)));
List list = criteria.list();

However, when I execute the criteria it returns me trades that are open which is fine but the child object(trade events) that I obtain has all the trade events belonging to that trade even though it's notional is greater than 100. So the child objects are not being restricted. How can I achieve this?

Thanks,<br />Salil Surendran
Susanta Chatterjee
Ranch Hand

Joined: Aug 12, 2002
Posts: 102
I am not an expert, I think it should look like following:
Salil Surendran
Ranch Hand

Joined: Jan 16, 2004
Posts: 40
Your code is correct but it produces the same result. In you code you also need to add an alias to tradeEvents to make it work.
I agree. Here's the link:
subject: Querying on Child Objects
It's not a secret anymore!