简体   繁体   中英

Possible reasons for a class cast from an instance to its own class to fail?

IE: com.adms.fcl3.entity.User cannot be cast to com.adms.fcl3.entity.User

I started getting this kind of errors since migrating my project from EJB/ManagedBeans to CDI, so my wild-guess would be that AoP / Injection messes up the classes.

But I have no evidence of that. And no solution either.

The responsible code:

public User getByLoginPasswdValid(String login, String passwd) {
    TypedQuery<User> q = em.createNamedQuery("User.findByLoginPasswdValid", User.class);
    q.setParameter("login", login);
    q.setParameter("passwd", passwd);
    List<User> results = q.getResultList();
    if (results.isEmpty()) {
        return null;
    }
    return results.get(0);
}

Fails at return results.get(0); . Debuger confirms me that the list is valid, has a User object, with proper values etc...

When you have multiple class loaders, you can load the two classes with the same name at the same time. These two class are not interchangable. Unfortunately the ClassCastException gives you no indication that while the classes have the same name, they are not the same class and have different class loaders.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM