Select all from a table hibernate

后端 未结 4 576
余生分开走
余生分开走 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:14
    Query query = session.createQuery("from Weather"); //You will get Weayher object
    List<WeatherModel> list = query.list(); //You are accessing  as list<WeatherModel>
    

    They both are different entities

    Query query = session.createQuery("from Weather"); 
    
     List<Weather> list = query.list(); 
    
    Weather w = (Weather) list.get(0);
    
    0 讨论(0)
  • 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<WeatherModel> criteriaQuery = criteriaBuilder.createQuery(WeatherModel.class);
    Root<WeatherModel> root = criteriaQuery.from(WeatherModel.class);
    criteriaQuery.select(root);
    Query<WeatherModel> query = session.createQuery(criteriaQuery);
    List<WeatherModel> weatherModelList = query.getResultList();
    
    0 讨论(0)
  • 2021-02-09 03:28

    I just had a similar problem, and it appears to have been solved by providing the full path to the object you are trying to query. So, when I made it look like this: session.createQuery("from com.mystuff.something.or.other.MyEntity") it worked.

    0 讨论(0)
  • Weather will be a different entity than WeatherModel. your list will have Weather objects, it can be only cast if it is sub-type of WeatherModel

    0 讨论(0)
提交回复
热议问题