views:

81

answers:

1

I updated my User class, and now whenever someone that had the old version of the User class stored in their session scope accesses my site, I get an InvalidClassException.

javax.servlet.ServletException: java.lang.RuntimeException: 
java.io.InvalidClassException: User; local class incompatible: 
stream classdesc serialVersionUID = 4949038118012519093, 
local class serialVersionUID = -971500502189813151

How do I stop this error from happening for those users? I could probably invalidate everyone's sessions every time I want to update a class that gets stored in session scope, but is there a better way, so that my user's don't have to login again?

A: 

You can add

private final long serialVersionUID=4949038118012519093;

to your class definition. New class should have the same serializable fields in the same order of course.

Ha
In Java when a class gets serialized and deserialized a check is done against the serialVersionUID for the class. The default behaviour is to assign a new random number to each serializable class when you compile, if one is not created by the developer. If you set one to a random number or -1 then as long as the class does not change you will be OK.
Romain Hippeau