I can do this easily with JPQL that only returns data from one table.
SELECT m1 FROM MasatosanTest m1
That means one data type is returned. So I can just store the query result into the List with type specified:
List<MasatosanTest> mt = query.getResultList();
code snippet
private static final String JPQL_TEST = "SELECT m1 FROM MasatosanTest m1;
@Path("innerJoin")
@GET
@Produces("application/json")
public List<MasatosanTest> getJoinedResult() {
System.out.println("getJoinedResult called");
EntityManager em = null;
List<MasatosanTest> mt = null;
try {
em = EmProvider.getDefaultManager();
Query query = em.createQuery(JPQL_TEST);
mt = query.getResultList();
}
catch(Exception e) {
System.out.println("MasatosanTestResource.java - getJoinedResult ERROR: " + e);
}
finally {
if(em != null) {
em.close();
}
}
return mt;
}
Now, if I try JPQL that involves 2 tables....
query
SELECT m1, m2 FROM Masatosan m1, Masatosan2 m2;
List<Masatosan> result = query.getResultList();
This does not cause immediate error however actually Object type is being returned instead of particular type such as Masatosan
or Masatosan2
So when I iterate, it cause CASTException,
for(Masatosan item : result) { .... }
What is the good way to deal with this scenario?
UPDATE
If I system print the variable "result
" it spits:
return object ==========>
[[Ljava.lang.Object;@1540f1e, [Ljava.lang.Object;@1ac7e54, [Ljava.lang.Object;@199cd0a,
[Ljava.lang.Object;@6487d7, [Ljava.lang.Object;@125755, [Ljava.lang.Object;@239ff3,
[Ljava.lang.Object;@da2335, [Ljava.lang.Object;@13da77b, [Ljava.lang.Object;@bea4e1,
[Ljava.lang.Object;@3add4b, [Ljava.lang.Object;@968e06, [Ljava.lang.Object;@4642c8,
[Ljava.lang.Object;@ca81a4, [Ljava.lang.Object;@105510f, [Ljava.lang.Object;@cde78,
[Ljava.lang.Object;@e1b60e, [Ljava.lang.Object;@776306, [Ljava.lang.Object;@6275c,
[Ljava.lang.Object;@21035, [Ljava.lang.Object;@1762346, [Ljava.lang.Object;@105ea3d,
[Ljava.lang.Object;@15564f6, [Ljava.lang.Object;@1577817, [Ljava.lang.Object;@18d30be,
[Ljava.lang.Object;@7b235c, [Ljava.lang.Object;@4e83d4, [Ljava.lang.Object;@b0f862]
First, I thought i was in array but that [ bracket inside the first one seems to be just a part of String???
To test:
for(Object items : result) {
System.out.println("-------------------" + items);
System.out.println(items.toString());
return null;
}
This will output:
-------------------[Ljava.lang.Object;@c723e8
[Ljava.lang.Object;@c723e8
So the square bracket next to "L" does not represent array.
This means the query result is actually store in List<Object>
which consists of objects of 2 types (i.e. Masatosan Object
and Masatotan2
Object)
And ClassCastException :( I'm confused, it should be castable)
int count = 0;
for(Object items : mt) {
System.out.println("-------------- " + count + " --------------" + items);
count ++;
Masatosan woot = (Masatosan) items;
System.out.println(woot.getUsername());
}
java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to local.test.entity.Masatosan
That possibly returns a
List<Object[]>
. The message of theClassCastException
will have the exact type. In case I'm correct:To quote the docs