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

grails - get column in query with joint table

我设置了这样的条件:

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

我认为它将 return 二维列表。如前所述 here。据我了解,第一个列表是所选项目的列表,第二个列表是项目列的列表。我的问题是

如何获得所有选中的项目?示例:

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

为什么与这个查询不同:

def rentalTest = Rental.executeQuery("FROM Rental")
println rentalTest.status //no error, returns the status column from rental

有人吗?

您提出了两个问题: 很快:

如何获得所有 selected 物品?示例:

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

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

但其中 none 是 null 安全的,可能会导致问题。 要遍历具有索引的元素:

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

您尝试过的查询 sql HQL 中的查询

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

尝试从 table 包装 select 新地图(项目),如图所示

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