Strange classCastException hibernate 3.5 glassfish

拟墨画扇 提交于 2019-12-23 10:09:58

问题


Hi I have a problem that I can't solve on my own. I have a war file packaged in ear and running on glassfish 3.0.1 with hibernate 3.5 as JPA provider. I compile it with maven and deploy it with idea or manually. Every other time I get a cast exception in my DAOs:

java.lang.ClassCastException: com.myproject.domain.entity.User cannot be cast to 
com.myproject.domain.entity.User

Other times it works perfectly fine. There is no pattern in this behaviour. Could someone shine some light on what is happening here?

Example method where the exception was thrown at com.myproject.domain.dao.UserDAOImpl.checkUserSessionValid(UserDAOImpl.java:195)

public User checkUserSessionValid(String sessionId) {
        User user = null;
        EntityManager em = provider.entityManager();

        try {
            em.getTransaction().begin();
           //Query q = em.createQuery("SELECT u FROM User u WHERE u.session.sessionId = :sessionId"); makes no difference :/
            Query q = em.createQuery("SELECT u FROM User u WHERE u.session.sessionId = :sessionId",User.class);
            q.setParameter("sessionId", sessionId);
            user = (User) q.getSingleResult();

            em.getTransaction().commit();
        } catch (NoResultException ignored) {

        } finally {
            em.close();
        }

        return user;
 }

My libraries
[INFO] +- org.apache.geronimo.specs:geronimo-jpa_2.0_spec:jar:1.0:provided
[INFO] +- javax.validation:validation-api:jar:1.0.0.GA:compile
[INFO] +- org.hibernate:hibernate-annotations:jar:3.5.1-Final:compile
[INFO] |  +- org.hibernate:hibernate-core:jar:3.5.1-Final:compile
[INFO] |  |  +- antlr:antlr:jar:2.7.6:compile
[INFO] |  |  +- commons-collections:commons-collections:jar:3.2.1:compile
[INFO] |  |  +- dom4j:dom4j:jar:1.6.1:compile
[INFO] |  |  |  \- xml-apis:xml-apis:jar:1.0.b2:compile
[INFO] |  |  \- javax.transaction:jta:jar:1.1:provided (scope managed from compile)
[INFO] |  +- org.hibernate:hibernate-commons-annotations:jar:3.2.0.Final:compile
[INFO] |  +- org.hibernate.javax.persistence:hibernate-jpa-2.0-api:jar:1.0.0.Final:compile
[INFO] |  \- org.slf4j:slf4j-api:jar:1.5.2:compile
[INFO] +- org.hibernate:hibernate-entitymanager:jar:3.5.1-Final:compile
[INFO] |  +- cglib:cglib:jar:2.2:compile
[INFO] |  |  \- asm:asm:jar:3.1:compile
[INFO] |  \- javassist:javassist:jar:3.9.0.GA:compile
[INFO] +- org.hibernate:hibernate-validator:jar:4.1.0.Final:compile
[INFO] +- org.slf4j:slf4j-simple:jar:1.5.2:test
[INFO] +- mysql:mysql-connector-java:jar:5.1.13:test
[INFO] +- org.hsqldb:hsqldb:jar:2.0.0:test

回答1:


Well, we sometimes had a similar error using JBoss. The problem there was a class loader (class loader repository) problem. When you have the same class loaded by multiple class loaders you can get that exception because a class loaded by loader 1 and a class loaded by loader 2 are not the same.

In our case we were able to solve this by enabling pass-by-value semantics, i.e. whenever some class loader (or app) border is crossed, the values are serialized/deserialized. Maybe you can check that for Glassfish as well (and look up how class loading is done there).

You could also check whether you have multiple copies or versions of your library in the classpath.




回答2:


For all of you coming here via google, this problem is present in 4.3.6 and up: https://hibernate.atlassian.net/browse/HHH-9446

Downgrading to Hibernate 4.3.5 did the trick for our team.




回答3:


Please check your Generated Sources if you are using Netbeans or any other IDE.

For example NetBeans will generate in your case a User_ class. It will do it for every other class as well within com.myproject.domain.entity package.

Check every file and remove imports from same package. Do not Clean your project since it will regenerate the code with the import statements from the same package. Just Build again and then deploy.

Just to be sure, check your all classes for the following statement (or equivalent) and remove if the import is from the same package:

import com.myproject.domain.entity.User;



来源:https://stackoverflow.com/questions/5483420/strange-classcastexception-hibernate-3-5-glassfish

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