I have a JPA object:
@Entity
@Table(name="WF_GROUP")
public class Group {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String groupName;
private long parentId;
/* ... */
}
I have a GroupDAO with this method:
public List<Group> getAllGroups() {
List<Group> groups = new ArrayList<Group>();
String query = "select * from WF_GROUP";
Query q = getEntityManager().createNativeQuery(query);
groups.addAll( q.getResultList() );
return groups;
}
The problem is the q.getResultList() returns a result list of type Object than contains an object array for each property.
Why doesn’t q.getResultList() return a list of Group objects?
Thanks!
Rob
Because you don’t specify which class the native query should return. Have a look at the other createNativeQuery methods, taking additional arguments.
Note that the point of using JPA is to use objects rather than database tables. JPQL is there for that. You should use the following code: