Strange classCastException hibernate 3.5 glassfish
Asked Answered
M

3

6

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
Manual answered 30/3, 2011 at 7:48 Comment(2)
Had the same error with a different configuration: Glassfish 3.0.1, EclipseLink, Netbeans/Ant-Deployment. Restarting the server always helped. But it's annoying (GF restart can take a long time). Would like to know the solution (MeTOO-comment :-)Involuntary
Can be that the jar with com.myproject.domain.entity.User located in multiple locations? Glassfish lib folder? Domain lib folder? Multiple wars? Lib under ear? It would be really helpful if you specify all this locations... Also when Hibernate is located?Vittoria
R
3

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.

Rimmer answered 30/3, 2011 at 8:5 Comment(4)
The project is very simple and in fact should be a web app not ear.Manual
Project is simple and could be a web app not ear as it got reduced to a single war inside it.Hibernate isn't installed on glassfish but in my ear/lib along with the domain.jar. The problem wasn't really apparent until I tried to integrate Quartz into my app. Now it appears 3 out of 5 times. I used to see exceptions from quartz jobs using DAOs saying that persistence unit hadn't been yet deployed. In my ServletContextListener that start's quartz after deployment I added a line that gets entitimanager and closes it. Solved the problem with Punit but I now I'm getting errors from DAOs.Manual
There must be some relation with quartz threads but I can't figure it out. BTW The exception I posted is not from quartz but from authentication filter. When I load my gwt login page I see in firebug that multiple POST request are send on load. really bizzare ?! should I change my daos to get around it? BaseDao { protected EntityManagerProvider provider; } class EntityManagerProvider{ private static EntityManagerFactory factory = Persistence.createEntityManagerFactory("myunit"); } I really appreciate your helpManual
As for Quatz: We have an MBean that starts Quartz, so the scheduler would not start until the persistence unit is initialized, since MBeans are started at the end of the startup process. During Quartz startup we read the database and load the jobs. Maybe you could change it that way in order to keep the original entity manager.Rimmer
E
3

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.

Eohippus answered 24/2, 2015 at 9:25 Comment(0)
M
0

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;

Must answered 7/6, 2014 at 1:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.