Different class loaders throw a ClassCastException when saving data through Spring

I am creating MVC Spring webapp. Usage: Jetty (servlet container), DataNucleus (dao platform), DB4O (built-in data storage).

When I save an object (made from a Spring controller) using JDO from the DataNucleus, it saves perfectly to the database.

@PersistenceCapable public class Test { @Persistent private String testString; //getter-setters implemented } 

When I make a simple request for previously added objects, I get a ClassCastException in my Test class (cannot distinguish abcTest to abcTest ).

The Test class loader returned by JDO is (toString) [ sun.misc.Launcher$AppClassLoader@5acac268 ] , the class class loader of the Test class, before I saved it to the database, [ WebAppClassLoader@1593275665 ]

I got this far, but I really don't know what to do with a classloader problem like this, I have never thought about class loaders before. Any direction is useful.

+4
source share
2 answers

For the apparent exception of the class, there should not be two different versions of the class. Even the same class definition is considered as two different classes when loaded by two different class loaders. Which seems to be the case here.

Unfortunately, I am not familiar with the platforms you are using, so I can’t give more specific recommendations than this: try experimenting with moving the container containing your Test class to different places in your web application class class and / or reconfiguring class loaders Spring and Jetty to both delegate the loading of Test to the same parent classloader.

+4
source

I think your problem may be similar to that described here .

If so, then it would apparently be useful to make sure that the jdo jarfile is uploaded by the common ancestor of the class loaders.

+2
source

All Articles