2014-01-29 34 views
0

的Grails版本上類[]的方法:2.3.4的Grails java.lang.IllegalStateException的:Grails應用程序之外使用

Hibernate插件:運行時 「:休眠:3.6.10.6」

線BootStrap.groovy中產生錯誤:

def adminRole = new Role(authority: 'ROLE_USER').save(flush: true)

事實上任何在任何類保存操作(控制器,BootStrap.groovy中)導致該錯誤。

但是,當我得到在另一臺計算機中創建的域類時,工作正常,沒有錯誤。

有什麼建議嗎?

謝謝。

完整堆棧跟蹤:

ERROR context.GrailsContextLoader Error initializing the application: Method on class [com.hib.Role] was used outside of a Grails application. If running in the context of a test using the mocking API or bootstrap Grails correctly. 
java.lang.IllegalStateException: Method on class [com.hib.Role] was used outside of a Grails application. If running in the context of a test using the mocking API or bootstrap Grails correctly. 
    at BootStrap$_closure1.doCall(BootStrap.groovy:9) 
    at grails.util.Environment.evaluateEnvironmentSpecificBlock(Environment.java:308) 
    at grails.util.Environment.executeForEnvironment(Environment.java:301) 
    at grails.util.Environment.executeForCurrentEnvironment(Environment.java:277) 
    at java.util.concurrent.FutureTask.run(FutureTask.java:262) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) 
    at java.lang.Thread.run(Thread.java:744) 

我不使用maven或嘲諷。

Botstrap.groovy

class BootStrap { 

     def init = { servletContext -> 

     def adminRole = new Role(authority: 'ROLE_ADMIN').save(flush: true) 

    } 
      def destroy = { 
     } 
    } 
+0

你可以發佈你的'Bootstrap.groovy'嗎?聽起來你正在運行'init'關閉之外的一個閉包 –

+0

我添加了Bootstrap.groovy。相關代碼位於init關閉內部。 – atahan

+0

我認爲它與Windows 8有關。因爲在Mac OS X和Windows 7中沒有錯誤。 – atahan

回答

0

我不得不使用Grails 2.3.6和Hibernate 3.6.10.8一個非常類似的錯誤。在運行集成測試時嘗試在GORM對象上執行操作時出現。 最後,當我沒有在datasource.groovy的'environments'的'test'部分創建數據源時,我在GORM域類的映射塊中設置了一個數據源。排序和它的工作。

+2

你能解釋一下嗎?這個問題僅在項目從2.2.1升級到2.3.9後纔出現? – user1859465

1

@ user3414639的提示幫助我。在我的情況下,代碼由幾個可以引入GORM對象的通用模塊的項目組成。當一個GORM對象上的PostInsert事件觸發了不同的GORM對象的保存(第二個GORM對象被映射到新的數據源)時,我看到了這個錯誤。

將新連接添加到項目的項目datasource.groovy的測試部分爲我解決了錯誤。我將繼續深入研究配置(不知道爲什麼GORM對象#1沒有失敗),但至少這讓我走上了正確的軌道。

dataSource { 
} 
environments { 
    test { 
     dataSource_missing { 
      driverClassName = "com.mysql.jdbc.Driver" 
      url = "jdbc:mysql://localhost/testdomain?useUnicode=yes&characterEncoding=UTF-8" 
      username = "" 
      password = "" 
     } 
    } 
} 
相關問題