Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unexplainable ClassCastException thrown in Play Framework

I've been experiencing a strange error while working on my Play Framework project. While my project is running, I will sometimes receive a ClassCastException, but the error is this:

ClassCastException occured : models.Person cannot be cast to models.Person

This occurs usually when I'm calling a find method such as:

Person p = Person.find("name=?","Joe").first();

If I restart the project, the problem goes away, but only temporarily. It makes testing my project a major pain. How do I fix this?

like image 771
torourke Avatar asked Dec 10 '11 18:12

torourke


3 Answers

I've experienced this error while in Dev mode in Play, in two scenarios (as far as I can remember):

  • Modify an entity and try to recover values from cache that are objects of that entity class.
  • A compilation error while reloading the code of the page/application

In both scenarios fixing compilation errors or cleaning the cache solved the issue.

Not saying that those are the only possibilities, it may be that you are having some other issue.

like image 104
Pere Villega Avatar answered Nov 14 '22 18:11

Pere Villega


This most likely occurs because you've somehow loaded the Person class under two different class loaders. When a class is loaded twice in two different class loaders it's effectively two distinct classes.

(Unfortunately, I can't tell you where/how you might have done this.)

(And it is a bit curious to have the problem pop up on the statement you list. Are you certain that's where it's occurring? Perhaps you should show the exception traceback.)

like image 43
Hot Licks Avatar answered Nov 14 '22 18:11

Hot Licks


In my case, this is related to applying evolutions from the web interface. Someone raised a bug for this, but so far it hasn't received any attention from the dev team. There is a patch attached to the ticket, but I haven't tried it, so YMMV.

like image 1
Will Hains Avatar answered Nov 14 '22 18:11

Will Hains