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.  [ 7 posts ] 
Author Message
 Post subject: How to write that query?
PostPosted: Mon Aug 11, 2008 3:27 pm 
Newbie

Joined: Mon Aug 11, 2008 3:17 pm
Posts: 6
Hi..

I have 2 classes, Player and Team. Team has a collection of players, and my problem is to write query wich will select from my database all Players wich has no team, I mean players wich are not in any team.

It could be easy if I use classic SQL, because TeamId is a field in Players table, but how can i get whole player object using classic SQL? Or wich hibernate query will return me list of this player objects?

My mapping for those classes:

Code:
<class name="Player" table="Players">

    <id name="Id" column="Id" type="String">
      <generator class="uuid.hex" />
    </id>

    <property name="FirstName" type="String" length="40">
      <column name="FirstName" />
    </property>

    <property name="LastName" type="String" length="40">
      <column name="LastName" />
    </property>

    <many-to-one name="UserID" class="User" cascade="all"  lazy="false" column="UserID"/>   

  </class>

  <class name="Team" table="Teams">

    <id name="Id" column="Id" type="String">
      <generator class="uuid.hex" />
    </id>


    <property name="Name" type="String" length="150">
      <column name="Name" />
    </property>

    <bag name="Players" cascade="all" lazy="false">
      <key column="TeamId" />
      <one-to-many class="Player" />
    </bag>

  </class>


I hope that someone can help me..
Thanks a lot...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 11, 2008 4:40 pm 
Newbie

Joined: Tue Jan 22, 2008 5:51 pm
Posts: 7
Location: Brazil
Make a bidirectional property anchor in a "Player" class that doesn't cascade anything...
hmm, something like this:

Code:
<many-to-one name="Team" column="TeamId" class="Team" cascade="none" not-null="false" />


so, when u load list of "Teams", in each have an list of "Players" that will have an object reference do "Team". i gess u get it...

and, in hql, if u want to list a "Players" without a team. try that:

Code:
from Player player where player.Team is null



Rapid tip: Try not use cascade="all" in "child" classes, because, the hibernate will generate "update" statments to update your "father" classes....

check it <many-to-one name="UserID" class="User" cascade="all" lazy="false" column="UserID"/>

use cascade="none" if the intention is only relationship, not commit all "relation" data... =)

cya


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 11, 2008 4:58 pm 
Newbie

Joined: Tue Jan 22, 2008 5:51 pm
Posts: 7
Location: Brazil
Ups, double posted!! ^^


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 11, 2008 5:56 pm 
Newbie

Joined: Mon Aug 11, 2008 3:17 pm
Posts: 6
Nice.. i thought about that solution, but is there no posibility to make it in just one query without extra properties?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 12, 2008 2:24 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Try this:

Code:
from Player p where not exist (select pls.Id from Team t join t.Players pls where p.Id = pls.Id)

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 12, 2008 8:09 am 
Newbie

Joined: Mon Aug 11, 2008 3:17 pm
Posts: 6
Sorry, but..

Quote:
Incorrect query syntax [from Entities.Player p where not exist (select pls.Id from Entities.Team t join t.Players pls where p.Id = pls.Id)]


Any ideas?

--------


Ohh.. I've change it to

Quote:
select p from Player p where p not in (select pls.Id from Team t join t.Players pls where p.Id = pls.Id)


and now it works correctly ;)

Thanx for your help..


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 12, 2008 8:44 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
"exists" not "exist" .. was already misspelled in my original post.

_________________
--Wolfgang


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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.