10

So I have this following code:

Query query = session.createQuery("from Weather");
        List<WeatherModel> list = query.list();
        WeatherModel w = (WeatherModel) list.get(0);

I wan't to get all the items from the table Weather, but I keep getting the following error:(line 23 is where I create the query)

java.lang.NullPointerException
    at action.WeatherAction.validate(WeatherAction.java:23)
    at com.opensymphony.xwork2.validator.ValidationInterceptor.doBeforeInvocation(ValidationInterceptor.java:251)
    at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:263)............

What's the problem?

NoobEditor
  • 15,563
  • 19
  • 81
  • 112
Hampel Előd
  • 405
  • 2
  • 8
  • 19
  • Fixed the error, new error: java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to model.WeatherModel at action.WeatherAction.validate(WeatherAction.java:29) – Hampel Előd May 04 '13 at 09:20
  • new error: HTTP Status 500 - weather is not mapped [from weather] – Hampel Előd May 04 '13 at 09:24
  • This question is hard to follow, please 1. Put line numbers in your code, 2. update the question instead of adding comments... (or delete it and create a new one) – Déjà vu May 04 '13 at 09:25
  • Possible duplicate of [Hibernate selecting all rows of a table (using .\* ) join with multiple tables, giving Exception](http://stackoverflow.com/questions/10041134/hibernate-selecting-all-rows-of-a-table-using-join-with-multiple-tables-g) – avalancha Mar 04 '16 at 11:51
  • Type safe answer: http://stackoverflow.com/a/35930287/147265 – Adam Mar 11 '16 at 00:56

4 Answers4

11
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);
PSR
  • 39,804
  • 41
  • 111
  • 151
  • If the WeatherModel table is really huge, how to handle this? Is there any way that each time I select 1000 records? – coderz Oct 26 '15 at 22:29
  • 2
    @coderz may I recommend using a paginated list? `createQuery("from Weather").setFirstResult(firstResult).setMaxResults(maxResults).getResultList();` – vkuo Oct 04 '16 at 13:15
7

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);

// Your underlying table name might change in the future.
// Let hibernate take care of the names.
Root<WeatherModel> root = criteriaQuery.from(WeatherModel.class);
criteriaQuery.select(root);

Query<WeatherModel> query = session.createQuery(criteriaQuery);
List<WeatherModel> weatherModelList = query.getResultList();
HSchmale
  • 1,838
  • 2
  • 21
  • 48
  • I've edited your answer; you can put code in `code markdown` by indenting it by 4 spaces. Your answer was in the Low Quality queue. You could improve your answer by explaining how this code solves the problem. – S.L. Barth is on codidact.com Nov 23 '16 at 05:59
1

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.

user1219387
  • 135
  • 1
  • 12
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

Ashish Thukral
  • 1,445
  • 1
  • 16
  • 26