Avoiding Type Safety Warnings Using Hibernate Query.list()

前端 未结 3 1386
囚心锁ツ
囚心锁ツ 2020-12-21 04:12

Is it possible to avoid cast warnings after using createQuery().list?

//Type safety: The expression of type List needs unchecked conversion to conform to Lis         


        
3条回答
  •  再見小時候
    2020-12-21 04:42

    You can avoid the warnings if you use an EntityManager, but not sure if it makes things any nicer:

    EntityManager em = provider.get(); // your code will probably be different here
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery query = builder.createQuery(type); // your Class
    Root root = query.from(type); // your Class
    query.select(root);
    TypedQuery typedQuery = em.createQuery(query);
    typedQuery.getResultList(); // List
    

    Edit: obviously, there are nicer ways of setting this out...

提交回复
热议问题