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

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

问题描述

我在Grails中投影时遇到了一些问题。你能帮我查看一下,并为我提供解决方案吗?


  1. 我想查询许多表上的数据,一对一的关系以及对它们两者的一些属性的投影。例如:

      class Person {
    int id
    字符串名称
    字符串地址
    static hasMany = [cars:car]
    }

    class Car {
    int id
    字符串品牌
    长价
    Person owner
    static belongsTo = [owner:Person]
    }

    我只用一个查询withCriteria(应用投影)来获取指定汽车的信息(包括品牌,价格和车主名称)?是否可以使用:

      Car.withCriteria {
    预测{
    property(brand))
    property(price)
    property(owner.name)
    }
    eq(id,carId)
    }


  2. 我可以使用投影来获取一个指定人员的信息以及他所有车辆的名称吗?例如:[01,Perter,01 Street A,[Mercedes,Toyota,Ducatti]]?特殊情况:(用上面的Person类)

    一个人可以加入多个组织,一个组织可以有一个父级 组织(反之亦然,一个组织可以有许多其他的依赖组织)。但有一条规则:一个人只能加入一个特定组织的一个儿童组织。所以对于一个给定的组织O和一个人P,获取P的信息的最快方式是什么,以及以P作为成员的O组织的名称。我更喜欢使用Grails投影。


    以下是数据模型:

      class Person {
    int id
    字符串名称
    字符串地址
    static hasMany = [joinedOrgs:Organization]
    }

    class组织{
    int id
    字符串名称
    组织parentOrg
    static hasMany = [成员:Person,childOrgs:组织]
    }


我是Grails的新手,我想更多地了解GORM。 (1)和(2),我不知道是否有一个用一种标准查询方式来完成你想要的任务,但另一种方式似乎更简单和自然。对于(1):

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



<$ p $
$ / code>

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

关于(3),这是一种设计问题。您目前的域名设计并未反映您所描述的规则,因此很难保持这种约束。您可以考虑映射PersonOrganization表和将childrenOrganization设置为临时属性。例如:

  class组织{
int id
字符串名称
组织父级
静态瞬变= ['儿童']
...

布尔儿童(){
def children = Organization.createCriteria()。list(){
eq('parent',this)
}
返回子女
}
}

之后,您可以使用跟踪返回功能(如getAllAncestors())来确定组织的所有父级层次结构,在人员 - 组织列表中查找它。这似乎不是最好的方式,但这是一种可能的方式。


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

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

  2. 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]]?

  3. 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.

解决方案

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

For (2)

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

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
    }
}

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天全站免登陆