2016-04-16 5 views
0

Ich programmiere meine erste Java EE-Anwendung mit JPA, EJB und JSP. bis jetzt habe ich nicht anders als den Tomcat-Server und JSP-Seiten verwendet, um einige einfache Web-Anwendung zu programmieren. Da ich ein sehr großes Projekt schreibe, entschloss ich mich, eine Java Enterprise Application mit EJB und JPA für Modell (mit automatischer Code-Generierung für Persistenz) und Payara Server zu entwickeln. Ich weiß, die Verwendung von automatisierten Code-Generierung ist keine gute Praxis, aber ich bin quadriplegic und ich brauche etwas, das für mich schreiben ... Leider gibt die erste Operation mit EJB einen Fehler zurück und ich kann nicht weitermachen.java.lang.IllegalArgumentException: NameQuery des Namens: findByName nicht gefunden

dies Log-Server:

Caused by: java.lang.IllegalArgumentException: NamedQuery of name: findByName not found. 
    at org.eclipse.persistence.internal.jpa.QueryImpl.getDatabaseQueryInternal(QueryImpl.java:355) 
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createNamedQuery(EntityManagerImpl.java:1135) 
    at com.sun.enterprise.container.common.impl.EntityManagerWrapper.createNamedQuery(EntityManagerWrapper.java:522) 
    at model.session.BrandFacade.getBrandByName(BrandFacade.java:41) 
    at model.session.BrandFacade.create(BrandFacade.java:52) 
    at model.session.BrandFacade.create(BrandFacade.java:61) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:606) 
    at org.glassfish.ejb.security.application.EJBSecurityManager.runMethod(EJBSecurityManager.java:1081) 
    at org.glassfish.ejb.security.application.EJBSecurityManager.invoke(EJBSecurityManager.java:1153) 
    at com.sun.ejb.containers.BaseContainer.invokeBeanMethod(BaseContainer.java:4786) 
    at com.sun.ejb.EjbInvocation.invokeBeanMethod(EjbInvocation.java:656) 
    at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822) 
    at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:608) 
    at org.jboss.weld.ejb.AbstractEJBRequestScopeActivationInterceptor.aroundInvoke(AbstractEJBRequestScopeActivationInterceptor.java:64) 
    at org.jboss.weld.ejb.SessionBeanInterceptor.aroundInvoke(SessionBeanInterceptor.java:52) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:606) 
    at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:883) 
    at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822) 
    at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:608) 
    at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.doCall(SystemInterceptorProxy.java:163) 
    at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.aroundInvoke(SystemInterceptorProxy.java:140) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:606) 
    at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:883) 
    at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822) 
    at com.sun.ejb.containers.interceptors.InterceptorManager.intercept(InterceptorManager.java:369) 
    at com.sun.ejb.containers.BaseContainer.__intercept(BaseContainer.java:4758) 
    at com.sun.ejb.containers.BaseContainer.intercept(BaseContainer.java:4746) 
    at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:212) 
    ... 34 more 

dies die BrandCrudController ... Aufruf .Create Methode erzeugen Fehler:

package controller; 

import java.io.IOException; 
import java.io.PrintWriter; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import javax.ejb.EJB; 
import javax.servlet.ServletException; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import model.entities.Brand; 
import model.exceptions.DuplicateException; 
import model.session.localInterfaces.BrandFacadeLocal; 

public class BrandCrudController extends HttpServlet { 

    @EJB 
    private BrandFacadeLocal brandFacade; 

    /** 
    * Processes requests for both HTTP <code>GET</code> and <code>POST</code> 
    * methods. 
    * 
    * @param request servlet request 
    * @param response servlet response 
    * @throws ServletException if a servlet-specific error occurs 
    * @throws IOException if an I/O error occurs 
    */ 
    protected void processRequest(HttpServletRequest request, HttpServletResponse response) 
      throws ServletException, IOException { 

     response.setContentType("text/html;charset=UTF-8"); 


     try { 
      brandFacade.create("name"); 
     } catch (DuplicateException ex) { 
      Logger.getLogger(BrandCrudController.class.getName()).log(Level.SEVERE, null, ex); 
     } //*/ 


     try (PrintWriter out = response.getWriter()) { 
      /* TODO output your page here. You may use following sample code. */ 
      out.println("<!DOCTYPE html>"); 
      out.println("<html>"); 
      out.println("<head>"); 
      out.println("<title>Servlet NewServlet</title>");    
      out.println("</head>"); 
      out.println("<body>"); 
      out.println("<h1>brand creato </h1>"); 
      out.println("</body>"); 
      out.println("</html>"); 
     } 
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code."> 
    /** 
    * Handles the HTTP <code>GET</code> method. 
    * 
    * @param request servlet request 
    * @param response servlet response 
    * @throws ServletException if a servlet-specific error occurs 
    * @throws IOException if an I/O error occurs 
    */ 
    @Override 
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
      throws ServletException, IOException { 
     processRequest(request, response); 
    } 

    /** 
    * Handles the HTTP <code>POST</code> method. 
    * 
    * @param request servlet request 
    * @param response servlet response 
    * @throws ServletException if a servlet-specific error occurs 
    * @throws IOException if an I/O error occurs 
    */ 
    @Override 
    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
      throws ServletException, IOException { 
     processRequest(request, response); 
    } 

    /** 
    * Returns a short description of the servlet. 
    * 
    * @return a String containing servlet description 
    */ 
    @Override 
    public String getServletInfo() { 
     return "Short description"; 
    }// </editor-fold> 

} 

dies BrandFacade.java

package model.session; 

import model.session.localInterfaces.BrandFacadeLocal; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import javax.ejb.Stateless; 
import javax.persistence.EntityManager; 
import javax.persistence.NoResultException; 
import javax.persistence.PersistenceContext; 
import model.entities.Brand; 
import model.exceptions.DuplicateException; 


@Stateless 
public class BrandFacade extends AbstractFacade<Brand> implements BrandFacadeLocal { 

