关于Hibernate的createQuery()的空指针

时间:2011-06-02 15:06:23

标签: java hibernate

当我尝试使用createQuery()运行HQL查询时,我正在努力找到空指针异常的底部。

运行查询的代码非常简单。最初我有一个我正在调用的命名查询,但只是为了使事情更简单并消除任何复杂性我正在做这个(springwildlife是包,物种是类/实体):

Query q = session.createQuery("SELECT s FROM springwildlife.Species s");

(顺便说一句:我也曾经尝试过有没有包装名称。我也尝试过更简单的“从springwildlife.Species”查询)

我已经创建了一个XML映射(species.hbm.xml),如下所示:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
  <class name="springwildlife.Species" table="species">
      <id name="id" type="java.lang.Long" column="id" >
          <generator class="native">
              <param name="sequence">species_id_seq</param>
          </generator>

  </id>

  <!-- in the actual file I have one these for each item I want to be mapped -->

  <property name="propertyFromClass" type="java.lang.String">
   <column name="sql_table_column_name" />
  </property>

</hibernate-mapping>

在我的hibernate.cfg.xml文件中,我像这样输入映射:

我的班级/实体看起来像这样:

package springwildlife;

public class Species
{
    // properties here

    public Species()
    {
    }

    // sets, gets, etc.

}

有没有人知道发生了什么?我花了很多时间试图追踪它并且我受到了阻碍。

(我不确定它是否相关,但以防万一,我使用Resin作为我的服务器而IntelliJ作为我的IDE)

这是堆栈跟踪(SpeciesFactory.java中的第68行是对createQuery的调用):

[11-06-02 11:19:26.726] {http://*:8080-2} org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:656)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:119)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:96)
    at com.caucho.server.dispatch.ServletFilterChain.doFilter(ServletFilterChain.java:109)
    at com.caucho.server.webapp.WebAppFilterChain.doFilter(WebAppFilterChain.java:156)
    at com.caucho.server.webapp.AccessLogFilterChain.doFilter(AccessLogFilterChain.java:95)
    at com.caucho.server.dispatch.ServletInvocation.service(ServletInvocation.java:287)
    at com.caucho.server.http.HttpRequest.handleRequest(HttpRequest.java:792)
    at com.caucho.network.listen.TcpSocketLink.dispatchRequest(TcpSocketLink.java:730)
    at com.caucho.network.listen.TcpSocketLink.handleRequest(TcpSocketLink.java:689)
    at com.caucho.network.listen.TcpSocketLink.handleRequestsImpl(TcpSocketLink.java:669)
    at com.caucho.network.listen.TcpSocketLink.handleRequests(TcpSocketLink.java:617)
    at com.caucho.network.listen.AcceptTask.doTask(AcceptTask.java:104)
    at com.caucho.network.listen.ConnectionReadTask.runThread(ConnectionReadTask.java:98)
    at com.caucho.network.listen.ConnectionReadTask.run(ConnectionReadTask.java:81)
    at com.caucho.network.listen.AcceptTask.run(AcceptTask.java:67)
    at com.caucho.env.thread.ResinThread.runTasks(ResinThread.java:164)
    at com.caucho.env.thread.ResinThread.run(ResinThread.java:130)
Caused by: java.lang.NullPointerException
    at springwildlife.SpeciesFactory.getSpeciesLister(SpeciesFactory.java:68)
    at springwildlife.LifelistController.handleRequestInternal(LifelistController.java:26)
    at org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:153)
    at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:48)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
    ... 18 more

2 个答案:

答案 0 :(得分:13)

在我的情况下,Session对象有效,但我仍然得到了NullPointerException。 原来是我的HQL中的错误(SELECT TCompany FROM TCompany c...而不是SELECT c FROM TCompany c...)。 因此,如果会话有效,您可以检查您的HQL ......

答案 1 :(得分:4)

由于NullPointerExceptioncreateQuery()引发,{唯一可能的原因是sessionnull