This discussion is archived
7 Replies Latest reply: Jun 29, 2012 5:15 AM by gimbal2 RSS

Class cast exception EJB 3.0

859967 Newbie
Currently Being Moderated
Hi all,

I am facing this issue:

When i query a list of objects data type A; and i got result in a container which is a java.util.List

The fact, when i print out class name at runtime, i saw it is a java.util.Vector; not a List.

Secondly, when i access one instance of the list; and cast to my object A
i got class cast excetpion +(cannot cast A to A )+

This happens sometimes; but quite frequent.

do you know if there is a workaround for it?

I am using netbeans 7.1' glassfish 3.1.2 and ejb 3.0 on XP

any help appreciated!

thanks,

-HL

Edited by: CardM on Jun 28, 2012 8:33 PM
  • 1. Re: Class cast exception EJB 3.0
    Kayaman Guru
    Currently Being Moderated
    CardM wrote:
    When i query a list of objects data type A; and i got result in a container which is a java.util.List

    The fact, when i print out class name at runtime, i saw it is a java.util.Vector; not a List.
    List is an interface and Vector is a class that implements List.

    Secondly, when i access one instance of the list; and cast to my object A
    i got class cast excetpion +(cannot cast A to A )+
    How are you doing your querying? You shouldn't be casting in the first place. You're doing something fundamentally wrong here I suspect.
  • 2. Re: Class cast exception EJB 3.0
    r035198x Pro
    Currently Being Moderated
    CardM wrote:

    Secondly, when i access one instance of the list; and cast to my object A
    i got class cast excetpion +(cannot cast A to A )+

    This happens sometimes; but quite frequent.

    do you know if there is a workaround for it?

    I am using netbeans 7.1' glassfish 3.1.2 and ejb 3.0 on XP
    That happens if you have the class A loaded on your classpath twice. Check that the jar that contains that class is not being added to multiple locations in your deployment.
  • 3. Re: Class cast exception EJB 3.0
    859967 Newbie
    Currently Being Moderated
    In fact, the Class i am mentioning is an entity. Hence i don't think there are more than one.

    And here is how i did to access one instance
            List<User> list = q.getResultList();
            System.out.println("Class list of users is VECTOR:" + list.getClass());
            User o = (User)list.get(0);
    I assure that the user list has got result; but accessing the first user is problematic; of course happened with other indices too.

    Edited by: CardM on Jun 29, 2012 3:32 AM
  • 4. Re: Class cast exception EJB 3.0
    r035198x Pro
    Currently Being Moderated
    CardM wrote:
    In fact, the Class i am mentioning is an entity. Hence i don't think there are more than one.
    Check the deployment to confirm. Just because it's an Entity doesn't mean you can't have duplicates on the class path.
  • 5. Re: Class cast exception EJB 3.0
    gimbal2 Guru
    Currently Being Moderated
    r035198x wrote:
    CardM wrote:

    Secondly, when i access one instance of the list; and cast to my object A
    i got class cast excetpion +(cannot cast A to A )+

    This happens sometimes; but quite frequent.

    do you know if there is a workaround for it?

    I am using netbeans 7.1' glassfish 3.1.2 and ejb 3.0 on XP
    That happens if you have the class A loaded on your classpath twice. Check that the jar that contains that class is not being added to multiple locations in your deployment.
    Indeed. On top of that both instances of the same class are loaded in different classloaders; even though it may be the same binary class file, because they live in different classloaders to Java they will be different classes. Hence you can get incredibly odd looking "cannot cast class a.b.MyClass to a.b.MyClass". I know of no other reason for that to happen.
  • 6. Re: Class cast exception EJB 3.0
    859967 Newbie
    Currently Being Moderated
    r035198x wrote:
    CardM wrote:
    In fact, the Class i am mentioning is an entity. Hence i don't think there are more than one.
    Check the deployment to confirm. Just because it's an Entity doesn't mean you can't have duplicates on the class path.
    Could you help me how to check the deployment?
    i am a newbie in Glassfish.

    thanks,

    -HL
  • 7. Re: Class cast exception EJB 3.0
    gimbal2 Guru
    Currently Being Moderated
    CardM wrote:
    Could you help me how to check the deployment?
    i am a newbie in Glassfish.
    Somewhere somehow, files are deployed to glassfish so Glassfish can then service the stuff through the web. Check the files (or file, if it is an ear file) that are deployed to Glassfish because that's the only truth.

    Being a 'newbie' (I hate that word, you're a novice) in Glassfish is one thing. The fact that you have to ask this means you don't know much about Java enterprise development in general. But you're also confused by classpath clashes, which can only mean you're not too well versed in Java in general either. If I'm right, it will be very difficult to guide you to do anything I'm afraid. The only way this can work is if someone can give a vague description and you can then understand it and do stuff on your own, its not like anyone can drop by and give you personal instructions. Nor would anyone want to do that.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points