0

I was working on JSF product crud application using the Java Persistence API with eclipse -link 2.5 and i was wondering why JPA is not recognizing my entities.

here's my entity class:

package Models;
import java.io.Serializable;
import javax.persistence.*;

@Entity
@Table(name = "Article")
@NamedQuery(name = "find all articles", query = "SELECT ar FROM ArticleEntity ar")
public class ArticleEntity implements Serializable{
    
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    @Id
    private int code; 
    private String designation;
    private float prixHT;
    private float prixTTC;
    private float marge;
    private float remise;
    private float tva;
    
    public int getCode() {
        return code;
    }
    
    public String getDesignation() {
        return designation;
    }
    
    public float getMarge() {
        return marge;
    }
    
    public float getPrixHT() {
        return prixHT;
    }
    
    public float getPrixTTC() {
        return prixTTC;
    }
    
    public float getRemise() {
        return remise;
    }
    
    public float getTva() {
        return tva;
    }

    public void setCode(int code) {
        this.code = code;
    }
        
    public void setDesignation(String designation) {
        this.designation = designation;
    }
    
    public void setMarge(float marge) {
        this.marge = marge;
    }
    
    public void setPrixHT(float prixHT) {
        this.prixHT = prixHT;
    }
    
    public void setPrixTTC(float prixTTC) {
        this.prixTTC = prixTTC;
    } 
    
    public void setRemise(float remise) {
        this.remise = remise;
    }
    
    public void setTva(float tva) {
        this.tva = tva;
    }

}

and this is the entity manager of my class:

package DAO;
import java.util.*;
import javax.persistence.*;
import Models.ArticleEntity;

public class ArticleORM {
    
    private EntityManagerFactory articleMnagerFactory = Persistence.createEntityManagerFactory("JsfDatabase");
    private EntityManager articleManager;
    
    public ArticleORM() {
        this.articleManager = this.articleMnagerFactory.createEntityManager();
    }
    
    public List<ArticleEntity> findAll(){
        this.articleManager.getTransaction().begin();
        Query query = this.articleManager.createQuery("SELECT ar FROM ArticleEntity ar");
        return (List<ArticleEntity>) query.getResultList();
    }

}


and this is my persistence.xml file:

<persistence version="2.1" 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_1.xsd">
    <persistence-unit name="JsfDatabase" transaction-type="RESOURCE_LOCAL">
        <class>Models.ArticleEntity</class>
        <properties>
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/JsfDatabase"/>
            <property name="javax.persistence.jdbc.user" value="root"/>
            <property name="javax.persistence.jdbc.password" value=""/>
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
        </properties>
    </persistence-unit>
</persistence>

this is the error thrown in the console

