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

时间:2010-08-24 16:10:15

标签: java grails groovy gorm

我有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)但我得到同样的错误。

怎么了?有什么想法吗?

2 个答案:

答案 0 :(得分:2)

如果params.nameUser的{​​{1}},请试试这个:

firstName

如果JobProcess.withCriteria { scriptUser { like('firstName', "%${params.name}%") } maxResults(params.max as int) firstResult(params.offset ? params.offset.toInteger() : 0) } 有所不同,您是否可以澄清您的问题以解释您的使用方式?

这是概念证明代码:

params.name

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

答案 1 :(得分:0)

我也可以通过firstName和lastName组合:

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

有没有办法直接使用fullName()?