运行grails 1.3.7时休眠异常'未为参数2指定值'

Gen*_*sky 5 grails hibernate

我的应用程序遇到了一个奇怪的问题。该应用程序是使用HSQLDB开发和测试的,并且运行良好。当我构建一个WAR文件并将其部署在服务器上时,一小段代码(当然对应用程序来说至关重要)失败了。

代码

def assessment = Assessment.findByPostingAndAssessor(posting, judge)
Run Code Online (Sandbox Code Playgroud)

错误:

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)
Run Code Online (Sandbox Code Playgroud)

我在Google上环顾四周,发现这则旧博客文章

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

这暗示了这个问题,并说这是由错误的休眠版本引起的。但这才三岁。我现在使用带有休眠插件1.3.7的grails 1.3.7。此外,我不知道在哪里可以找到博客文章描述的Maven规范,以便可以对其进行编辑。

我的其他应用程序都没有出现此问题。我尝试使用上述查找程序,并尝试手动创建条件查询,但两者均产生相同的错误。传递给该方法的值都不为null。

我真的真的需要这个才能尽快工作,所以我想知道是否有人对下一步的尝试有任何想法。

作为记录,您可以在此处查看完整的堆栈跟踪:http : //grails.1312388.n4.nabble.com/file/n3787337/hibernate-exception.txt

谢谢,

基因

编辑于9/2/2011:

这是导致错误的休眠/ 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
Run Code Online (Sandbox Code Playgroud)

堆栈跟踪如下。

编辑9/3/2011:

以下是涉及的类:

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
}
}
Run Code Online (Sandbox Code Playgroud)

您是否认为Searcher和之间的层次关系User是问题的一部分?

再次强调一下,当我将其作为带有内存数据库的运行应用程序运行时,此代码可以工作。

编辑9/3/2011:

这是一个说明问题的测试案例。运行良好grails run-app(在内存中),但在使用“ grails prod run-app”(带有MySQL数据库)时失败。

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

编辑日期:2011年9月4日:我也曾在Grails Nabble小组中问过这个问题,而Daniel Henrique Alves Lima指出了我的代码架构中的潜在问题。(请参阅在Nabble上的帖子)。问题似乎是我已经编码:

    static belongsTo = [posting: Posting, assessor: Searcher]
Run Code Online (Sandbox Code Playgroud)

在我的Assessment课堂上,导致了上述行为。当我将声明更改为

    static belongsTo = [assessor: Searcher]
Run Code Online (Sandbox Code Playgroud)

findBy..()代码正常工作。我仍然认为代码中隐藏着一个休眠错误(因为遗漏参数不是报告错误的好方法:-)),但是至少我的程序现在可以保存数据!

感谢为解决方案做出贡献的每个人!

Bur*_*ith 2

我认为这是因为 or 之一postingjudge空。如果情况并非如此,您可以尝试打开 SQL 日志记录,尽管我不确定这种情况是发生在引发异常的代码之前还是之后。

将其添加到 Config.groovy 中的 log4j 部分将记录 SQL:

debug 'org.hibernate.SQL'
Run Code Online (Sandbox Code Playgroud)

这将记录绑定参数:

trace 'org.hibernate.type'
Run Code Online (Sandbox Code Playgroud)

编辑 9/4:Posting我在调试器中花费了一些时间来处理您的测试代码,这是由于和之间的一对一映射所致Assessment。一对一使用与所有者的外键相同的主键值,因此,PreparedStatement 填充代码中的逻辑会跳过设置第一个参数的值的调用,并且不会增加位置计数器,因此设置的一个值是第一个插槽中的第二个参数的值,而第二个插槽中没有任何值。从 log4j 输出中可以看到,HSQLDB 也做了同样的事情,但显然没有那么严格。请注意,HSQLDB 只是巧合地工作,因为您只创建每个实例的一个实例,因此它们共享相同的 id 值 - 如果您首先创建一种类型的一些虚拟实例来强制不匹配,您将得到错误的结果(但不是例外)。我的建议是将其重新设计为一对多,如果需要,可以添加一个约束,将集合的大小限制为最多一个,例如children(size:0..1)- 请参阅http://grails.org/doc/latest/参考/约束/size.html