0

I can't understand why i've got ClassCastException at the last row of code
I thought if i had OrgStructure list parametrised - there will be no problem

List<MqtAsutrOrgst2> OrgStructure = null;
    Query query = null;
    ...
            ...

    OrgStructure = query.getResultList();       
    for (Object o : OrgStructure) {

        MqtAsutrOrgst2 orgStructureItem = (MqtAsutrOrgst2) o;

    }

p.s. MqtAsutrOrgst2 is just an @Entity

Sergey
  • 21
  • 7
  • 3
    How is `MqtAsutrOrgst2` related to `OrgStructureItemType` ? – AllTooSir Jul 17 '13 at 07:25
  • 1
    You haven't even shown us the *declaration* for `OrgStructure`, let alone what the query's doing. The fact that `orgstructItems` is parameterized doesn't change the contents of `OrgStructure` at all... – Jon Skeet Jul 17 '13 at 07:26
  • Does `for (MqtAsutrOrgst2 o : OrgStructure)` do the same thing? – CrystalDuck Jul 17 '13 at 07:26
  • Leeward, in case of [for (MqtAsutrOrgst2 o : OrgStructure)] i've got same Exception, but at this line – Sergey Jul 17 '13 at 08:50

4 Answers4

2

I can't understand why i've got ClassCastException at the last row of code

What I understand is MqtAsutrOrgst2 is subclass or implementation type of OrgStructureItemType class . The List orgstructItems is defined to keep anything that is a sub type of OrgStructureItemType which includes MqtAsutrOrgst2 and probably some other sub classes as well which cannot be casted between one another.

MqtAsutrOrgst2 orgStructureItem = (MqtAsutrOrgst2) o;

You are forcing the compiler to believe that at runtime the object o will be an object of MqtAsutrOrgst2 , but actually it is an object of some other sub class of OrgStructureItemType which cannot be cast to MqtAsutrOrgst2.

There are better ways to do this , but you can do a temporary fix :

if(o instanceof MqtAsutrOrgst2)     
  MqtAsutrOrgst2 orgStructureItem = (MqtAsutrOrgst2) o;

The below code will be a quick fail :

for (MqtAsutrOrgst2 o : OrgStructure) { ... }
AllTooSir
  • 48,828
  • 16
  • 130
  • 164
1

Just try to log the type of the object and everything should be clear

Piotr Idzikowski
  • 713
  • 5
  • 13
0

I suppose that MqtAsutrOrgst2 is a subclass of OrgStructure. Seems you get list of OrgStructure using JPA. We need more information on Query object that you use. But most likely you use Query that constructs exactly objects of superclass OrgStructure.

artplastika
  • 1,972
  • 2
  • 19
  • 38
  • query.getResultList() returns org.apache.openjpa.kernel.DelegatingResultList – Sergey Jul 17 '13 at 09:37
  • right! Query constructs exactly objects of superclass OrgStructure but why it turns into DelegatingResultList. And what can i do with that? any ideas? – Sergey Jul 17 '13 at 11:45
  • > OrgStructure but why it turns into DelegatingResultList - That's ok, because you assign it from getResultList(). Please show the code where you create Query object and declaration of class MqtAsutrOrgst2 with all annotations applied to that class. – artplastika Jul 18 '13 at 07:11
0

so finally problem was caused by missing one of the fields in materialised query table which was mapped on MqtAsutrOrgst2 class
thanx all

Sergey
  • 21
  • 7