SEVERE: Servlet.service() for servlet [Faces Servlet] in context with path [/AppJSF] threw exception [An exception occurred while creating a query in EntityManager: 
Exception Description: Problem compiling [SELECT ar FROM ArticleEntity ar]. 
[15, 28] The abstract schema type 'ArticleEntity' is unknown.] with root cause
Local Exception Stack: 
Exception [EclipseLink-0] (Eclipse Persistence Services - 2.7.6.v20200131-b7c997804f): org.eclipse.persistence.exceptions.JPQLException
Exception Description: Problem compiling [SELECT ar FROM ArticleEntity ar]. 
[15, 28] The abstract schema type 'ArticleEntity' is unknown.
    at org.eclipse.persistence.internal.jpa.jpql.HermesParser.buildException(HermesParser.java:157)
    at org.eclipse.persistence.internal.jpa.jpql.HermesParser.validate(HermesParser.java:349)
    at org.eclipse.persistence.internal.jpa.jpql.HermesParser.populateQueryImp(HermesParser.java:280)
    at org.eclipse.persistence.internal.jpa.jpql.HermesParser.buildQuery(HermesParser.java:165)
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:142)
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:118)
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.<init>(EJBQueryImpl.java:104)
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.<init>(EJBQueryImpl.java:88)
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1749)
    at DAO.ArticleORM.findAll(ArticleORM.java:17)
    at Beans.MBArticle.selectAll(MBArticle.java:65)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:568)
    at javax.el.BeanELResolver.invoke(BeanELResolver.java:147)
    at javax.el.CompositeELResolver.invoke(CompositeELResolver.java:79)
    at org.apache.el.parser.AstValue.getValue(AstValue.java:158)
    at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:189)
    at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:109)
    at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194)
    at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:182)
    at javax.faces.component.UIData.getValue(UIData.java:731)
    at org.primefaces.component.api.UIData.getDataModel(UIData.java:768)
    at javax.faces.component.UIData.getRowCount(UIData.java:356)
    at org.primefaces.component.datatable.DataTableRenderer.encodeTbody(DataTableRenderer.java:1085)
    at org.primefaces.component.datatable.DataTableRenderer.encodeTbody(DataTableRenderer.java:1064)
    at org.primefaces.component.datatable.DataTableRenderer.encodeRegularTable(DataTableRenderer.java:426)
    at org.primefaces.component.datatable.DataTableRenderer.encodeMarkup(DataTableRenderer.java:361)
    at org.primefaces.component.datatable.DataTableRenderer.encodeEnd(DataTableRenderer.java:94)
    at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:875)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1764)
    at javax.faces.render.Renderer.encodeChildren(Renderer.java:168)
    at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:845)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1757)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1760)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1760)
    at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:399)
    at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131)
    at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:121)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:594)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:227)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:197)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:135)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:687)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:360)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:399)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:889)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1743)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191)
    at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.base/java.lang.Thread.run(Thread.java:833) ```

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Does this answer your question? [Do I need elements in persistence.xml?](https://stackoverflow.com/questions/1780341/do-i-need-class-elements-in-persistence-xml) – XtremeBaumer Apr 22 '22 at 12:14
  • How do you package the app? in a WAR? – Simon Martinelli Apr 22 '22 at 12:38
  • @SimonMartinelli it's still in test mode didn't deploy it yet – Aouled issa Yousri Apr 22 '22 at 13:08
  • @XtremeBaumer do i need to package my app in a jar file and reference it in tag – Aouled issa Yousri Apr 22 '22 at 13:12
  • Unless you outsource the models to a separate module, no, you dont have to reference it in tag – XtremeBaumer Apr 22 '22 at 13:28
  • @XtremeBaumer sorry but the link you provided didn't help me find a solution to my problem – Aouled issa Yousri Apr 22 '22 at 14:21
  • @XtremeBaumer i tried using Criteria query API instead of JPQL and it says that my ArticleEntity class is not referenced in the persistence.xml when i actually referenced it inside a tag – Aouled issa Yousri Apr 22 '22 at 15:17
  • Turn on logging and see what gets logged when it loads and deploys the persistence unit (on the createEntityManagerFactory and createEntityManager calls), as it should tell you quite a bit of detail on what and where it gets things from if loging is set to finest ( see https://wiki.eclipse.org/EclipseLink/Examples/JPA/Logging for how to turn on logging in EclipseLink). This is common when old persistence.xml files get left on the class path though, so check and remove any from your build to make sure they don't interfere, so that it loads the one you think it should be loading. – Chris Apr 22 '22 at 16:33
  • why are you starting a transaction in the findAll method and just leaving it? You don't need transactions for queries. – Chris Apr 22 '22 at 16:37
  • @Chris it says that the Models.ArticleEntity is compiled with an unsupported JDK – Aouled issa Yousri Apr 22 '22 at 19:36
  • @Chris it's working fine now i managed to fix it. It seems that the entity is compiled using jdk 17 so i downgraded the version to 14 and now everything works fine – Aouled issa Yousri Apr 22 '22 at 20:24

0 Answers0