Quantcast

Criteria List return: java.lang.ClassCastException@175c0e3

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Criteria List return: java.lang.ClassCastException@175c0e3

didinj
Hi all,

I modified generated controller from domain below:
class Message {
    
    User msgfrom
    User msgto
    String msgbody
    Date msgcreated
    
    static hasMany = [reply:Reply]

    static constraints = {
        msgfrom()
        msgto()
        msgbody(nullable:true, maxSize: 8094)
        msgcreated()
    }
    
    String toString() {
        msgfrom
    }
}

And here's modified controller list method:
def list = {
        def user = springSecurityService.getPrincipal()
        def query = {
            eq("msgto",user.id)
        }
        def criteria = Message.createCriteria()
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        def messages = criteria.list(query,max: params.max, offset: params.offset)

        [messageInstanceList: messages, messageInstanceTotal: messages.count()]
    }

When I run, it's return :
Error 500: Internal Server Error
URI
    /lindv1/message/list
Class
    java.lang.IllegalArgumentException
Message
    java.lang.ClassCastException@175c0e3

Is there something wrong with my code?

I just want filtering a list.

Thanks,
Didin J.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Criteria List return: java.lang.ClassCastException@175c0e3

burtbeckwith
Hard to tell without line numbers or a stacktrace, but you're returning a User from your toString() method. I thought Groovy would call toString() on it, but if not then changing it to msgfrom.toString()  that might fix this. But you probably meant to return a property of the User rather than the whole thing, e.g. msgfrom.username ?

Burt

didinj wrote
Hi all,

I modified generated controller from domain below:
class Message {
    
    User msgfrom
    User msgto
    String msgbody
    Date msgcreated
    
    static hasMany = [reply:Reply]

    static constraints = {
        msgfrom()
        msgto()
        msgbody(nullable:true, maxSize: 8094)
        msgcreated()
    }
    
    String toString() {
        msgfrom
    }
}

And here's modified controller list method:
def list = {
        def user = springSecurityService.getPrincipal()
        def query = {
            eq("msgto",user.id)
        }
        def criteria = Message.createCriteria()
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        def messages = criteria.list(query,max: params.max, offset: params.offset)

        [messageInstanceList: messages, messageInstanceTotal: messages.count()]
    }

When I run, it's return :
Error 500: Internal Server Error
URI
    /lindv1/message/list
Class
    java.lang.IllegalArgumentException
Message
    java.lang.ClassCastException@175c0e3

Is there something wrong with my code?

I just want filtering a list.

Thanks,
Didin J.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Criteria List return: java.lang.ClassCastException@175c0e3

didinj
thanks burt,

Here's stacktrace:
GRAILS-7799: Subtype 'java.lang.Long' of reloadable type com.media.eximio.User is not reloadable: may not see changes reloaded in this hierarchy (please comment on that jira)
| Error 2012-07-05 11:40:48,492 [http-bio-8080-exec-10] ERROR property.BasicPropertyAccessor  - IllegalArgumentException in class: com.media.eximio.User, getter method of property: id
| Error 2012-07-05 11:40:48,497 [http-bio-8080-exec-10] ERROR errors.GrailsExceptionResolver  - IllegalArgumentException occurred when processing request: [GET] /lindv1/message/list
java.lang.ClassCastException@1e7838f. Stacktrace follows:
Message: java.lang.ClassCastException@1e7838f
    Line | Method
->>   53 | <init>       in grails.orm.PagedResultList
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|   1511 | invokeMethod in grails.orm.HibernateCriteriaBuilder
|     22 | list . . . . in com.media.eximio.MessageController$$ENcGZ4SZ
|   1110 | runWorker    in java.util.concurrent.ThreadPoolExecutor
|    603 | run . . . .  in java.util.concurrent.ThreadPoolExecutor$Worker
^    722 | run          in java.lang.Thread

And here's User domain :
class User {

    transient springSecurityService

    static searchable = true
    
    String username
    String password
    String fullname
    String specialist
    String city
    String country = "Indonesia"
    byte[] avatar
    Date createdate
    Date updatedate
    boolean enabled = true
    boolean accountExpired
    boolean accountLocked
    boolean passwordExpired
    
    List tags = new ArrayList()
    static hasMany = [hospital:Hospital,tags:Tags,status:Status,schedule:Schedule, friends: User, activity:Activity]

    static constraints = {
        username blank: false, unique: true
        password blank: false
        fullname blank: true, maxLength:50, nullable: true
        specialist blank: true, maxLength:50, nullable: true
        city blank: true, maxLength:30, nullable: true
        country blank: true, nullable: true
        avatar blank: true, maxSize:102400, nullable: true
        hospital blank: true, nullable: true
        status blank: true, nullable: true
        schedule blank: true, nullable: true
    }

