gpt4 book ai didi

java - 使用一对多关系时 Grails withCriteria 异常

转载 作者:行者123 更新时间:2023-12-02 08:30:23 24 4
gpt4 key购买 nike

我有 2 个域:

class JobProcess {

static constraints = {
scriptUser(blank:false)
scriptType()
scriptName()
scriptPath()
scriptArgs(size:0..1000,nullable:true)
scriptDateCreated(nullable:true)
scriptDateStarted(nullable:true)
scriptDateFinished(nullable:true)
scriptRun()
scriptId()
}

static mapping = {
version false
}
User scriptUser
String scriptType
String scriptName
String scriptPath
String scriptArgs
Date scriptDateCreated
Date scriptDateStarted
Date scriptDateFinished
String scriptRun
int scriptId
}

和:

class User {

static hasMany = [ jobs : JobProcess ]

static constraints = {
login(unique: true, blank: false)
password(password: true, blank: false)
email(blank: false, emailAddr: true, maxSize: 50)
firstName(blank:false, maxSize:32)
lastName(blank:false, maxSize:32)
phoneNo(nullable: true)
}

static mapping = {
sort lastName: 'asc'
}

String login
String firstName
String lastName
String phoneNo
String email
String password

boolean locked = false

String toString()
{
if (firstName == "")
return email
return fullName()
}

String fullName()
{
return firstName + " " + lastName
}
}

这个查询:

selected = JobProcess.withCriteria{
like("scriptUser", "%${params.name}%")
maxResults(params.max as int)
firstResult(params.offset? params.offset.toInteger():0)
}

我收到错误:

ERROR property.BasicPropertyAccessor  - IllegalArgumentException in class: common.User, getter method of property: id      
ERROR errors.GrailsExceptionResolver - java.lang.ClassCastException@503ca729

我还尝试将类似部分替换为:
eq("scriptUser", 1) 但我得到了同样的错误。

怎么了?有什么想法吗?

最佳答案

如果 params.nameUserfirstName,请尝试以下操作:

JobProcess.withCriteria {
scriptUser {
like('firstName', "%${params.name}%")
}
maxResults(params.max as int)
firstResult(params.offset ? params.offset.toInteger() : 0)
}

如果 params.name 有所不同,您能否澄清您的问题以解释您如何使用它?

这是概念验证代码:

// grails-app/domain/JobProcess.groovy
class JobProcess {
User scriptUser
}

// grails-app/domain/User.groovy
class User {
String name
}

// grails-app/conf/Bootstrap.groovy
def init = { servletContext ->
def foo = new User(name: 'foo').save()
def bar = new User(name: 'bar').save()
new JobProcess(scriptUser: foo).save()
new JobProcess(scriptUser: bar).save()
new JobProcess(scriptUser: bar).save()

def result = JobProcess.withCriteria {
scriptUser {
like('name', 'ba%')
}
}
result.each {
println "Result ID: ${it.id}"
}

// prints:
// Result ID: 2
// Result ID: 3
}

这是基于“查询关联”部分 here .

关于java - 使用一对多关系时 Grails withCriteria 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3558527/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com