调用JPA命名查询时出现空指针异常

时间:2016-09-05 08:43:42

标签: eclipse jpa glassfish persistence

我知道这里有很多这样的问题,但我看过很多,但仍然无法解决我的问题。

我正在尝试调用命名查询。我有以下内容:

@Stateless
public class ClientCFacade extends AbstractFacade<ClientC>  {

    @PersistenceContext(unitName = "the_database")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public ClientCFacade() {
        super(ClientC.class);
    }

    public List<ClientC> findByClientId(Integer clientId) {
        try {

            List<ClientC> clientCsList = em.createNamedQuery(ClientCs.FIND_BY_CLIENT_ID, ClientC.class).
                    setParameter("clientId", clientId)
                    .getResultList();


            return clientCsList;

        } catch (NoResultException ex) {
            return null;
        }
    }

@Entity
@Table(name = "clients")
@NamedQueries({
        @NamedQuery(name = ClientCs.FIND_BY_CLIENT_ID,
                query = "select cc" +
                        "from " +
                        "ClientCs cc " +
                        "WHERE " +
                        "cc.clientId= :clientId")
})
public class ClientCs implements Serializable {

    private static final long serialVersionUID = 1L;
    public static final String FIND_BY_CLIENT_ID = "ClientCs.findByClientId";

 @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Integer id;
    @Column(name = "client_id", nullable = false)
    private Integer clientId;

所有的吸气剂和制定者。

我称之为:

List<ClientC> Cs = clientCFacade.findByClientId(1234);

我的错误是:

java.lang.NullPointerException
            at the.package.name.ClientCFacade.findByClientId(ClientCFacade.java:56)
            at the.package.name.service.ApiService.runTransaction(ApiService.java:413)
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
            at java.lang.reflect.Method.invoke(Method.java:483)
            at com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60)
            at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$TypeOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:185)
            at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)
            at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:302)
            at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
            at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)
            at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
            at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
            at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1542)
            at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1473)
            at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1419)
            at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1409)
            at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:409)
            at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:558)
            at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:733)
            at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
            at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
            at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
            at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
            at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
            at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
            at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
            at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
            at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:357)
            at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260)
            at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188)
            at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)
            at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168)
            at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)
            at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
            at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
            at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
            at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
            at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
            at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
            at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)
            at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)
            at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
            at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
            at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
            at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564)
            at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544)
            at java.lang.Thread.run(Thread.java:745)

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

您正在设置命名查询中不存在的参数。 在方法public List<ClientC> findByClientId(String clientId)

中将id替换为clientId

它应该是这样的:

 List<ClientC> clientCsList = em.createNamedQuery(ClientCs.FIND_BY_CLIENT_ID, ClientC.class).
                setParameter("clientId", clientId)
                .getResultList();