Grails:在许多桌子上投影?
我在 Grails 中的投影方面遇到一些问题。您能帮我检查一下并为我提出解决方案吗?
我想查询许多具有多对一关系的表上的数据,并在它们的某些属性上进行投影。例如:
类人{ 整数ID 字符串名称 字符串地址 静态 hasMany = [汽车:汽车] } 类车{ 整数ID 琴弦品牌 长价 个人所有者 静态属于= [所有者:人] }
那么,如何仅使用一个 withCriteria 查询(应用投影)来获取指定汽车的信息(包括品牌、价格和车主姓名)?是否可以使用:
Car.withCriteria { 预测{ 属性(“品牌”) 财产(“价格”) 属性(“所有者.名称”) } eq("id", 车号) }
我可以使用投影来获取某个指定人的信息以及他所有汽车的名称吗?例如:[01、Perter、01 Street A、[梅赛德斯、丰田、杜卡迪]]?
特殊情况:(具有上述 Person 类)
一个人可以加入多个组织,一个组织可以有一个“父”组织(反之亦然,一个组织可以有许多其他依赖组织)。但有一条规则:一个人只能加入给定组织的一个子组织。因此,对于给定的组织 O 和个人 P,获取 P 的信息以及以 P 作为成员的 O 所依赖组织的名称的最快方法是什么。我更喜欢使用 Grails 投影。这是数据模型:
类人{ 整数ID 字符串名称 字符串地址 静态 hasMany = [joinedOrgs : 组织] } 类组织{ 整数ID 字符串名称 组织父组织 静态 hasMany = [成员:个人,childOrgs:组织] }
我是 Grails 的新手,我想更多地了解 GORM。非常感谢您的帮助。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
试试这个
Try this
对于(第1),我认为您要寻找的内容在这里进行了解释
http://grails.1312388.n4.nabble.com/grails-reports-page-multiple-domain-criteria-join-td3510604.html
帖子重点介绍了使用
.createCriteria ()
For (No. 1) I think what you're looking for is explained here
http://grails.1312388.n4.nabble.com/grails-reports-page-multiple-domain-criteria-join-td3510604.html
the post highlights using
.createCriteria()
对于(1)和(2),我不知道是否有一种方法可以只用 1 个条件查询来完成您想要的操作,但替代方法似乎更简单和自然。对于(1):
对于(2)
关于(3),这里是一种设计问题。您当前的域设计没有反映您所描述的规则,因此很难维持该约束。您可以考虑映射 PersonOrganization 表并将childrenOrganization 设置为临时属性。例如:
之后,您可以使用像 getAllAncestors() 这样的回溯函数来确定组织的所有父层次结构,并在人员组织列表中查找它。这似乎不是最好的方法,但这是一种可能的方法。
For (1) and (2), I don't know if there's a way to do what you want with just 1 criteria query, but the alternative way is just seems to be more simple and natural. For (1):
For (2)
About (3), it's kind of design problem here. Your current domain design doesn't reflect the rule you describe, so it will be hard to maintain that constraint. You may consider mapping a PersonOrganization table and make childrenOrganization a transient property. For example:
After that, you can use a trace-back function like getAllAncestors() to determine all parent-hierarchy of an organization, the look it up in the person-organization list. It seems not the best way, but that's a possible way.