grails - 使用联合表获取查询中的列

时间:2017-05-11 02:03:21

标签: grails groovy criteria

我的设置标准如下:

std::unique_ptr

我认为它将返回二维列表。如上所述here。根据我的理解,第一个列表是所选项目的列表,第二个列表是项目列的列表。我的问题是

如何获取所有选定的项目?例如:

            def e = Equipment.createCriteria()
            def equipmentInstanceList = e.list {
                createAlias "rentals", "r", org.hibernate.sql.JoinType.LEFT_OUTER_JOIN
                projections {
                    property('name')
                    property('r.status')
                    property('r.dateRented')
                    property('r.dateReturned')
                }
            }

如何访问第二个列表中的特定列?例如:

def list = equipmentInstanceList.[all] //supposed to get all the selected items
def a =  equipmentInstanceList[1] //will only get the 1st item in 1st list

修改

此查询与上述条件的结果相同。

def a = equipmentInstanceList[1].status //supposed to get the status column in 2nd list

我仍在尝试获取def equipmentForRent = Equipment.executeQuery("SELECT e.name, r.status, r.dateRented, r.dateReturned FROM ers.Equipment e LEFT JOIN e.rentals r") 中的列。到目前为止我在尝试什么。

equipmentForRent

仍然出现此错误:

each.equipmentForRent { e -> println e.dateReturned }

为什么它与此查询不同:

Exception evaluating property 'dateRented' for java.util.Arrays$ArrayList, Reason: groovy.lang.MissingPropertyException: No such property: dateRented for class: java.lang.String

任何?

1 个答案:

答案 0 :(得分:1)

您在一个问题中提出了两个问题: 很快:

如何获取所有选定的项目?例如:

如何访问第二个列表中的特定列?例如:

def a = equipmentInstanceList[1].status //supposed to get the status column in 2nd list

获得第二名:

def a = equipmentInstanceList[2].status // this now points to 2nd element status

但这些都不是安全的,可能导致问题。 迭代具有索引的元素:

equipmentInstanceList?.eachWithIndex{  e,i->
  println "-- $i is index $e is element"
 }

您的查询已尝试在HQL中执行sql查询

def equipmentForRent = Equipment.executeQuery("SELECT e.name, r.status, r.dateRented, r.dateReturned FROM ers.Equipment e LEFT JOIN e.rentals r")

尝试从表格中选择新的地图(项目),如图所示

def equipmentForRent = Equipment.executeQuery("SELECT new map(e.name as name, r.status as status, r.dateRented as dateRented, r.dateReturned as dateReturned) FROM Equipment e LEFT JOIN e.rentals r")