Grails在抽象域上创建标准 [英] Grails createCriteria on abstract domain

查看:85
本文介绍了Grails在抽象域上创建标准的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我们假设我们有以下类:

$ <$>我很好奇如何使用条件构建器来访问继承类的字段。 b
$ b

  class A {
String fieldOne
String fieldTwo

static hasMany = [childs:AbstractChildClass]

}

和抽象子类看起来像这样: p>

 抽象类AbstractChildClass {

整数值1
整数值2
A

静态映射
tablePerHierarchy false
}
}

当然有几个扩展类,比如:

  class ExtendingClassOne extends AbstractChildClass {

DesiredObject desiredObject

static mapping = {
tablePerHierarchy false
}

}

我们还假设有DesiredO类bject看起来像这样:

  class DesiredObject {
String infoA
String infoB
}

问题在于如何通过创建A类标准来获取字段infoA和infoB。My到目前为止,这是:

  A.createCriteria()。list {
childs {
desiredObject {
ilike('infoA','%something%')
}
}
}

这当然不起作用,因为在表中,ExtendingClassOne只是desiredObject的id,我不知道如何将对象与条件构建器连接以获取其字段。



感谢您阅读。

Marco

解决方案

不要期望域模型与其相关的数据库模式100%匹配。

在你的情况中,多态性只能用于AbstractChildClass的 tablePerHierarchy true



如果你想坚持使用 tablePerHierarchy false ,你可以像下面这样定义你的类:

  class A {

static hasMany = [childrenOne:ExtendingClassOne,childrenTwo:ExtendingClassTwo]

}

因此您的查询将如下所示:

  A.withCriteria {
(['childrenOne','childrenTwo'])中的字符串c){
$ c{
desiredObject {
ilike('infoA','%something%')
}
}
}
}


I am quite curious how one would use the criteria builder to access fields of an inherited class.

Let's assume we have the following class:

class A { 
  String fieldOne 
  String fieldTwo

  static hasMany = [childs: AbstractChildClass]

}

and the abstract child class would look like this:

abstract class AbstractChildClass {

  Integer valueOne
  Integer valueTwo
  A a

  static mapping
      tablePerHierarchy false
  }
}

of course there are several extending classes such as:

class ExtendingClassOne extends AbstractChildClass {

    DesiredObject desiredObject

    static mapping = {
        tablePerHierarchy false
    }

}

let's also assume there is the class DesiredObject which looks like this:

class DesiredObject {
    String infoA
    String infoB
}

The question is how one would get the fields infoA and infoB by creating a criteria for class A. My approach so far is this:

A.createCriteria().list {
    childs {
        desiredObject {
            ilike('infoA', '%something%')
        }
    }
}

This of course does not work because in the table ExtendingClassOne is only the id of the desiredObject and I have no clue how to join the object with the criteria builder to get its fields.

Thank you for reading.

Marco

解决方案

don't expect 100% match between your domain model and the DB schema it's related to.

In your case the polymorphism would work only with tablePerHierarchy true for AbstractChildClass.

If you do want to stick with tablePerHierarchy false, you can define your class like:

class A { 

  static hasMany = [ childrenOne:ExtendingClassOne, childrenTwo:ExtendingClassTwo ]

}

thus your query would be as simple as:

A.withCriteria{
  for( String c in [ 'childrenOne', 'childrenTwo' ] ){
    "$c"{
      desiredObject {
        ilike('infoA', '%something%')
      }
    }
  }
}

这篇关于Grails在抽象域上创建标准的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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