Up to this point I have had a fully functional NetBeans Platform application with a single screen that utilized Shipvia entity class through the following code:
import entity.Shipvia;
import entity.Route;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Persistence;
import javax.persistence.Query;
public class RetrieveResultList {
public static List RetrieveResultList (String tablename){
EntityManager entityManager = Persistence.createEntityManagerFactory("EntityLibraryPU").createEntityManager();
System.out.println("NAMED QUERY:>"+tablename+".findAll");
Query query = entityManager.createNamedQuery(tablename+".findAll");
List<Shipvia> resultList = query.getResultList();
return resultList;
}
}
As you can see I create a dynamic query based on the table name passed. So if the user opens a ShipviaTopComponent, it will call Shipvia.findAll (see part of entity class below).
package entity;
import java.io.Serializable;
import javax.persistence.*;
import javax.xml.bind.annotation.XmlRootElement;
@Entity
@Table(name = "SHIPVIA")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Shipvia.findAll", query = "SELECT c FROM Shipvia c")
});
My goal is to have a second screen, called RouteTopComponent utilize Route entity class
package entity;
import java.io.Serializable;
import javax.persistence.*;
import javax.xml.bind.annotation.XmlRootElement;
@Entity
@Table(name = "ROUTE")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Route.findAll", query = "SELECT c FROM Route c")
});
However, here is the error I am getting when I try to open the Route screen:
SEVERE [global]
java.lang.IllegalArgumentException: NamedQuery of name: Route.findAll not found.
at org.eclipse.persistence.internal.jpa.EJBQueryImpl.getDatabaseQueryInternal(EJBQueryImpl.java:577)
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createNamedQuery(EntityManagerImpl.java:1043)
at com.demo.viewer.RetrieveResultList.RetrieveResultList(RetrieveResultList.java:23)
at com.demo.viewer.RoutesTopComponent.<clinit>(RoutesTopComponent.java:40)
Caused: java.lang.ExceptionInInitializerError
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
Line 40 of RouteTopComponent is:
List<Route> resultList = RetrieveResultList.RetrieveResultList("Route");
It puzzles me as to why it works fine opening ShipviaTopComponent and finds Shipvia.findAll, but not RouteTopComponent/Route.findAll ?
As discussed in the comments, you have to list the entity classes explicitly in the persistence.xml file if you are using RESOURCE_LOCAL transaction-type/ running JPA in Java SE environment.