如何在 Persistent Set 的特定索引处获取对象 - Grails

How to get object at specific index in Persistent Set - Grails

我有一组持久对象:

def applicantFiles = applicant.recommendationFiles

如何从持久集合中获取第 i 个元素的对象?我试过

applicantFiles[1] and applicantFiles.getAt(1)

两者都不起作用。

集合无法编入索引,它们是无序的。如果你需要索引一个集合,那么 declare it as a list:

To keep objects in the order which they were added and to be able to reference them by index like an array you can define your collection type as a List:

class Author {

    List books

    static hasMany = [books: Book]
}

理解关联的 table 需要一个列用作索引。否则无法保留顺序。您可以使用 indexColumn 指定要使用的列:

By default when mapping an indexed collection such as a Map or List the index is stored in a column called association_name_idx which is an integer type in the case of lists and a String in the case of maps. You can alter how the index column is mapped using the indexColumn argument:

static mapping = {
    matrix indexColumn: [name: "the_matrix", type: Integer]
}

http://grails.github.io/grails-doc/2.3.x/ref/Database%20Mapping/indexColumn.html

如果您没有声明实例变量的类型,那么 GORM 会将类型默认为 Set。