java.lang.ClassCastException with the same class object

不问归期 提交于 2019-12-02 07:36:05

问题


This piece of code irritates me, sometimes it working and some other times it doesn't !

The NamedQuery : (name = "User.findByLogin", query = "SELECT u FROM User u WHERE u.login = :login")

public User findByLogin(String login) {
        Query query = em.createNamedQuery("User.findByLogin");
        query.setParameter("login", login);
        try {
            return (User) query.getSingleResult();
        } catch (javax.persistence.NoResultException ex) {
            return null;
        }
    }

The error make me crazy !

Avertissement: EJB5184:A system exception occurred during an invocation on EJB UserFacade, method: public dz.admin.entity.User dz.admin.service.UserFacade.findByLogin(java.lang.String) Avertissement: javax.ejb.EJBException .... Caused by: java.lang.ClassCastException: dz.elit.admin.entity.User cannot be cast to dz.elit.admin.entity.User at dz.elit.admin.service.UserFacade.findByLogin(UserFacade.java:45)


回答1:


My guess would be that you have different classloaders. The same class loaded in a different classloader is still considered different by the JVM.

To verify this, you could try to catch the exception and print/log the classloaders.

public User findByLogin(String login) {
    Query query = em.createNamedQuery("User.findByLogin");
    query.setParameter("login", login);
    Object result = null;
    try {
        result = query.getSingleResult();
        return (User) result ;
    } catch (javax.persistence.NoResultException ex) {
        return null;
    } catch (ClassCastException ex) {
        logger.info("Object classloader: " + result.getClass().getClassLoader());
        logger.info("Target class classloader: " + User.class.getClassLoader());
        if(result.getClass().getClassLoader() != User.class.getClassLoader()) {
            logger.warn("Different classloaders detected!");
        }
    }
}

As for the solution, that of course depends on the rest of your setup... To get you started, I can give you some pointers to related questions asked before. Maybe some of the answers there can be helpful to you:

  • Solution for the ClassCastException due to ClassLoader issue
  • ClassCastException because of classloaders?
  • cast across classloader?
  • ClassCastException when casting to the same class

Some of the suggested solutions include changing your classloader setup, using a common interface, or serializing/deserializing your object.




回答2:


I face same issue in my spring-boot application and waste my so much time to solve it.

Issue is generate because of classloaders.

Classloaders of Model class and Session class both are different.

System.out.println("ClassLoader : " + Employee.class.getClassLoader());
System.out.println(session.getClass().getClassLoader()); //Hibernate Session object

ClassLoader : org.springframework.boot.devtools.restart.classloader.RestartClassLoader@a3af3c
sun.misc.Launcher$AppClassLoader@1d16e93

I face this issue because of this dependency.

<dependency>
  <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <optional>true</optional>
</dependency>

When i comment this then my code working fine, so i suggest you to please check your jars.

System.out.println("ClassLoader : " + Employee.class.getClassLoader());
System.out.println(session.getClass().getClassLoader()); //Hibernate Session object

ClassLoader : sun.misc.Launcher$AppClassLoader@1d16e93
sun.misc.Launcher$AppClassLoader@1d16e93

Hope this answer help you.




回答3:


Query query = em.createNamedQuery("User.findByLogin");
...
return (User) query.getSingleResult();

Probably you will need to check whether User really exist (not null). sometime it will be null and you will need to re-refer it

sometimes it working and some other times it doesn't !



来源:https://stackoverflow.com/questions/43865259/java-lang-classcastexception-with-the-same-class-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!