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'}]}