What is the “proper” way to cast Hibernate Query.list() to List?

后端 未结 10 1666
梦谈多话
梦谈多话 2020-12-04 11:44

I\'m a newbie with Hibernate, and I\'m writing a simple method to return a list of objects matching a specific filter. List seemed a natural return t

10条回答
  •  独厮守ぢ
    2020-12-04 12:34

    You can avoid compiler warning with workarounds like this one:

    List resultRaw = query.list();
    List result = new ArrayList(resultRaw.size());
    for (Object o : resultRaw) {
        result.add((MyObj) o);
    }
    

    But there are some issues with this code:

    • created superfluous ArrayList
    • unnecessary loop over all elements returned from the query
    • longer code.

    And the difference is only cosmetic, so using such workarounds is - in my opinion - pointless.

    You have to live with these warnings or suppress them.

提交回复
热议问题