detlev wrote:
pksiv wrote:
List boxes = session.createCriteria(Box.class).createAlias("container","cntr").add(Restrictions.eq("cntr.ship",myShip)).list();
Thank you very much, that works fine.
But what is if I have a fourth table books (ship ==> container ==> box ==> books). Is this also so possible (get all books in a ship)?
Regards, Detlev
It should work indefinitely, but I would be concerned with the efficiency of the query past a certain point.
I think it might be something like this, but I'm not testing any of this code...
List books = session.createCriteria(Book.class).createAlias("box","bx").createAlias("bx.container","cntr").add(Restrictions.eq("cntr.ship",myShip)).list();
Section 15.4 gives several examples.
I am assuming that you're actually going to add some additional Restrictions to this Criteria, otherwise you don't need to do any of this.
Code:
List books = new ArrayList();
Ship ship = (Ship)session.createCriteria(Ship.class).add(Restrictions.eq("id",myShipId).uniqueResult();
for(iterator i = ship.getContainers().iterator();i.hasNext();) {
Container container = (Container)i.next();
for(iterator j = container.getBoxes().iterator();j.hasNext();) {
Box box = (Box)j.next();
books.addAll(box.getBooks());
}
}
Although I guess this method performs a lot more SQL depending on how many containers and boxes you have on a ship.