ฉันมีชั้นเรียนเช่นนี้:
class Foo {
String bar
}
ฉันกำลังพยายามรับ ID ของวัตถุ Foo
ทั้งหมดที่มี bar
String อยู่ในรายการ bars
ฉันได้ลองหลายวิธีแล้ว แต่ได้รับข้อผิดพลาดเดิมเสมอ:
java.lang.String cannot be cast to java.util.Collection
บางสิ่งที่ฉันได้ลอง:
def ids = Foo.findAllByBarInList( bars )*.id
def ids = Foo.findAllByBarInList( bars ).collect{ it.id }
def ids = Foo.findAllByBarInList( bars ).collect{ it -> it?.id }
อัปเดต:
ฉันกำลังสร้าง bars
ด้วย แยก ดังนั้นมันจึงเป็นอาร์เรย์ ไม่ใช่รายการ มันทำให้ฉันผิดหวังเพราะ Foo.findAllByBarInList( bars )
ส่งคืนออบเจ็กต์ Foo
ของฉันได้ดี แต่เมื่อฉันพยายามรวบรวมรหัสเท่านั้นจึงจะล้มเหลว ตอนนี้ฉันสร้าง bars
ด้วย tokenize แทน และทุกอย่างก็เรียบร้อยดี
collect
และตัวดำเนินการสเปรด*.
นั้นมีพื้นฐานมาจาก Groovy - person ubiquibacon   schedule 17.07.2012def ids = Foo.findAllByBarInList( bars )*.id
เหมาะกับฉัน - person Kelly   schedule 17.07.2012