-->
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.  [ 8 posts ] 
Author Message
 Post subject: Question of searching collection object (Urgent)
PostPosted: Thu Sep 15, 2005 6:01 am 
Newbie

Joined: Tue Apr 05, 2005 5:39 am
Posts: 6
Location: Stuttgart, Germany
Hi,
I need some idea. Actually I have "Profile" object and it has one to many relationship with "Country" object...That means a "Profile" can hold multiple "Country"...In simple code is something like.

public class Profile{

private Long id; //The hibernate id
private Set countries = new HashSet();

//getter and setter for countries and id

}

public class Country{
private Long id;
private String countryName;
//getter and setters
}

Now in my database I have lots of profile with references to collection of different countries.

I am designing a search screen...In the screen user can select any no of countries displayed in the form of check boxes and on clicking search button, hibernate should retrieve those profiles where each of those profile is having atleast reference to those selected countries. I mean it is an "AND" operation...If the user selects "Canada", "Japan" and "UK"...then only those profile which contains atleast all these 3 as reference should be retrieved.

Please help.

Best Regards,
Suvankar


Top
 Profile  
 
 Post subject: Try something like this
PostPosted: Thu Sep 15, 2005 9:26 am 
Contributor
Contributor

Joined: Thu Nov 06, 2003 9:49 pm
Posts: 104
Location: New York, NY
Try something like this:

select distinct p from Profile p where p.countries.name in ('US','AU')

I have a working example of this where p.countries is a Set of countries that is many-to-many.

Code:
   public void testCountry()
    {
        Session s = sf.openSession();
        Transaction tx = s.beginTransaction();
        try
        {
            s.save(new Country("US"));
            s.save(new Country("DE"));
            s.save(new Country("UK"));
            s.save(new Country("FR"));
            s.save(new Country("AU"));
            tx.commit();
        }
        catch (Exception e)
        {
            tx.rollback();
        }

        tx = s.beginTransaction();
        try
        {
            Country us = (Country) s.createQuery("from Country c where c.name='US'").list().get(0);
            Country de = (Country) s.createQuery("from Country c where c.name='DE'").list().get(0);
            Country uk = (Country) s.createQuery("from Country c where c.name='UK'").list().get(0);
            Country au = (Country) s.createQuery("from Country c where c.name='AU'").list().get(0);
            Profile p = new Profile();
            p.setName("Josh");
            p.getCountries().add(us);
            p.getCountries().add(de);
            s.save(p);
            p = new Profile();
            p.setName("Werner");
            p.getCountries().add(de);
            p.getCountries().add(uk);
            s.save(p);
            p = new Profile();
            p.setName("Gavin");
            p.getCountries().add(au);
            p.getCountries().add(us);
            s.save(p);
            tx.commit();
        }
        catch (Exception e)
        {
            tx.rollback();
        }

        List list = s.createQuery("from Profile p order by p.name").list();
        for (Iterator iterator = list.iterator(); iterator.hasNext();)
        {
            Object c = iterator.next();
            System.out.println(c.toString());
        }

        list = s.createQuery("select distinct p from Profile p where p.countries.name in ('US','AU') order by p.name").list();
        for (Iterator iterator = list.iterator(); iterator.hasNext();)
        {
            Object c = iterator.next();
            System.out.println(c.toString());
        }

        s.close();
    }


This produces:

Code:
09:22:26,234 DEBUG SchemaExport:161 - create table country (
    id bigint generated by default as identity (start with 1),
    name varchar(255),
    primary key (id)
)
09:22:26,250 DEBUG SchemaExport:161 - create table profile (
    id bigint generated by default as identity (start with 1),
    name varchar(255),
    primary key (id)
)
09:22:26,265 DEBUG SchemaExport:161 - create table profile_country (
    profile_id bigint,
    countries_id bigint
)
09:22:26,265 DEBUG SchemaExport:161 - alter table profile_country
    add constraint FKC5C9CF00912B3BB
    foreign key (profile_id)
    references profile
09:22:26,281 DEBUG SchemaExport:161 - alter table profile_country
    add constraint FKC5C9CF00D3A9D0BD
    foreign key (countries_id)
    references country
