Reputation: 21
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
Upvotes: 0
Views: 744
Reputation: 21
so finally problem was caused by missing one of the fields in materialised query table which was mapped on MqtAsutrOrgst2 class
thanx all
Upvotes: 0
Reputation: 1982
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.
Upvotes: 0
Reputation: 49402
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) { ... }
Upvotes: 2
Reputation: 723
Just try to log the type of the object and everything should be clear
Upvotes: 1