-->
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.  [ 3 posts ] 
Author Message
 Post subject: query to get many-to-many objects
PostPosted: Mon Sep 07, 2009 5:40 am 
Newbie

Joined: Mon Sep 07, 2009 5:35 am
Posts: 4
Hi,
I'm new to hibernate and I'm trying to do some basic stuff...
I have countries and partners. Moreover countries have many partners and this association is defined by means of the table partner_in_country.
here is how I mapped the classes:

Country
Code:
<hibernate-mapping>
   <class name="ch.orange.rps_ws.domain.Country" table="Country">
      <id name="id" column="country_id">
         <generator class="native"/>
      </id>
      <property name="country_fr" column="country_fr"/>
      <property name="country_it" column="country_it"/>
      <property name="country_de" column="country_de"/>
      <property name="country_en" column="country_en"/>
      
      <set name="partners" table="partner_in_country">
           <key column="country_id"/>
           <many-to-many column="partner_id" class="ch.orange.rps_ws.domain.Partner"/>
          </set>
          
   </class>
   
</hibernate-mapping>


Partner
Code:
<hibernate-mapping>
   <class name="ch.orange.rps_ws.domain.Partner" table="Partner">
      <id name="id" column="partner_id">
         <generator class="assigned"/>
      </id>
      <property name="official_name" column="official_name"/>
      <property name="network_name" column="network_name"/>
      <property name="handset_display" column="handset_display"/>
      <property name="web_address" column="web_address"/>
      <property name="map_url" column="map_url"/>
      <property name="map_code_2g" column="map_code_2g"/>
      <property name="map_code_3g" column="map_code_3g"/>
      <property name="visible" column="visible"/>
      
      <set name="services" table="partner_has_service">
           <key column="partner_id"/>
           <many-to-many column="service_id" class="ch.orange.rps_ws.domain.Service"/>
          </set>
   </class>
</hibernate-mapping>


now I'd like to implement a method in the PartnerDAO (or in the CountryDAO) which returns all the partners in one country by using a named query.

How should I do that???? or better.. how should the query look like?


Top
 Profile  
 
 Post subject: Re: query to get many-to-many objects
PostPosted: Mon Sep 07, 2009 7:33 am 
Beginner
Beginner

Joined: Sat Aug 01, 2009 5:28 am
Posts: 42
may be
<query name="multiple.partner">from Partner as P where size(P.countries) &gt; 0</query>

Query q = s.getNamedQuery("multiple.partner");

List<Partner> l = q.list();
for(Partner p : l) {
System.out.println("Partner: " + p.getName());
for(Country c : owner.getCountries()) {
System.out.println("\Countries: " + c.getName());
}
}

_________________
Warm Regards,
Tarun Walia


Top
 Profile  
 
 Post subject: Re: query to get many-to-many objects
PostPosted: Mon Sep 07, 2009 7:48 am 
Newbie

Joined: Mon Sep 07, 2009 5:35 am
Posts: 4
I solved like this:

Code:
<query name="partners.by.country.id">select country.partners as partner from Country as country where country.id=?</query>


but now I get also the sets included in partners.. since I only want the information included in the partner table, how do I avoid hibernate to retrieve the services of a partner?


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