419 words

java.lang.ClassCastException in OSGi runtime when loading data from DB

2016-12-09 10:44


I'm trying to use ObjectDB in an OSGi environment (Karaf 4.0.7).

I'm now facing ClassCastExceptions on my "entity" classes when I try to read data from the DB.

In a very old (2011?) forum entry this problem was already discussed and it appears to be related to classloader problems.

My entity is a dedicated bundle.

I have one bundle with the database service (doing the actual objectdb actions -> persist, query etc).

I have one business bundle that handles the "business" actions.

The entity obviously has to get loaded by all bundles in order to manipulate the data.

This is the code that seems to throw the exception:

public Person loadPerson(final Integer id) {
  TypedQuery<Person> typedQuery = entityManager.createQuery("SELECT FROM Person p WHERE p.id = 1", Person.class);
  Person singleResult = typedQuery.getSingleResult();
  return singleResult;

Is there a solution to this problem?

I'm not sure if restricting the use of the Entity class to the database service only and the using DTOs could solve the problem but I would mean doubling the classes and having to move data around between the Entities and DTOs.

Many thanks in advance for your support,


alexweirig's picture
Joined on 2016-11-30
User Post #10
2016-12-11 08:31

The ClassCastException probably indicates that the Person class is available from two different class loaders:

  1. A Person class that is specified in your query.
  2. A Person class that is available to ObjectDB.

To eliminate the ClassCastException you must make sure that only one shared class loader is used by both ObjectDB and your bundle that uses ObjectDB. 

ObjectDB Support
ObjectDB - Fast Object Database for Java (JPA/JDO)
support's picture
Joined on 2010-05-03
User Post #2,675
2016-12-12 14:11


the objectdb.jar file being deployed as a standard bundle I can't control it's classloader.

Messing around with classloaders is not best practice in OSGi. Especially am I wondering how I should get this working when I have multiple bundles using objectdb (e.g. Person, Buildings, Departments, Hardware etc). All of these bundles will persist and manipulate entities persisted in objectdb.

Is there a better way to solve this? Do you by any change have some sample projects for OSGi?

I hope I don't have to drop objectdb as I just ordered my first server license...



alexweirig's picture
Joined on 2016-11-30
User Post #11
2016-12-12 16:27

Some ObjectDB users make extensive use of OSGi with ObjectDB including multiple bundles with entity classes. Unfortunately we do not have a reference project, and since we do not develop OSGi bundles it seems that probably we are not aware of all the possibilities and settings. Maybe there are other solutions that do not require setting the context class loader. However, to avoid duplication of classes on retrieval of objects ObjectDB must have access to the same class loader that is used by the bundle that uses ObjectDB to load the entity classes.

ObjectDB Support
ObjectDB - Fast Object Database for Java (JPA/JDO)
support's picture
Joined on 2010-05-03
User Post #2,676

Post Reply

Please read carefully the posting instructions - before posting to the ObjectDB website.

  • You may have to disable pop up blocking in order to use the toolbar (e.g. in Chrome).
  • Use ctrl + right click to open the browser context menu in the editing area (e.g. for using a browser spell checker).
  • To insert formatted lines (e.g. Java code, stack trace) - select a style in the toolbar and then insert the text in the new created block.
  • Avoid overflow of published source code examples by breaking long lines.
  • You may mark in paragraph code words (e.g. class names) with the code style (can be applied by ctrl + D).
  • Long stack traces (> 50 lines) and complex source examples (> 100 lines) should be posted as attachments.
Maximum file size: 32 MB