代码之家  ›  专栏  ›  技术社区  ›  John

Grails在命名查询中使用继承的属性

  •  0
  • John  · 技术社区  · 10 年前

    我希望在派生实例的命名查询中使用继承属性,以便可以提供自定义的结果排序。我收到一个错误。

    package com.example
    
    class Ticket {
        // User is defined elsewhere
        static belongsTo = [user : User]
        String idNumber
    }
    
    class SeasonTicket extends Ticket {
    
        // some custom properties go here
    
        static namedQueries = {
            locateOrderedSeasonTicketsByUser { user ->
                // all derived instances are in the same table, hence return only the correct derived instances
                eq("class", "com.example.SeasonTicket")
    
                // return a match on user
                user {
                    idEq(user.id)
                }
    
                // implement custom order
                order "customProperty"
            }
        } << Ticket.namedQueries
    }
    

    最后一行允许使用基类中定义的任何继承的命名查询。

    运行调用的集成测试时,我遇到以下错误:

    SeasonTicket.locateOrderedSeasonTicketsByUser(someUserInstance)
    

    方法com.example.User.call()的签名不适用于 参数类型: (com.example.SseasonTicket$__clinit__closure2_closure3_closure4) 值: [com.example.SseasonTicket$__clinit__closure2_closure3_closure4@31ee7d7a] 可能的解决方案:wait(),last(),save(),any(),getAll(), 等待(长时间)

    集成测试是我第一次尝试简单测试:

    void "SeasonTicket.locateOrderedSeasonTicketsByUser finds an object"() {
        given:
        def seasonTicket = new SeasonTicket()
        def user = new User()       
        user.addToSeasonTickets(seasonTicket)
        user.save(flush: true, failOnError: true)
    
        expect: "we can find one season ticket"
        SeasonTicket.locateOrderedSeasonTicketsByUser(user).list().size() == 1
    }
    

    看起来基类中的用户字段没有被识别。请问我做错了什么?

    1 回复  |  直到 10 年前
        1
  •  0
  •   aiolos    10 年前

    本地参数之间存在命名冲突 user 以及类属性“user”。

    这部分标准

    user {
        idEq(user.id)
    }
    

    将尝试调用本地参数上的方法 使用者 而不是使用 使用者 的属性 Ticket .

    重命名namedQuery的参数 locateOrderedSeasonTicketsByUser .