About Question enthuware.oce-jpad.v6.2.476 :

Moderator: admin

Post Reply
johnlong
Posts: 197
Joined: Mon Jun 20, 2016 5:06 pm
Contact:

About Question enthuware.oce-jpad.v6.2.476 :

Post by johnlong »

The right way would be: TypedQuery<Employee> q = em.createQuery(c);
I think it would be good to mention that in this case "c" has to be typed as well.

Question :
The cast for the return value is not really required (but is not wrong). The following will work as well: List<Employee> returnValue = q.getResultList();
Would it work if CriteriaQuery, Root and TypedQuery are not typed?

admin
Site Admin
Posts: 10333
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.oce-jpad.v6.2.476 :

Post by admin »

No, I don't think so but you would have to try it out.

johnlong
Posts: 197
Joined: Mon Jun 20, 2016 5:06 pm
Contact:

Re: About Question enthuware.oce-jpad.v6.2.476 :

Post by johnlong »

(Note that em.createQuery always returns a TypedQuery (which extends Query) object when you pass a CriteriaQuery. But when you pass a String, it returns a Query object.)

Not exactly correct.
There is an overloaded method em.createQuery(String s, Class<T> c) which returns TypedQuery<T>

admin
Site Admin
Posts: 10333
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.oce-jpad.v6.2.476 :

Post by admin »

Which part is not correct?
1. em.createQuery always returns a TypedQuery (which extends Query) object when you pass a CriteriaQuery.
This is correct.
2. But when you pass a String, it returns a Query object.)
This is also correct because 1. TypedQuery is a Query 2. In your example, you are not passing just a String. You are passing String and the type.

Post Reply

Who is online

Users browsing this forum: No registered users and 41 guests