【发布时间】:2011-11-09 13:44:39
【问题描述】:
我的应用程序遇到了一个奇怪的问题。该应用程序是使用 HSQLDB 开发和测试的,并且运行良好。当我构建一个 WAR 文件并将其部署在服务器上时,一段特定的代码(当然对应用程序至关重要)失败了。
代码
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)
我在 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
谢谢,
基因
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
堆栈跟踪如下。
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
}
}
您认为Searcher 和User 之间的层级关系是问题的一部分吗?
再次强调,当我将这段代码作为带有内存数据库的运行应用程序运行时,它是有效的。
2011 年 9 月 3 日编辑:
这是一个说明问题的测试用例。像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 上的 his post)。问题似乎是我编码过:
static belongsTo = [posting: Posting, assessor: Searcher]
在我的Assessment 类中,这导致了上述行为。当我将声明更改为
static belongsTo = [assessor: Searcher]
findBy..() 代码工作正常。我仍然认为代码中潜伏着一个休眠错误(因为省略参数不是报告错误的好方法:-)),但至少我的程序现在可以保存数据!
感谢所有为解决方案做出贡献的人!
【问题讨论】: