[java.lang.IllegalArgumentException由于找不到命名查询

问题描述 投票:-1回答:1

我可以将数据添加到数据库中,但是我没有从数据库中获取数据。好的,我正在写我的问题,我的实体类是Product,并且我将数据库操作存储在ProductRepository.java中然后在我的数据库中,其名称为jsfjpadb。

productId productName salesPrice1枚Kerem 12352本书23

我正在尝试获取数据库中的数据,但我想将其显示在ProductOzetSayfasi.xhtml上,但没有出现。

public class ProductRepository {

    public List<Product> list() {
        EntityManagerFactory factory = Persistence.createEntityManagerFactory("kerem");
        EntityManager manager = factory.createEntityManager();
//      String string = "SELECT product FROM Product as product";
//      Query query = manager.createNamedQuery(string);
        TypedQuery<Product> productQuery = manager.createQuery("SELECT p FROM Product p",Product.class);
        List<Product> productList =productQuery.getResultList();
        manager.close();
        return productList;
    }
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.2" xmlns="http://xmlns.jcp.org/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">
    <persistence-unit name="kerem" transaction-type="RESOURCE_LOCAL">
        <class>com.kerem.inventory.entity.Tablo</class>
        <class>com.kerem.inventory.entity.Product</class>

        <properties>
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/jsfjpadb"/>
            <property name="javax.persistence.jdbc.user" value="root"/>
            <property name="javax.persistence.jdbc.password" value="kerem2112"/>
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
        </properties>
    </persistence-unit>
</persistence>
package com.kerem.inventory.faces;
import java.util.*;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import com.kerem.inventory.entity.Product;
import com.kerem.inventory.repository.ProductRepository;

@ManagedBean
public class ProductOzetSayfasiBean {

    private List<Product> productList;

    public ProductOzetSayfasiBean() {
        ProductRepository repository = new ProductRepository();
        productList = repository.list();
    }

    public List<Product> getProductList() {
        return productList;
    }

}
package com.kerem.inventory.entity;
import java.io.Serializable;
import java.util.List;
import javax.persistence.*;

@Entity
@Table(name = "product")
public class Product implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int productId;
    private String productName;
    private double salesPrice;

    public Product() {
    }

    public int getProductId() {
        return this.productId;
    }

    public void setProductId(int productId) {
        this.productId = productId;
    }

    public String getProductName() {
        return this.productName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

    public double getSalesPrice() {
        return this.salesPrice;
    }

    public void setSalesPrice(double salesPrice) {
        this.salesPrice = salesPrice;
    }
}
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html">

<h:head>
    <title>Product</title>
</h:head>
<h:body>
    <h1>Product</h1>
    <h:form>
        <h:dataTable value="#{productOzetSayfasiBean.productList}" var="product">
            <h:column>
                <h:outputText value="#{product.productId}"/>
            </h:column>
        </h:dataTable>
    </h:form>
</h:body>
</html>

堆栈跟踪


INFO: Creating instance of com.kerem.inventory.faces.ProductOzetSayfasiBean
[EL Info]: 2020-05-01 04:44:00.226--ServerSession(985404382)--EclipseLink, version: Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd
Fri May 01 04:44:00 PDT 2020 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
Fri May 01 04:44:01 PDT 2020 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
[EL Info]: connection: 2020-05-01 04:44:02.365--ServerSession(985404382)--file:/D:/EclipseProjeleri/.metadata/.plugins/org.eclipse.wst.server.core/tmp1/wtpwebapps/kerem/WEB-INF/classes/_kerem login successful
May 01, 2020 4:44:02 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [Faces Servlet] in context with path [/kerem] threw exception [NamedQuery of name: select product from Product as product not found.] with root cause
java.lang.IllegalArgumentException: NamedQuery of name: select product from Product as product not found.
    at org.eclipse.persistence.internal.jpa.QueryImpl.getDatabaseQueryInternal(QueryImpl.java:351)
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createNamedQuery(EntityManagerImpl.java:1124)
    at com.kerem.inventory.repository.ProductRepository.list(ProductRepository.java:19)
    at com.kerem.inventory.faces.ProductOzetSayfasiBean.<init>(ProductOzetSayfasiBean.java:19)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:500)
    at java.base/java.lang.reflect.ReflectAccess.newInstance(ReflectAccess.java:166)
    at java.base/jdk.internal.reflect.ReflectionFactory.newInstance(ReflectionFactory.java:404)
    at java.base/java.lang.Class.newInstance(Class.java:591)
    at org.apache.myfaces.config.annotation.Tomcat7AnnotationLifecycleProvider.newInstance(Tomcat7AnnotationLifecycleProvider.java:56)
    at org.apache.myfaces.config.ManagedBeanBuilder.buildManagedBean(ManagedBeanBuilder.java:156)
    at org.apache.myfaces.el.unified.resolver.ManagedBeanResolver.createManagedBean(ManagedBeanResolver.java:333)
    at org.apache.myfaces.el.unified.resolver.ManagedBeanResolver.getValue(ManagedBeanResolver.java:296)
    at javax.el.CompositeELResolver.getValue(CompositeELResolver.java:63)
    at org.apache.myfaces.el.unified.resolver.FacesCompositeELResolver.getValue(FacesCompositeELResolver.java:169)
    at org.apache.myfaces.el.VariableResolverImpl.resolveVariable(VariableResolverImpl.java:65)
    at org.apache.myfaces.el.convert.VariableResolverToELResolver.getValue(VariableResolverToELResolver.java:123)
    at javax.el.CompositeELResolver.getValue(CompositeELResolver.java:63)
    at org.apache.myfaces.el.unified.resolver.FacesCompositeELResolver.getValue(FacesCompositeELResolver.java:169)
    at org.apache.el.parser.AstIdentifier.getValue(AstIdentifier.java:94)
    at org.apache.el.parser.AstValue.getValue(AstValue.java:137)
    at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:184)
    at org.apache.myfaces.view.facelets.el.ContextAwareTagValueExpression.getValue(ContextAwareTagValueExpression.java:96)
    at javax.faces.component._DeltaStateHelper.eval(_DeltaStateHelper.java:246)
    at javax.faces.component.UIData.getValue(UIData.java:2028)
    at javax.faces.component.UIData.createDataModel(UIData.java:1976)
    at javax.faces.component.UIData.getDataModel(UIData.java:1953)
    at javax.faces.component.UIData.getRowCount(UIData.java:478)
    at org.apache.myfaces.shared.renderkit.html.HtmlTableRendererBase.encodeInnerHtml(HtmlTableRendererBase.java:328)
    at org.apache.myfaces.shared.renderkit.html.HtmlTableRendererBase.encodeChildren(HtmlTableRendererBase.java:198)
    at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:549)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:749)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:758)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:758)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:758)
    at org.apache.myfaces.view.facelets.FaceletViewDeclarationLanguage.renderView(FaceletViewDeclarationLanguage.java:1900)
    at org.apache.myfaces.application.ViewHandlerImpl.renderView(ViewHandlerImpl.java:285)
    at org.apache.myfaces.lifecycle.RenderResponseExecutor.execute(RenderResponseExecutor.java:115)
    at org.apache.myfaces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:241)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:199)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:528)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1099)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:670)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1520)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1476)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.base/java.lang.Thread.run(Thread.java:830)

database jpa
1个回答
0
投票

通过注释或在xml映射文件中定义命名查询。 createNamedQuery需要查询名称,但不需要实际查询。如果您不想这样做,请改用createQuerycreateQuery进行实际查询。

参考Creating Queries using JPQL

© www.soinside.com 2019 - 2024. All rights reserved.