【问题标题】:Grails querying model containing an enum set包含枚举集的 Grails 查询模型
【发布时间】:2014-09-09 11:59:34
【问题描述】:

我的域类是

class RoomWantedAd{
    Set<MateAgeRange> mateAgeRanges
    static hasMany=[mateAgeRanges :MateAgeRange]
}

她的伴侣年龄范围是:

enum MateAgeRange {
    TWENTIES('18-29')
    ,THIRTIES('30-39')
    ,FOURTIES("40-49")
    ,FIFTIES("50-59")
    ,SIXTIES("60+")



final String value

private MateAgeRange(String value) {
    this.value = value
}

String toString() { value }
String getKey() { name() }


static belongsTo=[roomWanted:RoomWanted]
}

我的问题是搜索。在搜索页面中,一个人可以在 [18-29, 30-39, 40-49, 50-59, 60+] 中选择 0 个或多个值。在数据库中,[18-29, 30-39, 40-49, 50-59, 60+] 中的 0 个或多个值存储在字段“mateAgeRanges”中。 让 db 在 'mateAgeRange' 字段中包含 [30-39, 50-59]。让在搜索页面中,用户选择 [18-29, 50-59, 60+]。那么必须返回上面列表对应的Ad。这是因为用户选择中的至少一个值存在于 db 列表中。这怎么可能。是否可以使用 SQL 查询或 grails GORM 查询。

【问题讨论】:

    标签: grails model enums querying


    【解决方案1】:

    您必须使用与创建/更新完全相同的&lt;g:select/&gt; 标签。因此,您将在浏览器中看到诸如 THIRTIES 之类的人类可读值,但在后台将使用 '30-39' 值。

    【讨论】:

    • 搜索页面 UI 没问题。 UI 和值工作正常。我的问题是关于查询。这个问题是怎么查询的。
    • 不明白。您根据从表单中获得的数据进行查询。你还需要什么?
    • 我其实很想知道这个问题对应的sql查询
    • 在 DataSource.groovy 中激活 sql 日志记录:hibernate { cache.use_second_level_cache = true cache.use_query_cache = false show_sql=true format_sql=true }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-16
    • 1970-01-01
    • 2011-11-05
    • 1970-01-01
    • 2020-09-14
    • 2015-04-20
    • 1970-01-01
    相关资源
    最近更新 更多