Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange classCastException hibernate 3.5 glassfish

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
like image 376
user682217 Avatar asked Mar 30 '11 07:03

user682217


2 Answers

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.

like image 192
Thomas Avatar answered Oct 23 '22 03:10

Thomas


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.

like image 37
tobitobitobi Avatar answered Oct 23 '22 03:10

tobitobitobi