2011-09-03 60 views
4

我遇到了一個與我的應用程序有關的奇怪問題。該應用程序是用HSQLDB開發和測試的,並且運行良好。當我構建一個WAR文件&將其部署到服務器上時,一個特定位代碼(當然對應用程序至關重要)失敗。運行grails時Hibernate異常'沒有爲參數2指定值'1.3.7

代碼

def assessment = Assessment.findByPostingAndAssessor(posting, judge) 

錯誤:

Caused by: java.sql.SQLException: No value specified for parameter 2 
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055) 
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956) 
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926) 
    at com.mysql.jdbc.PreparedStatement.fillSendPacket(PreparedStatement.java:2214) 
    at com.mysql.jdbc.PreparedStatement.fillSendPacket(PreparedStatement.java:2138) 
    at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1853) 
    at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:208) 
    at org.hibernate.loader.Loader.getResultSet(Loader.java:1808) 
    at org.hibernate.loader.Loader.doQuery(Loader.java:697) 
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259) 
    at org.hibernate.loader.Loader.doList(Loader.java:2228) 

我看了看周圍的谷歌,發現這個相當老的博客文章

http://blog.flurdy.com/2008/09/no-value-specified-for-parameter-when.html

其中提到了這個問題並說這是由一個不好的休眠版本引起的。但那已經三歲了。我現在在hibernate插件1.3.7中使用grails 1.3.7。此外,我找不到在哪裏可以找到博客文章描述的maven規範,以便我可以編輯它們。

而我的其他應用程序都沒有表現出這個問題。我嘗試使用上面的finder,並嘗試手動創建一個標準查詢,但都產生了相同的錯誤。傳遞給該方法的值都不爲null。

我真的很需要這個儘快工作,所以我想知道如果任何人有任何想法接下來要嘗試什麼。

根據記錄,你可以看到完整的堆棧跟蹤這裏:http://grails.1312388.n4.nabble.com/file/n3787337/hibernate-exception.txt

感謝,

基因

EDITED 2011年9月2日:

這裏是休眠/ SQL導致出錯的日誌:

11/09/02 17:56:15 DEBUG hibernate.SQL: select this_.id as id22_0_, this_.version as version22_0_, this_.assessor_id as assessor3_22_0_, this_.comment as comment22_0_, this_.date_created as date5_22_0_, this_.last_updated as last6_22_0_, this_.value as value22_0_ from assessment this_ where this_.id=? and this_.assessor_id=? 
11/09/02 17:56:15 TRACE type.LongType: binding '2' to parameter: 1 
11/09/02 17:56:15 ERROR util.JDBCExceptionReporter: No value specified for parameter 2 
11/09/02 17:56:15 ERROR docusearch.UiController: Could not save assessment 
org.hibernate.exception.SQLGrammarException: could not execute query 

st ack跟蹤如下。

EDITED 2011/9/3:

這裏是所涉及的類:

package com.fxpal.docusearch 

import com.sun.org.apache.xalan.internal.xsltc.cmdline.getopt.IllegalArgumentException; 

class Assessment { 
    Posting posting 
    AssessmentValue value 
    Searcher assessor 

    static belongsTo = [posting: Posting, assessor: Searcher] 

    static indexes = { 
    posting() 
    assessor() 
    } 

    static constraints = { 
     posting(nullable: false) 
     value() 
     assessor(nullable: true) 
    } 

    static judge(Posting posting, Searcher judge, String value) { 
     if (judge == null) 
      throw new IllegalArgumentException("missing judge value"); 
     // error occurs here 
     def assessment = Assessment.findByPostingAndAssessor(posting, judge) 
     def judgment = AssessmentValue.fromString(value) 
     if (judgment && !assessment) 
      assessment = new Assessment(posting: posting, assessor: judge) 
     if (!judgment && assessment) { 
      assessment.delete(flush:true) 
      assessment = null 
     } 
     else { 
      assessment.value = judgment 
      assessment.save(flush:true) 
     } 
     return assessment 
    } 
} 


package com.fxpal.docusearch 

