Hibernate的createQuery()上的指针为空

ih99xse1  于 2023-02-04  发布在  其他
关注(0)|答案(2)|浏览(151)

当我尝试使用createQuery()运行HQL查询时,发生了一个空指针异常,我正在努力弄清这个异常的原因。
运行查询的代码非常简单,最初我调用的是一个命名查询,但为了使事情更简单,消除任何复杂性,我这样做(springwildlife是包,Species是类/实体):

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

(BTW:我也试过在有和没有软件包名称的情况下使用它。我也试过执行一个更简单的“FROM springwildlife.Species”查询)
我创建了一个XMLMap(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文件中,我像这样拉入Map:
我的类/实体如下所示:

package springwildlife;

public class Species
{
    // properties here

    public Species()
    {
    }

    // sets, gets, etc.

}

有人知道发生了什么事吗?我花了很多时间试图追踪它,但我受阻了。
(我不确定这是否相关,但为了以防万一,我使用Resin作为服务器,IntelliJ作为IDE)
这是堆栈跟踪(www.example.com中的第68行SpeciesFactory.java是对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
8cdiaqws

8cdiaqws1#

在我的例子中,Session对象是有效的,但是我仍然得到了NullPointerException。结果是我的HQL中的一个错误(SELECT TCompany FROM TCompany c...而不是SELECT c FROM TCompany c...)。所以如果Session是有效的,你可以检查你的HQL...

pdkcd3nj

pdkcd3nj2#

由于NullPointerException是在createQuery()所在的行抛出的,因此唯一可能的原因是sessionnull

相关问题