0

I am using a REST-Service with JPA and every second time I am running the project I get a ClassCastExeption (from a NamedQuery Result). It always works the first time after restarting the Server (Glassfish 4.1.1).

I tried if the problem only occurs with a specific class but it fails to cast no matter what class i get from JPA (after second start of the project).

User u = em.createNamedQuery("getUserByUUID", User.class)
                .setParameter("uuid", uuid)
                .setParameter("deleted", false)
                .getSingleResult();
return (User) u;

Error Message:

java.lang.ClassCastException: de.edvschuleplattling.evt.eventmonitoring.dto.User cannot be cast to de.edvschuleplattling.evt.eventmonitoring.dto.User

Alessio
  • 3,404
  • 19
  • 35
  • 48
  • 1
    Any chance, class is getting loaded by two different class loader? – Akshay Khopkar Jun 19 '19 at 19:28
  • I would imagine this is because there are two different ClassLoaders loading your User class. See [this SO post](https://stackoverflow.com/questions/11865917/classcastexception-because-of-classloaders) for more information. – Michael Krause Jun 19 '19 at 19:29
  • I never did anything with class loaders, so I dont know why there should be 2 different ones. How do I test this? – Matthias Luger Jun 19 '19 at 19:31
  • Hi @MatthiasLuger, if the namedquery returns a User.class, why do you need to cast again in the return? Maybe irrelevant, but if it is a .dto class, why is it persisted? Normally this means only for data transfer objects and entities lives in another package. – Brother Jun 20 '19 at 07:27
  • Also, can you add the full stacktrace to see exactly where is it happening and if possible, the namedquery itself. – Brother Jun 20 '19 at 07:30
  • How do you deploy your application? – Simon Martinelli Jun 20 '19 at 08:10
  • The cast in the return is not necessary, i only wrote it for showing purposes. And the error is thrown in the return line (with and without cast) – Matthias Luger Jun 20 '19 at 18:00

0 Answers0