尝试使用aws tomcat cdi创建NamedQuery时出现空异常

cnjp1d6j  于 2021-10-10  发布在  Java
关注(0)|答案(0)|浏览(244)

我将aws tomcat 8.5与weld一起使用,当尝试从实体管理器创建命名查询时,会发生以下错误:

Exception Make:getCars():Generic, Message:[null]
sb.boundary.SampleBoundary.getCars(SampleBoundary.java:74)
sb.view.SampleView.getCars(SampleView.java:82)
java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.base/java.lang.reflect.Method.invoke(Method.java:566)
javax.el.BeanELResolver.getValue(BeanELResolver.java:94)
com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176)
com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203)
org.apache.el.parser.AstValue.getValue(AstValue.java:169)
org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:190)
org.jboss.weld.el.WeldValueExpression.getValue(WeldValueExpression.java:50)
com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:109)
javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194)
javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:182)
javax.faces.component.UIData.getValue(UIData.java:732)
org.primefaces.component.api.UIData.lambda$isLazy$0(UIData.java:83)
org.primefaces.util.ComponentUtils.eval(ComponentUtils.java:492)
org.primefaces.component.api.UIData.isLazy(UIData.java:83)
org.primefaces.component.datalist.DataListRenderer.encodeMarkup(DataListRenderer.java:79)
org.primefaces.component.datalist.DataListRenderer.encodeEnd(DataListRenderer.java:73)
javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:924)
javax.faces.component.UIComponent.encodeAll(UIComponent.java:1863)
javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859)
javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859)
com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:456)
com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:134)
javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:337)
com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:120)
com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:219)
javax.faces.webapp.FacesServlet.service(FacesServlet.java:647)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:544)
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:143)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
org.apache.catalina.valves.RemoteIpValve.invoke(RemoteIpValve.java:764)
org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:698)
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:353)
org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:616)
org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:831)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1629)
org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.base/java.lang.Thread.run(Thread.java:829)

边界代码如下:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package sb.boundary;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import sb.entity.Car;
import sb.entity.DummyCar;

/**
 *
 * @author multicoder
 */
@Stateless
//Don't forget to put a persistance context
//https://developer.jboss.org/thread/223578

public class SampleBoundary implements Serializable {

    //@Inject
    @PersistenceContext(unitName = "SamplePU")
    private EntityManager em;

    /**
     * This method is here to return a dummy set of data, this does not require
     * a working persistence unit
     *
     * @return
     */
    public List<DummyCar> getDummyCars() {
        List<DummyCar> dummyCars = new ArrayList<>();
        DummyCar dc = new DummyCar();
        dc.setCarId(1);
        dc.setMake("Found On Road Dead (FORD)");
        dc.setModel("Vulture");
        dc.setYear(1977);
        dummyCars.add(dc);
        dc = new DummyCar();
        dc.setCarId(2);
        dc.setMake("Telsa Coil");
        dc.setModel("Down With Edison V2");
        dc.setYear(1931);
        dummyCars.add(dc);
        dc = new DummyCar();
        dc.setCarId(3);
        dc.setMake("Prius");
        dc.setModel("Maximum of the minimum legal size Mark IIIIIV");
        dc.setYear(2013);
        dummyCars.add(dc);
        return dummyCars;
    }

    public List<Car> getCars() {
        List<Car> cars = null;
        try {
            if (em == null) {
                cars = new ArrayList<>();
                Car car = new Car();
                car.setCarId(-1);
                car.setMake("getCars():Em is null error");
                car.setModel("getCars():We just have no entity manager to deal with");
                cars.add(car);
            }
            if(!em.isOpen()){
                cars = new ArrayList<>();
                Car car = new Car();
                car.setCarId(-1);
                car.setMake("getCars():Em is not open error");
                car.setModel("getCars():We just have no entity manager to deal with");
                cars.add(car);
            }
            Query query = em.createNamedQuery("Car.findAll",Car.class);
            cars = query.getResultList();
            if (cars.size() == 1) {
                cars = new ArrayList<>();
                Car car = new Car();
                car.setCarId(-1);
                car.setMake("Query was non error car");
                car.setModel("We just have no cars to report :|");
                cars.add(car);
            }
        } catch (NoResultException e) {
            System.out.println("Exception when pulling car data via entity!");
            System.out.println("It is:" + e.getMessage());
            cars = new ArrayList<>();
            Car car = new Car();
            car.setCarId(-1);
            car.setMake("Exception Make:getCars():NoResultException");
            car.setModel(e.getMessage());
            cars.add(car);
        } catch (Exception e) {
            System.out.println("Exception when pulling car data via entity!");
            System.out.println("It is:" + e.getMessage());
            cars = new ArrayList<>();
            Car car = new Car();
            car.setCarId(-1);
            car.setMake("Exception Make:getCars():Generic");
            StringBuilder sb = new StringBuilder();
            sb.append("Message:[").append(e.getMessage()).append("]<br/>");
            if (e.getCause() != null) {
                Throwable cause = e.getCause();
                sb.append("Cause:[").append(cause.getMessage()).append("]<br/>");
            }
            for (StackTraceElement ste : e.getStackTrace()) {
                sb.append(ste.toString()).append("<br/>");
            }
            car.setModel(sb.toString());
            cars.add(car);
        }
        return cars;
    }

    public void createSampleCars() {
        try {
            Car car = new Car();
            car.setCarId(1);
            car.setMake("Test Car saved by app!");
            car.setModel("Tester, model... the car model");
            car.setYear(2021);
            em.persist(car);
        } catch (Exception e) {
            Car car = new Car();
            car.setCarId(1);
            car.setMake("Exception bus! createSampleCars()");
            car.setModel(e.getMessage());
            car.setYear(2021);
        }
    }

    public String doQuery(String query){
        return "";
    }
}

这是persistence.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.2" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd">
  <!-- Define Persistence Unit -->
  <persistence-unit name="SamplePU">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <jta-data-source>jdbc/testDS</jta-data-source>
    <class>sb.entity.Car</class>
  </persistence-unit>
</persistence>

这是context.xml

<Context>
    <Resource 
        name="BeanManager"  
        auth="Container"
        type="javax.enterprise.inject.spi.BeanManager"
        factory="org.jboss.weld.resources.ManagerObjectFactory"/>
        <Resource
        name="jdbc/testDS"
        auth="Container"
        type="javax.sql.DataSource"
        driverClassName="oracle.jdbc.OracleDriver"
        url="sampledatabase.theserver.amazonaws.com"
        username="TheUserName"
        password="ThePassWord" /> 
</Context>

如果需要,可以在此处找到完整的电流源。https://github.com/multicoder/tomcattest

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题