grails-使用联合表在查询中获取列
我有这样的标准:grails-使用联合表在查询中获取列,grails,groovy,criteria,Grails,Groovy,Criteria,我有这样的标准: def e = Equipment.createCriteria() def equipmentInstanceList = e.list { createAlias "rentals", "r", org.hibernate.sql.JoinType.LEFT_OUTER_JOIN projections { property
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 a = equipmentInstanceList[1].status //supposed to get the status column in 2nd list
如何访问第二个列表中的特定列?例如:
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 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
有人吗?您一次提出了两个问题: 很快: 如何获取所有选定的项目?例如:
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 a = equipmentInstanceList[1].status //supposed to get the status column in 2nd list
如何访问第二个列表中的特定列?例如:
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 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")
您建议的解决方案中的地图键将作为真实列,对吗?对不起,我提出了一个毫无意义的问题。非常感谢您的帮助查询中的
新映射
方法解决了我的问题没有问题我想您的第二条评论意味着您理解它。