I have two classes Stream
and Lake
that extend WaterBody
. The Stream
or Lake
are stored in the session managed by Apache Shiro 1.2.4 to be used later.
WaterBody lake = new Lake();
session.setAttribute("water", lake);`
And retrieve it by using
WaterBody water = (WaterBody) session.getAttribute("water");
On a fresh start of the Wildfly 9.0.2 server I can store the store the objects just fine to the session and retrieve them just fine. It does not matter if I am storing a Lake
or Stream
to the water
session attribute. However after a redeploy of the project without restarting the Wildfly server I can store to the session attribute water
the same class type only as before ie: can't store Lake
first and redeploy then store Stream
. I get this error:
java.lang.ClassCastException: cannot assign instance of [Lfwp.fish.water.HydroUnit; to field fwp.fish.water.WaterBody.hucs of type [Lfwp.fish.water.HydroUnit; in instance of fwp.fish.water.Stream
The class WaterBody
has getters and setters for a private field of type HydroUnit
and since this is inheritied by both Stream
and Lake
the error makes little sense.
If on a fresh start the water
session attribute is assigned to both the same class before and after the redeploy it will assign the value to the session variable just fine and then on retrieval it will throw this error:
java.lang.ClassCastException: fwp.fish.water.Lake cannot be cast to fwp.fish.water.WaterBody
Since Lake
extends WaterBody
it should not have an issue.
With Shiro we are using the ehcache 2.9 for the session cache.
It seems like the serialVersionUID
is being ignored by the cache or it's generating a different one itself behind the scenes and it thinks that the classes have changed when they have not between redeployments.
The classes:
public class WaterBody implements Serializable {
private static final long serialVersionUID = -5481729151007010163L;
private int id;
private HydroUnit[] hucs;
public WaterBody() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public HydroUnit[] getHucs() {
return hucs;
}
public void setHucs(HydroUnit[] hucs) {
this.hucs = hucs;
}
}
public class Lake extends WaterBody {
private static final long serialVersionUID = -925557850476426686L;
private String centroidLatitude;
private String centroidLongitude;
public Lake(){
super();
}
public String getCentroidLatitude() {
return centroidLatitude;
}
public void setCentroidLatitude(String centroidLatitude) {
this.centroidLatitude = centroidLatitude;
}
public String getCentroidLongitude() {
return centroidLongitude;
}
public void setCentroidLongitude(String centroidLongitude) {
this.centroidLongitude = centroidLongitude;
}
}
public class Stream extends WaterBody {
private static final long serialVersionUID = 2556033593701784053L;
private String mouthLatitude;
private String mouthLongitude;
public Stream(){
super();
}
public String getMouthLatitude() {
return mouthLatitude;
}
public void setMouthLatitude(String mouthLatitude) {
this.mouthLatitude = mouthLatitude;
}
public String getMouthLongitude() {
return mouthLongitude;
}
public void setMouthLongitude(String mouthLongitude) {
this.mouthLongitude = mouthLongitude;
}
}