1

Schema: each Employee may have many phone numbers and each phone number belongs to an Employee (a one-to-Many Relationship).

Using the following JPQL query: SELECT e, p FROM Employee e JOIN e.phones p,

How would you handle the resulting List<Object[]> from this query in your application code? In terms of accessing each employee and his/her phone number in the app. Code from that List<Object[]>? Using

em.createNamedQuery("..").getResultList().get(1)[]
// or
em.createNamedQuery("..").getResultList().get(2)[0]

results in error.

4
  • You misspelled createNamedQuery and getResulList in the code above. Does your actual code even compile? Commented May 11, 2012 at 15:15
  • 1
    results in error - you might want to elaborate on the exact error you get. Commented May 11, 2012 at 15:18
  • Hi Matt , sorry about the spelling.. didnt see it!! Commented May 11, 2012 at 16:05
  • hi Thomas the result is: "Cannot cast from List<Object> to List<Object[]>" getResultList Commented May 11, 2012 at 16:14

1 Answer 1

1

The problem most likely is that getResultList() returns a non generic List and thus you'd have to cast it to List<Object[]> first.

Assuming, your list has at least one entry this should work:

Employee e = (Employee)((List<Object[]>)em.createNamedQuery("..").getResultList()).get(0)[0];

Update:

Just for the reference: depending on the JPA version you use, you might get a List<Object> instead. In that case you could cast the value to Object[]:

Employee e = (Employee)((Object[])em.createNamedQuery("..").getResultList().get(0))[0];
Sign up to request clarification or add additional context in comments.

1 Comment

thank for your help the problem lied in my app. code and i found it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.