Jsondb / jsondb-core

JsonDB a pure java database that stores its data as Json Files

Home Page:http://www.jsondb.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ClassCastException

opened this issue · comments

Hello again,

I am having a different issue now.

Same bean

@Data
@Document(collection = "beans", schemaVersion = "1.0")
public class Bean {
  @Id
  private String id;
  
  private List<String> stuff;

  public Bean() {
    this.id = UUID.randomUUID().toString();
  }
}

and database class

public class Database {
 private final JsonDBTemplate jsonDBTemplate = new JsonDBTemplate...

  public void a() {
   List<Bean> all = jsonBDTemplate.findAll(Bean.class);
   for (Bean bean : all) {
     System.out.println(bean);
   }
 }
}

I also have a Spring Controller

@RestController
@RequestMapping("/test")
public class TestController {
  @Autowired
  private Database database;

  @RequestMapping("/a")
  public void invokeA() {
    database.a();
  }
}

So when I call http://localhost/test/a I get

java.lang.ClassCastException: Bean cannot be cast to Bean
  at Database.a() ~[]
  at TestController.invokeA() ~[]
  at sun.reflect .....

where the at Database.a() points to the line for (Bean bean : all) .

I was looking online and people suggest that somehow I may have several class loaders and somehow I may be casting classes from different class loaders. Have you ever had this, and how do I obtain more information for debug?

Spring boot starter parent: 1.5.8.RELEASE
Version: 1.0.64

Ok, I have found out the root cause of the issue. It happened because I used spring-boot-devtools which would restart the application every time you save a file. So the devtools artifact has its own class loader that pretty much makes all the objects during the development, instead of sun.misc.Launcher$AppClassLoader.

Would be great to know if there is a way to make it work with the devtools. Not a priority though.

Hi,

Sorry could not get to studying the problem, been busy with work. Just wanted to mention As far as I remember have not used any custom classloader in JsonDB.

I do know that there is a user who has used JsonDB with a lot of custom classloader code and it works fine.

I have not used spring-boot-devtools myself, but i will see if i can try it out over the weekend.

If you suspect classloaders are causing this, then what if you saved the classloader used at time of instantiation of JsonDBTemplate
final ClassLoader cached = Thread.currentThread().getContextClassLoader();
And then before you run your for each loop set the cached loader
Thread.currentThread().setContextClassLoader(cached)

Would this maybe solve your problem

I think that fiddling with class loaders just for the development environment is not worth it. I have disabled boot-dev, and I think I am fine with that.
Is there a way to integrate with that framework though?

@oiale @FarooqKhan @SJKA Please check: https://github.com/vince-bickers/spring-data-jsondb. It's still WIP, but basic Repository and REST integration is working.

@vince-bickers for creating this let me know when it is complete I can add this detail to the webpage and provide links to your repository. Optionally if you are willing we can move this under the jsondb organization on github you can still own it.