tags:

views:

235

answers:

1

I'm not very good in SQL and HQL...

I have two domains:

class Hotel {
 String name
}

class Room {
 Hotel hotel
 float price
}

How many hotels have at least one room ?

+1  A: 

You probably want to make this a bi-directional relationship.

class Hotel {
 String name;
 List<Room> rooms;
}

class Room {
 Hotel hotel
 float price
}

Then HQL:

 from Hotel h where size(h.rooms) >= 1 

Will return Hotels where the rooms collection has at least one value.

More details here.

Vincent Ramdhanie
I know about this way but i can't modify DB structure(domains) at this moment... Maybe in future it will be done in this way but now i can't :(
Olexandr
Then in plain SQL: select count(*), hotel.name from room join hotel on room.hotel_id = hotel.hotel_id group by hotel.hotel_id having count(*) > 1
Vincent Ramdhanie
Thx for advises. I've implemented in this way(all fields are not null so every room has a not-null hotel field):Room.executeQuery("select count(rm) from Room as rm group by rm.hotel")[0]
Olexandr