2017-07-20 20 views
0

在我的項目中我正在使用兩個數據庫,並且兩個數據庫中的列名都不相同。所以我在配置文件中定義了一個標誌,並在域中注入了一個依賴項。我可以在groovy域注入依賴項嗎?

例如: - >

class MRAffiliate { 

    transient def grailsApplication; 

    String companyName; 

    String annotations; 

    static mapping = {   
     table name: "affiliati"//, schema: "public" 
     id generator:'sequence', params:[sequence:'affiliati_seq'] 
     id column: "id"//, sqlType: "int4"; 
     if (grailsApplication.config.com.dogmasystems.postgres==true){ 
      companyName column: "ragione_sociale"; 
     } else { 
      companyName column: "ragione_sociale", sqlType: "string"; 
     } 
     if (grailsApplication.config.com.dogmasystems.postgres==true){ 
      annotations column: "annotazioni"; 
     } else { 
      annotations column: "annotazioni", sqlType: "string"; 
     } 
     version false; 
    } 
} 

是否有任何其他方式根據數據庫

,我發現了錯誤定義的列名,而執行此代碼。 的錯誤是, 「錯誤評估ORM映射阻塞域MRAffiliate沒有這樣的屬性:grailsApplication類:org.codehaus.groovy.grails.orm.hibernate.cfg.HibernateMappingBuilder」

+0

@Szymon哪個grails版本是這樣的? –

+0

grails版本:2.4.4 –

+0

您正嘗試從靜態上下文('mapping' closure')中訪問實例變量('grailsApplication')。您無法從靜態上下文訪問任何實例狀態。該語言不允許使用它,因爲它不合情理。 –

回答

2

嘗試Holders.grailsApplication.config.xxx - 見this domain class這使用映射塊中的配置。

+0

謝謝,它正在工作 –

+0

好知道它爲你工作..請考慮接受答案:) –