I have some problems with projection in Grails. Could you please help me review them and suggest solutions for me?
I want to query data on many tables which has many-to-one relationship and projection on some properties on both of them. For example:
class Person { int id String name String address static hasMany = [cars : Car] } class Car { int id String brand long price Person owner static belongsTo = [owner : Person] }
So, how can I use just one query withCriteria (apply projection) to get information of a specified car (include brand, price, and the owner name)? Is it possible to use:
Car.withCriteria { projections { property("brand") property("price") property("owner.name") } eq("id", carId) }
Can I use a projection to get information of one specified person along with name of all his cars? For example: [01, Perter, 01 Street A, [Mercedes, Toyota, Ducatti]]?
A special situation: (with above Person class)
A person can join many Organization, and an Organization can have one "parent" Organizations (and vice versa, an Organization can have many other depend organizations). But there's a rule: a person just can join only one child organization of a given organization. So with a given organization O and a person P, what is the fastest way to get information of P along with the name of depended organization of O which has P as a member. I prefer to use Grails projection.Here's data model:
class Person { int id String name String address static hasMany = [joinedOrgs : Organization] } class Organization { int id String name Organization parentOrg static hasMany = [members : Person, childOrgs : Organization] }
I'm a newbie with Grails, and I'd like to understand GORM much more. Thank you so much for your help.