GORM access list index of hasMany association

♀尐吖头ヾ 提交于 2019-12-06 01:50:30

Using indexColumn in combination with updateable and insertable mappings, you can have collection items (e.g. your Photos) index aware. For example:

class Album {
    static hasMany = [photos: Photo]
    List photos // Generates column `album_idx` in table for Photo.
    Integer size // Memoized.

    static mapping = {
      photos indexColumn: [name: "position", type: Integer]
    }
}

class Photo {
    Integer position

    static belongsTo = [album: Album]
    static hasOne = [content: PhotoData] // PhotoData has byte[] field for data

    static mapping = {
      position updateable: false, insertable: false
    }
}

photo.position will now give you this photo's index (NOTE: by default list order is 0 based)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!