2015-11-04 22 views
0

我試圖通過用派生字段替換動態字段(一個瞬態getter沒有基礎數據庫表示形式),以便我可以使用,例如,標準來查詢我的模型,以提高性能。原來充滿活力的領域是相當簡單:派生的屬性調用存儲的函數拋出StreamCorruptedException

Client resolveClient() { 
    if (prevCall && prevCall.client) { 
     return prevCall.client 
    } else { 
     return client 
    } 
} 

我不知道如何重現與一個MySQL的聲明,所以我想我會繼續前進,把它貼到存儲函數,定義如下:

CREATE FUNCTION `request_client`(requestId long) RETURNS varchar(255) CHARSET utf8 
begin 
    declare pci long; 
    declare clientId long; 
    declare clientName varchar(255); 

    select request.prev_call_id 
    from request 
    where request.id = requestId 
    into pci; 

    if pci is not null then 
     select call_history.client_id 
     from call_history 
     where call_history.call_id = pci 
     into clientId; 
    else 
     select request.client_id 
     from request 
     where request.id = requestId 
     into clientId; 
    end if; 

    select clients.client_name 
    from clients 
    where clients.client_id = clientId 
    into clientName; 

    return clientName; 

    end; 

然後我把在派生字段功能:

String derivedFieldName 
static mapping = { 
    derivedFieldName formula: '(select stored_function(id))' 
} 

的問題是,現在,當我在d運行任何查詢omain,甚至簡單到Request.list(),我得到以下異常:

類: java.io.StreamCorruptedException
消息:無效流頭:32303135

對於額外的樂趣,這是一個抽象域類。我不知道這是否真的有什麼區別。它仍然像任何其他域一樣被保存到數據庫中,並且我正在調用抽象類本身的查詢,而不是實現。

最令人沮喪的是衍生領域本身確實工作!我可以使用它成功檢索客戶名稱;我只是不能查詢整個域。

最後,我很確信派生的屬性是問題,因爲我已經評論過它,然後可以成功查詢域。

回答

0

如果有人在後面碰到這個問題,其實的抽象類。而不只是它是一個抽象類 - 它是一個抽象的類。顯然,Grails不支持這些屬性的派生屬性。

要將查詢移動到數據庫,我只需將已解析的客戶端保存到我的請求域中:/