我在Grails中遇到了一些投影问题。你能帮我审查一下并为我提供解决方案吗?
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] }
我是一个Grails的新手,我想更深入地了解GORM。非常感谢您的帮助。
property('owner.name','owner.name')
中的owner.name
。 即使您可以在没有别名的情况下获得上述结果,但您需要编写更多的代码。请参见以下示例projections { owner { property('name', 'name') } property('brand','brand') property('price','price') }
如果您仍然有任何疑问,或者这不是您要的答案,请告诉我。 谢谢 - Muhammad Aamir TalibcreateAlias('owner', 'owner')
的情况下编写上述代码,那么在较新版本的grails中可能是有可能的。 - Muhammad Aamir Talib