连接表上的 Grails Criteria 投影

发布于 2024-11-05 02:09:56 字数 260 浏览 0 评论 0原文

我对 grails 标准构建器有疑问,我想对与父表示例具有一对多关系的表上的列进行投影:

Car.createCriteria() { 
   projections { 
     property('name') 
     property('wheels.name')// ???? 
   }

   join 'wheels' 
   //or wheels {} ???
}

或者是否存在类似的情况?我认为这是别名的基本问题

I have an issue with grails criteria builder, I want to do a projection on a column that is on a table that is in one-to-many relation to parent table example:

Car.createCriteria() { 
   projections { 
     property('name') 
     property('wheels.name')// ???? 
   }

   join 'wheels' 
   //or wheels {} ???
}

or something similar exist? I think it is basic propblem with aliases

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

星軌x 2024-11-12 02:10:02

我假设有以下域类:

class Car {
  String name
  static  hasMany = [wheels : Wheel]
}

class Wheel {
  String name
  static belongsTo = [car : Car]
}

我还假设这是所需的输出:

CarName WheelName
Car1    Wheel1
Car1    Wheel2
Car2    Wheel3

在这种情况下,您将这样做:

void testCarProjectionItg() {
  def car1 = new Car(name: 'Car1').save()
  def car2 = new Car(name: 'Car2').save()

  def wheel1 = new Wheel(name: 'Wheel1')
  def wheel2 = new Wheel(name: 'Wheel2')
  def wheel3 = new Wheel(name: 'Wheel3')

  car1.addToWheels wheel1
  car1.addToWheels wheel2     
  car2.addToWheels wheel3
  wheel1.save()
  wheel2.save()
  wheel3.save()
  car1.save()
  car2.save()

  println Wheel.withCriteria {
    projections {
      property('name')
        car {
          property('name')
        }
    }       
  }
}

--Output from testCarProjectionItg--
[[Wheel1, Car1], [Wheel2, Car1], [Wheel3, Car2]]

在这种情况下,我更喜欢 HQL 查询:

println Wheel.executeQuery("select car.name, wheel.name from Car car inner join car.wheels wheel")
--Output from testCarProjectionItg--
[[Car1, Wheel1], [Car1, Wheel2], [Car2, Wheel3]]

I am assuming the following domain classes:

class Car {
  String name
  static  hasMany = [wheels : Wheel]
}

class Wheel {
  String name
  static belongsTo = [car : Car]
}

I also assume that this is the desired output:

CarName WheelName
Car1    Wheel1
Car1    Wheel2
Car2    Wheel3

In this case you would do this:

void testCarProjectionItg() {
  def car1 = new Car(name: 'Car1').save()
  def car2 = new Car(name: 'Car2').save()

  def wheel1 = new Wheel(name: 'Wheel1')
  def wheel2 = new Wheel(name: 'Wheel2')
  def wheel3 = new Wheel(name: 'Wheel3')

  car1.addToWheels wheel1
  car1.addToWheels wheel2     
  car2.addToWheels wheel3
  wheel1.save()
  wheel2.save()
  wheel3.save()
  car1.save()
  car2.save()

  println Wheel.withCriteria {
    projections {
      property('name')
        car {
          property('name')
        }
    }       
  }
}

--Output from testCarProjectionItg--
[[Wheel1, Car1], [Wheel2, Car1], [Wheel3, Car2]]

I would prefer a HQL query in this case:

println Wheel.executeQuery("select car.name, wheel.name from Car car inner join car.wheels wheel")
--Output from testCarProjectionItg--
[[Car1, Wheel1], [Car1, Wheel2], [Car2, Wheel3]]
猫九 2024-11-12 02:10:02

只做怎么样:

Car.createCriteria().list() { 
    createAlias("wheels","wheelsAlias")
    projections { 
        property('name') 
        property('wheelsAlias.name')
    }
}

或者我错过了什么?

what about doing only:

Car.createCriteria().list() { 
    createAlias("wheels","wheelsAlias")
    projections { 
        property('name') 
        property('wheelsAlias.name')
    }
}

Or am I missing something?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文