Select all from a table hibernate

后端 未结 4 572
余生分开走
余生分开走 2021-02-09 02:46

So I have this following code:

Query query = session.createQuery(\"from Weather\");
        List list = query.list();
        WeatherModel w          


        
4条回答
  •  梦谈多话
    2021-02-09 03:21

    On complex projects I prefer not to hard-code the entity name in a String literal:

    Session session = sessionFactory.getCurrentSession();
    CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
    CriteriaQuery criteriaQuery = criteriaBuilder.createQuery(WeatherModel.class);
    Root root = criteriaQuery.from(WeatherModel.class);
    criteriaQuery.select(root);
    Query query = session.createQuery(criteriaQuery);
    List weatherModelList = query.getResultList();
    

提交回复
热议问题