class Posting { 
    Document document 
    int rank 
    double score 
    Assessment assessment 

    static mapping = { 
     cache true 
    } 

    static belongsTo = [document: Document, query: Query] 
    static constraints = { 
     document(nullable: false) 
     rank() 
     score() 
     assessment(nullable: true) 
    } 
} 


package com.fxpal.docusearch 

import com.fxpal.authentication.User 

class Searcher Extends User { 
    String name 
    String email 
    String color 

    static constraints = { 
      name(blank:false, unique: true, maxSize:255) 
     email(nullable: true) 
     color(blank: false) 
    } 

    static mapping = { 
     tablePerHierarchy true 
    } 

    public String displayName() { 
     return name ?: username 
    } 
} 


package com.fxpal.authentication 

// This is the generic class generated by the Spring Security plugin 
class User { 

    String username 
    String password = 'n/a' 
    boolean enabled = true 
    boolean accountExpired = false 
    boolean accountLocked = false 
    boolean passwordExpired = false 

    static constraints = { 
     username blank: false, unique: true 
     password blank: false 
    } 

    static mapping = { 
     password column: '`password`' 
    } 

    Set<Role> getAuthorities() { 
    UserRole.findAllByUser(this).collect { it.role } as Set 
} 
} 

你以爲SearcherUser之間的層次關係是問題的一部分?

同樣,我應該強調,當我將它作爲運行應用程序與內存數據庫運行時,此代碼有效。

EDITED 2011/9/3:

這裏是說明該問題的測試用例。以grails run-app(在內存中)運行正常,但在使用'grails prod run-app`(使用MySQL數據庫)時會失敗。

http://grails.1312388.n4.nabble.com/file/n3788449/test.zip

EDITED 2011年9月4日: 我問過的Grails的Nabble組這個問題爲好,丹尼爾·阿爾維斯恩裏克利馬發現了一個潛在的問題與我的代碼的架構。(見Nabble上的his post)。這個問題似乎是,我已經編碼:

static belongsTo = [posting: Posting, assessor: Searcher] 

在我Assessment類,這就造成了上述行爲。當我改變了聲明

static belongsTo = [assessor: Searcher] 

findBy..()代碼工作正常。我仍然認爲代碼中潛伏着一個hibernate bug(因爲忽略參數不是報告錯誤的好方法:-)),但至少我的程序現在保存了數據!

感謝大家對解決方案的貢獻!

回答

1

我認爲這是因爲postingjudge之一爲空。如果情況並非如此,可以嘗試打開SQL日誌記錄,但我不確定在拋出異常的代碼之前或之後是否發生這種情況。

添加這Config.groovy中log4j的部分將記錄SQL:

debug 'org.hibernate.SQL' 

,這將記錄綁定參數:

trace 'org.hibernate.type' 

編輯9/4:我花在調試器中進行一段時間的測試代碼,這是由於PostingAssessment之間的一對一映射。一對一使用主鍵的相同值作爲所有者的外鍵,因此PreparedStatement總體代碼中的邏輯會跳過調用以設置第1個參數的值並且不會增加位置計數器,所以設置的一個值是第一個槽中的第二個arg值,第二個槽中沒有任何值。您可以從log4j輸出中看到HSQLDB執行相同的操作,但顯然不那麼嚴格。請注意,HSQLDB只能巧合使用,因爲您只創建每個實例的一個實例,因此它們共享相同的id值 - 如果先創建一個類型的虛擬實例以強制不匹配,則會得到錯誤的結果(但不是例外)。我的建議是將其重新修改爲一對多,如果需要,您可以添加一個約束,以將集合的大小限制爲最多一個,例如, children(size:0..1) - 請參閱http://grails.org/doc/latest/ref/Constraints/size.html

+0

謝謝伯特。這兩個值都是非空的;我編輯了上面的帖子以包含相關的日誌數據。 –

+0

你可以發佈精簡版的域類嗎? –

+0

添加了類 –

0

在嘗試通過另一個對象查找對象時,與grails 2.3.11有同樣的錯誤,即必填字段(belongsTO已就位)。

相關問題