在使用JPA作为ORM实现RESTful Web服务时获取错误“异常描述:缺少描述符”

时间:2014-02-14 10:28:41

标签: java web-services rest jpa eclipselink

当我在服务器上部署war文件并尝试访问我的服务时,我收到了此错误 服务器:Glass Fish Server - 开源版

我的门面代码

@GET
@Path("/Retrieve")
@Produces(MediaType.APPLICATION_XML)
public  Response test(@QueryParam("inputString") String Where,@QueryParam("class") String className) throws Exception{

    String body=null;
    Crud<?,?> jobject=Utility.objForm(className);

    if(jobject == null){
        System.out.println("Table not found");
    }
    body=jobject.read(Where);

    return Response.status(Status.OK).entity(body).build();
    }

Utility.objForm方法

public static Crud<?,?> objForm(String table)throws Exception
{
    ClassName tables=ClassName.valueOf(table);
    try{ 

     switch(tables)
     {

     case Location:return (new Crud<TTlsLocation,Location_Wrapper>(TTlsLocation.class,"t_tls_location",new TTlsLocation.Location_Wrapper(),new TTlsLocation()));
     case Country:return (new Crud<TTlsCountry,Country_Wrapper>(TTlsCountry.class,"t_tls_country",new TTlsCountry.Country_Wrapper(),new TTlsCountry()));
     case MassRetrieve:return (new Crud<Mass_Retrive,Mass_Retrive_Wrapper>(Mass_Retrive.class,"Mass_Retrive",new Mass_Retrive.Mass_Retrive_Wrapper(),new Mass_Retrive()));
     case CostCentre:return (new Crud<CostCentre,CostCentre_Wrapper>(CostCentre.class,"CostCentre",new CostCentre.CostCentre_Wrapper(),new CostCentre()));
     case Program:return (new Crud<Program,Program_Wrapper>(Program.class,"Program",new Program.Program_Wrapper(),new Program()));
     default:return (null);
     }
    }
    catch(Exception e)
    {return(null);}
}

我的jobject.read(String Where)crud类中的方法

   public class Crud<T1,T2>{

    private final Class<T1> type;
private final Object root;
private final Object ent;
    Method SLR;
Constructor ct2,ct1;
String dClass,dDb;
 private static final EntityManagerFactory emf=Persistence.createEntityManagerFactory("TLS");

JAXBContext jc;
Marshaller marshaller;
public Crud(Class<T1> type, String dClass, Object root, Object ent)throws Exception {
    this.type = type;
    this.root = root;
    this.ent = ent;
    this.dClass=dClass;

    SLR=(root.getClass()).getDeclaredMethod("setListOfRec", List.class );

    ct2=(root.getClass()).getDeclaredConstructor();
    ct1=(ent.getClass()).getDeclaredConstructor();

    jc = JAXBContext.newInstance(root.getClass());
    marshaller = jc.createMarshaller();

}

//Generic Function to retrieve Specific Table or Database View Data

@SuppressWarnings("unchecked")
public String read(String Where)
{   
    EntityManager em = emf.createEntityManager();
    if(em.isOpen()){
        System.out.println("EntityManager is Working");
    }

    try{

        String query = "SELECT * FROM " + dClass + " " + Where ;
        System.out.println(query);
    List<T1> mylist =(List<T1>)em.createNativeQuery(query,type).getResultList();
    System.out.print("query executed");
    StringWriter writer = new StringWriter();
    try
    {
          T2 emp;
          emp = (T2) ct2.newInstance();
          SLR.invoke(emp, mylist);
          marshaller.marshal(emp, writer);
          return(writer.toString());
      }
      catch(Exception e)
      {
          writer.write(e.toString());
        return(writer.toString());
      }
    }finally{
        em.close();

    }
}

我的persistance.xml文件

 <?xml version="1.0" encoding="UTF-8"?>

 <persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" 

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 

    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence 

    http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">

<persistence-unit name="TLS-1.0" transaction-type="RESOURCE_LOCAL">
    <class>model.TTlsStGroupCode</class>
    <class>model.TTlsProjectFunding</class>
    <class>model.TTlsN3Info</class>
    <class>model.TTlsLocation</class>
    <class>model.TTlsData</class>
    <class>model.TTlsCountry</class>
    <class>model.TTlsCensus</class>
    <class>model.TTlsActivityAtStGroup</class>
    <class>model.TRefTravelStatus</class>
    <class>model.Program</class>
    <class>model.Mass_Retrive</class>
    <class>model.CostCentre</class>
    <properties>
       <property name="javax.persistence.jdbc.password"value="MYPASS"/>
               <property name="javax.persistence.jdbc.url" value="jdbc:mysql://HOSTNAME:PORT/db"/>
           <property name="javax.persistence.jdbc.user" value="user"/>          
    </properties>
</persistence-unit>

错误:

javax.servlet.ServletException: Exception [EclipseLink-6007] (Eclipse Persistence 

Services - 2.3.0.v20110604-r9504): org.eclipse.persistence.exceptions.QueryException

Exception Description: Missing descriptor for [class Entity.TTlsLocation].

Query: ReadAllQuery(referenceClass=TTlsLocation sql="SELECT * FROM t_tls_location ")

1 个答案:

答案 0 :(得分:0)

你有一个名为model.TTlsLocation的类或一个类Entity.TTlsLocation吗? persistence.xml(不是persistance.xml)文件提到前者,后者例外。它是哪一个?