TypedQuery<x> returns vector of Object[] instead of list of x-type object

此生再无相见时 提交于 2020-01-06 03:43:31

问题


I have a method:

public List<Timetable> getTimetableTableForRegion(String id) {
        List<Timetable> timetables;
        TypedQuery<Timetable> query = em_read.createQuery("SELECT ..stuff.. where R.id = :id", Timetable.class).setParameter("id", Long.parseLong(id));
        timetables = query.getResultList();

        return timetables;
    }

which returns this:

so, what am I missing in order to return a list of Timetable's?


回答1:


ok, so, ..stuff.. part of my JPQL contained an inner join to other table. Even through in SELECT there were selected fields just from one table, which was used as type - Timetable, Eclipslink was unable to determine if this fields are part of that entity and instead of returning list of defined entity returned list of Object[].

So in conclusion: Use @OneToMany/@ManyToOne mappings (or flat table design) and query just for ONE table in your JPQL to be able to typize returned entities.




回答2:


Not sure it might be something is looking for, but I had similar problem and converted Vector to ArrayList like this:

    final ArrayList<YourClazz> results = new ArrayList<YourClazz>();;
    for ( YourClazzkey : (Vector<YourClazz>) query.getResultList() )
    {
        results.add(key);
    }


来源:https://stackoverflow.com/questions/23202730/typedqueryx-returns-vector-of-object-instead-of-list-of-x-type-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!