-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Query with dependent objects
PostPosted: Thu Apr 14, 2005 5:26 am 
Beginner
Beginner

Joined: Thu Apr 14, 2005 4:29 am
Posts: 28
Hi,

I have three tables:
ship (columns: id, name, ..)
container (columns: id, name, ship..)
box (columns: id, name, container ..)
and corresponding java classes.

A ship contains containers and a container contains boxes.

<class name="Container" table="container" dynamic-update="true">
<id name="id" column="id">
<generator class="assigned"/>
</id>
<property name="name" column="name" not-null="true"/>
<many-to-one name="ship" column="ship"/>
<bag name="boxes" inverse="true" lazy="true" cascade="all">
<key column="box"/>
<one-to-many class="Box"/>
</bag>
</class>
<class name="Box" table="box" dynamic-update="true">
<id name="id" column="id">
<generator class="assigned"/>
</id>
<property name="name" column="name" not-null="true"/>
<many-to-one name="container" column="container"/>
</class>

So far so good. Now I want a query like this (search all boxes of a given ship):
Ship myShip = ..;
List boxes = session.createCriteria(Box.class).add(Restrictions.eq("ship", myShip)).list();

That’s why I have add a method getShip() to the class box (for container such a method already exists).
public Project getShip()
{
return getContainer().getShip();
}

But this doesn’t work. Have I to edit the mapping files and if so how? Can I set a relation in the mapping file between box and ship?

Every hint is very appreciated!

Regards, Detlev







Hibernate version: 3
Debug level Hibernate log excerpt:
org.hibernate.QueryException: could not resolve property


Top
 Profile  
 
 Post subject: Re: Query with dependent objects
PostPosted: Thu Apr 14, 2005 9:29 am 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
Look for examples using createAlias()...

You don't need the getShip() method... I think you're query would be something like this:

List boxes = session.createCriteria(Box.class).createAlias("container","cntr").add(Restrictions.eq("cntr.ship",myShip)).list();

Note: This might not be 100% correct but should get you started in the right direction.


Top
 Profile  
 
 Post subject: Re: Query with dependent objects
PostPosted: Thu Apr 14, 2005 12:31 pm 
Beginner
Beginner

Joined: Thu Apr 14, 2005 4:29 am
Posts: 28
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


Top
 Profile  
 
 Post subject: Re: Query with dependent objects
PostPosted: Thu Apr 14, 2005 12:50 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
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.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 15, 2005 5:16 am 
Beginner
Beginner

Joined: Thu Apr 14, 2005 4:29 am
Posts: 28
Thanks a lot four your very good help!

Detlev


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.