09:22:26,281  INFO SchemaExport:173 - schema export complete
09:22:26,296  INFO DriverManagerConnectionProvider:147 - cleaning up connection pool: jdbc:hsqldb:data/testdb
09:22:26,296  INFO SessionFactoryImpl:379 - Checking 0 named queries
09:22:26,328  INFO DriverManagerConnectionProvider:147 - cleaning up connection pool: jdbc:hsqldb:data/testdb
Hibernate: insert into country (name, id) values (?, null)
Hibernate: call identity()
Hibernate: insert into country (name, id) values (?, null)
Hibernate: call identity()
Hibernate: insert into country (name, id) values (?, null)
Hibernate: call identity()
Hibernate: insert into country (name, id) values (?, null)
Hibernate: call identity()
Hibernate: insert into country (name, id) values (?, null)
Hibernate: call identity()
Hibernate: select country0_.id as id, country0_.name as name0_ from country country0_ where country0_.name='US'
Hibernate: select country0_.id as id, country0_.name as name0_ from country country0_ where country0_.name='DE'
Hibernate: select country0_.id as id, country0_.name as name0_ from country country0_ where country0_.name='UK'
Hibernate: select country0_.id as id, country0_.name as name0_ from country country0_ where country0_.name='AU'
Hibernate: insert into profile (name, id) values (?, null)
Hibernate: call identity()
Hibernate: insert into profile (name, id) values (?, null)
Hibernate: call identity()
Hibernate: insert into profile (name, id) values (?, null)
Hibernate: call identity()
Hibernate: insert into profile_country (profile_id, countries_id) values (?, ?)
Hibernate: insert into profile_country (profile_id, countries_id) values (?, ?)
Hibernate: insert into profile_country (profile_id, countries_id) values (?, ?)
Hibernate: insert into profile_country (profile_id, countries_id) values (?, ?)
Hibernate: insert into profile_country (profile_id, countries_id) values (?, ?)
Hibernate: insert into profile_country (profile_id, countries_id) values (?, ?)
Hibernate: select profile0_.id as id, profile0_.name as name1_ from profile profile0_ order by profile0_.name
Profile{id=3, name='Gavin', countries=[Country{id=5, name='AU'}, Country{id=1, name='US'}]}
Profile{id=1, name='Josh', countries=[Country{id=1, name='US'}, Country{id=2, name='DE'}]}
Profile{id=2, name='Werner', countries=[Country{id=3, name='UK'}, Country{id=2, name='DE'}]}
Hibernate: select distinct profile0_.id as id, profile0_.name as name1_ from profile profile0_, profile_country countries1_, country country2_ where profile0_.id=countries1_.profile_id and countries1_.countries_id=country2_.id and (country2_.name in ('US' , 'AU')) order by profile0_.name
Profile{id=3, name='Gavin', countries=[Country{id=5, name='AU'}, Country{id=1, name='US'}]}
Profile{id=1, name='Josh', countries=[Country{id=1, name='US'}, Country{id=2, name='DE'}]}


Top
 Profile  
 
 Post subject: Question of searching collection object
PostPosted: Thu Sep 15, 2005 9:43 am 
Newbie

Joined: Tue Apr 05, 2005 5:39 am
Posts: 6
Location: Stuttgart, Germany
Hello Joshua,
Thank you so much for the reply. However I need slightly different result. With respect to your example , the output for the query

list = s.createQuery("select distinct p from Profile p where p.countries.name in ('US','AU') order by p.name").list();

should return me only one profile , which is

p.setName("Gavin");
p.getCountries().add(au);
p.getCountries().add(us);


becoz this is the only profile contains exactly 'US' and 'AU'. I am sorry I think I have miss explained the problem in the initial post.

Thanks again,
Suvankar


Top
 Profile  
 
 Post subject: Try IN ALL
PostPosted: Thu Sep 15, 2005 1:13 pm 
Contributor
Contributor

Joined: Thu Nov 06, 2003 9:49 pm
Posts: 104
Location: New York, NY
Try IN ALL.


Top
 Profile  
 
 Post subject: Question of searching collection object
PostPosted: Fri Sep 16, 2005 4:21 am 
Newbie

Joined: Tue Apr 05, 2005 5:39 am
Posts: 6
Location: Stuttgart, Germany
Hello Joshua,
I tried but not working :-(

Regards,
Suvankar


Top
 Profile  
 
 Post subject: How about this?
PostPosted: Fri Sep 16, 2005 7:45 am 
Contributor
Contributor

Joined: Thu Nov 06, 2003 9:49 pm
Posts: 104
Location: New York, NY
How about this?

select p from Profile p where p.countries.name = 'US' and p.countries.name = 'AU' order by p.name


Top
 Profile  
 
 Post subject: Question of searching collection object
PostPosted: Fri Sep 16, 2005 8:44 am 
Newbie

Joined: Tue Apr 05, 2005 5:39 am
Posts: 6
Location: Stuttgart, Germany
hi Joshua,
I tried but it is not getting me the expected result. I thought of using "intersect" but then i found from hibernate's web site that hibernate does not support yet. So may be I will have to post process the query result in my service / business layer to filter out the desired result. If you still find something do post me. Thanks again for all your valuable inputs.

Regards,
Suvankar


Top
 Profile  
 
 Post subject: Works for me
PostPosted: Fri Sep 16, 2005 9:11 am 
Contributor
Contributor

Joined: Thu Nov 06, 2003 9:49 pm
Posts: 104
Location: New York, NY
Well, it worked for me. I don't know what results you were expecting.


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