    static mapping = {
        password column: '`password`'
        tags cascade:"all-delete-orphan"        
    }
   
    Set<Role> getAuthorities() {
        UserRole.findAllByUser(this).collect { it.role } as Set
    }
    
    def getTagList() {
        return LazyList.decorate(tags,FactoryUtils.instantiateFactory(Tags.class))
    }

    def beforeInsert() {
        encodePassword()
        createdate = new Date()
    }

    def beforeUpdate() {
        if (isDirty('password')) {
            encodePassword()
        }
        updatedate = new Date()
    }

    protected void encodePassword() {
        password = springSecurityService.encodePassword(password)
    }
    
    String toString() {fullname}
}

I still don't understand where I change msgfrom.toString()?
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Criteria List return: java.lang.ClassCastException@175c0e3

burtbeckwith
The toString method returns a String, but you're returning a User ('msgfrom'). This isn't a String. In Java it wouldn't even compile, but Groovy is more lenient, although it may be causing this problem at runtime. Change the method from returning something that's not a String to one that is, e.g.

   String toString() {
      msgfrom.toString()
   }

or

   String toString() {
      msgfrom.username
   }

Burt

didinj wrote
thanks burt,

Here's stacktrace:
GRAILS-7799: Subtype 'java.lang.Long' of reloadable type com.media.eximio.User is not reloadable: may not see changes reloaded in this hierarchy (please comment on that jira)
| Error 2012-07-05 11:40:48,492 [http-bio-8080-exec-10] ERROR property.BasicPropertyAccessor  - IllegalArgumentException in class: com.media.eximio.User, getter method of property: id
| Error 2012-07-05 11:40:48,497 [http-bio-8080-exec-10] ERROR errors.GrailsExceptionResolver  - IllegalArgumentException occurred when processing request: [GET] /lindv1/message/list
java.lang.ClassCastException@1e7838f. Stacktrace follows:
Message: java.lang.ClassCastException@1e7838f
    Line | Method
->>   53 | <init>       in grails.orm.PagedResultList
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|   1511 | invokeMethod in grails.orm.HibernateCriteriaBuilder
|     22 | list . . . . in com.media.eximio.MessageController$$ENcGZ4SZ
|   1110 | runWorker    in java.util.concurrent.ThreadPoolExecutor
|    603 | run . . . .  in java.util.concurrent.ThreadPoolExecutor$Worker
^    722 | run          in java.lang.Thread

And here's User domain :
class User {

    transient springSecurityService

    static searchable = true
    
    String username
    String password
    String fullname
    String specialist
    String city
    String country = "Indonesia"
    byte[] avatar
    Date createdate
    Date updatedate
    boolean enabled = true
    boolean accountExpired
    boolean accountLocked
    boolean passwordExpired
    
    List tags = new ArrayList()
    static hasMany = [hospital:Hospital,tags:Tags,status:Status,schedule:Schedule, friends: User, activity:Activity]

    static constraints = {
        username blank: false, unique: true
        password blank: false
        fullname blank: true, maxLength:50, nullable: true
        specialist blank: true, maxLength:50, nullable: true
        city blank: true, maxLength:30, nullable: true
        country blank: true, nullable: true
        avatar blank: true, maxSize:102400, nullable: true
        hospital blank: true, nullable: true
        status blank: true, nullable: true
        schedule blank: true, nullable: true
    }

    static mapping = {
        password column: '`password`'
        tags cascade:"all-delete-orphan"        
    }
   
    Set<Role> getAuthorities() {
        UserRole.findAllByUser(this).collect { it.role } as Set
    }
    
    def getTagList() {
        return LazyList.decorate(tags,FactoryUtils.instantiateFactory(Tags.class))
    }

    def beforeInsert() {
        encodePassword()
        createdate = new Date()
    }

    def beforeUpdate() {
        if (isDirty('password')) {
            encodePassword()
        }
        updatedate = new Date()
    }

    protected void encodePassword() {
        password = springSecurityService.encodePassword(password)
    }
    
    String toString() {fullname}
}

I still don't understand where I change msgfrom.toString()?
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Criteria List return: java.lang.ClassCastException@175c0e3

didinj
I already change toString method:
String toString() {
      msgfrom.toString()
   }

But it still returns same error message.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Criteria List return: java.lang.ClassCastException@175c0e3

burtbeckwith
Yep, not the problem. It's actually this line:

   eq("msgto",user.id)

You're telling Hibernate to compare a User to a user's id (a Long). Change it to

   eq("msgto",user)

Burt

didinj wrote
I already change toString method:
String toString() {
      msgfrom.toString()
   }

But it still returns same error message.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Criteria List return: java.lang.ClassCastException@175c0e3

didinj
I solved the problem, just add id after "msgto".
def query = {
            eq("msgfrom.id",user.id)
        }

Thanks Burt.
Loading...