Grails:在许多表上的投影? [英] Grails: Projection on many tables?

查看:19
本文介绍了Grails:在许多表上的投影?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Grails 中的投影有一些问题.你能帮我审查它们并为我提出解决方案吗?

I have some problems with projection in Grails. Could you please help me review them and suggest solutions for me?

  1. 我想查询许多表上的数据,这些表在它们两个的某些属性上具有多对一关系和投影.例如:

  1. 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)
}

  • 我可以使用投影来获取指定人员的信息以及他所有汽车的名称吗?例如:[01, Perter, 01 Street A, [Mercedes, Toyota, Ducatti]]?

  • 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]]?

    一种特殊情况:(有上面的Person类)
    一个人可以加入多个组织,一个组织可以有一个父"组织(反之亦然,一个组织可以有许多其他从属组织).但是有一条规则:一个人只能加入给定组织的一个子组织.因此,对于给定的组织 O 和个人 P,获取 P 的信息以及具有 P 作为成员的 O 的附属组织的名称的最快方法是什么.我更喜欢使用 Grails 投影.

    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.

    这是数据模型:

    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.非常感谢您的帮助.

    I'm a newbie with Grails, and I'd like to understand GORM much more. Thank you so much for your help.

    推荐答案

    对于 (1) 和 (2),我不知道是否有一种方法可以只使用 1 个条件查询来完成您想要的操作,但是另一种方法只是似乎更简单自然.对于(1):

    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):

    def car = Car.get(carId)
    def owners = car.owner?.name
    

    对于(2)

    def person = Person.get(personId)
    def cars = person.cars*.name
    

    关于(3),这是一种设计问题.您当前的域设计并未反映您描述的规则,因此很难维持该约束.您可以考虑映射 PersonOrganization 表并使 childrenOrganization 成为临时属性.例如:

    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:

    class Organization {
        int id
        String name
        Organization parent
        static transients = ['children']
        ...
    
        Boolean children() {
            def children = Organization.createCriteria().list() { 
                eq ('parent', this) 
            }
            return children
        }
    }
    

    之后,您可以使用像 getAllAncestors() 这样的回溯函数来确定组织的所有父层次结构,并在个人组织列表中进行查找.这似乎不是最好的方法,但这是一种可能的方法.

    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.

    这篇关于Grails:在许多表上的投影?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