So I have this following code:
Query query = session.createQuery(\"from Weather\");
List list = query.list();
WeatherModel w
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);
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();
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.
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