从休眠表中选择全部 [英] Select all from a table hibernate

查看:121
本文介绍了从休眠表中选择全部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有下面的代码:

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

我不想从表格中获取所有项目,但我仍然收到以下错误:(第23行是我创建查询的地方)

  java.lang.NullPointerException $ b $ 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)............

有什么问题? p>

解决方案

  Query query = session.createQuery(from Weather); //你将得到Weayher对象
List< WeatherModel> list = query.list(); //您正在以列表形式访问< WeatherModel>

它们都是不同的实体

  Query query = session.createQuery(from Weather); 

列表<天气> list = query.list();

天气w =(天气)list.get(0);


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?

解决方案

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

这篇关于从休眠表中选择全部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