到目前为止,我已经有了一个功能齐全的 NetBeans 平台应用程序,它只有一个屏幕,通过以下代码利用了 Shipvia 实体类:
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;
}
}
如您所见,我根据传递的表名创建了一个动态查询。因此,如果用户打开一个 ShipviaTopComponent,它将调用 Shipvia.findAll(参见下面的实体类的一部分)。
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")
});
我的目标是有第二个屏幕,称为 RouteTopComponent 利用 Route 实体类
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")
});
但是,这是我在尝试打开 Route 屏幕时遇到的错误:
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)
RouteTopComponent 的第 40 行是:
List<Route> resultList = RetrieveResultList.RetrieveResultList("Route");
我很困惑为什么打开 ShipviaTopComponent 并找到 Shipvia.findAll 工作正常,但没有找到 RouteTopComponent/Route.findAll ?