When you write em.createNamedQuery("NameOfNamedQuery", MyClass.class)
, you say you want to fetch MyClass instances (in your query MyClass==Element). The problem is now, that your query SELECT e.name FROM Element e
returns Strings instead of Element instances (as the e.name fields are of type Strings).
In order to correct the problem you either correct the call to em.createNamedQuery(Element.FIND_ALLNAMES, String.class)
(the preferred solution, as it returns a TypedQuery<String>
instance), or you remove the second parameter: em.createNamedQuery(Element.FIND_ALLNAMES)
(in this case you are not type-safe anymore, as it returns a Query
instance).