    @PersistenceContext(unitName = "MyWheelEE-ejbPU") 
    private EntityManager em; 

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

    public BrandFacade() { 
     super(Brand.class); 
    } 

    @Override 
    public Brand getBrandByName(String name) { 
     Brand b = null; 
     try { 
      b = (Brand) em.createNamedQuery("findByName") 
       .setParameter("name", name) 
       .getSingleResult(); 
     } catch (NoResultException e) { 
      return null; 
     } 
     return b; 
    } 

    @Override 
    public void create(String name) throws DuplicateException { 
     if (getBrandByName(name)!= null) 
      throw new DuplicateException("this name already exists"); 
     Brand b =new Brand(0, name); 
     em.persist(b);  
    } 

    @Override 
    public void create(Brand brand) { 
     try { 
      create(brand.getName()); 
     } catch (DuplicateException ex) { 
      Logger.getLogger(BrandFacade.class.getName()).log(Level.SEVERE, null, ex); 
     } 
    } 


} 

und das ist Brand.java entity

package model.entities; 

import java.io.Serializable; 
import java.util.List; 
import javax.persistence.Basic; 
import javax.persistence.CascadeType; 
import javax.persistence.Column; 
import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import javax.persistence.GenerationType; 
import javax.persistence.Id; 
import javax.persistence.NamedQueries; 
import javax.persistence.NamedQuery; 
import javax.persistence.OneToMany; 
import javax.persistence.Table; 
import javax.validation.constraints.NotNull; 
import javax.validation.constraints.Size; 
import javax.xml.bind.annotation.XmlRootElement; 
import javax.xml.bind.annotation.XmlTransient; 

/** 
* 
* @author Marco Bellaiuto 
*/ 
@Entity 
@Table(name = "brand") 
@XmlRootElement 
@NamedQueries({ 
    @NamedQuery(name = "Brand.findAll", query = "SELECT b FROM Brand b"), 
    @NamedQuery(name = "Brand.findByOid", query = "SELECT b FROM Brand b WHERE b.oid = :oid"), 
    @NamedQuery(name = "Brand.findByName", query = "SELECT b FROM Brand b WHERE b.name = :name")}) 
public class Brand implements Serializable { 

    private static final long serialVersionUID = 1L; 
    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    @Basic(optional = false) 
    @Column(name = "oid") 
    private Integer oid; 
    @Basic(optional = false) 
    @NotNull 
    @Size(min = 1, max = 15) 
    @Column(name = "name") 
    private String name; 
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "brandOid") 
    private List<Wheelchair> wheelchairList; 

    public Brand() { 
    } 

    public Brand(Integer oid) { 
     this.oid = oid; 
    } 

    public Brand(Integer oid, String name) { 
     this.oid = oid; 
     this.name = name; 
    } 

    public Integer getOid() { 
     return oid; 
    } 

    public void setOid(Integer oid) { 
     this.oid = oid; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    @XmlTransient 
    public List<Wheelchair> getWheelchairList() { 
     return wheelchairList; 
    } 

    public void setWheelchairList(List<Wheelchair> wheelchairList) { 
     this.wheelchairList = wheelchairList; 
    } 

    @Override 
    public int hashCode() { 
     int hash = 0; 
     hash += (oid != null ? oid.hashCode() : 0); 
     return hash; 
    } 

    @Override 
    public boolean equals(Object object) { 
     // TODO: Warning - this method won't work in the case the id fields are not set 
     if (!(object instanceof Brand)) { 
      return false; 
     } 
     Brand other = (Brand) object; 
     if ((this.oid == null && other.oid != null) || (this.oid != null && !this.oid.equals(other.oid))) { 
      return false; 
     } 
     return true; 
    } 

    @Override 
    public String toString() { 
     return "model.entities.Brand[ oid=" + oid + " ]"; 
    } 

} 

Antwort

3

Ihr Fehler ist hier, in Ihrem Stack-Trace:

Caused by: java.lang.IllegalArgumentException: NamedQuery of name: findByName not found. 

Sie haben eine benannte Abfrage "Brand.findByName" aber Ihr Code ruft "findByName" die es nicht gibt. Ihre Fassade sollte rufen Brand.findByName

+0

omg ... du hast Recht, ich sollte lesen lernen! xD – Marco

Verwandte Themen