I have been spending some time on this. My problem involves bringing back domain objects with certain criteria and conditions:
I have a large set of bikes. There is a possibility of having multiple bikes with similar wheel size. For example I can have 5 bikes:
owner_id | bike | wheel | price | active | toolset | forLance
_________|______|_______|_______|________|_________|__________
15459 |liner | 12 | 100 | Y | null | H
15459 |larker| 15 | 150 | Y | null | H
15459 |jefro | 21 | 225 | Y | null | H
15459 |raz | 21 | 230 | Y | null | L
15459 |jynx | 21 | 295 | Y | null | P
My query below retrieves the all the bikes with non-duplicate wheel size and lowest price.
MySQL query:
select * from bike b
where b.owner_id = 15459
and not exists( select * from bike
where wheels = b.wheels AND price < b.price
and owner_id = b.owner_id) and b.active = 'Y';
The result will give me the rows with the bikes : liner, larker, and jefro.
Is there an equivalent way to do this in grails//groovy? ( Get liner, larker, and jefro into a list of domain objects)
I have tried using a structure like:
def bikes = Bike.executeQuery(...)
or
def bike = Bike.findAll(...)
But I cannot seem to execute a query with a similar structure to the MySQL script I made.
Thanks for the help!
In your MySQL you are using subqueries to retrieve the data. AFAIK this is not possible with GORM. What you could do is writing it in Hibernate Query Language (HQL)
Lets assume your Domain Class is called Bike.
Bike.findAll("from Bike as b where b.active = 'Y' and b.owner = :owner and b.id in elements(select b1.id from Bike where b1.owner = :owner and b1.active = 'Y' and b1.price < b.price)", ['owner':owner])