带有OpenJPA的JPA给出了ArgumentException

时间:2015-11-10 10:11:46

标签: jpa ejb openjpa tomee

以下是我的Bean

package com.dunkul.entity;

import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="books")
public class Book implements Serializable{

   private int id;
   private String name;

   public Book(){        
   }

   @Id
   @GeneratedValue(strategy= GenerationType.IDENTITY)
   @Column(name="id")
   public int getId() {
      return id;
   }

   public void setId(int id) {
      this.id = id;
   }

   public String getName() {
      return name;
   }

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

//-------------------------------------------------

实体类

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    version="2.0">

    <persistence-unit name="EjbComponentPU" transaction-type="JTA">
        <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
        <jta-data-source>TestDS</jta-data-source>
        <class>com.dunkul.entity.Book</class>
    </persistence-unit>
 </persistence>


//--------------------------------------------------------------

persistance.xml

    package com.dunkul.client;

    import java.util.Hashtable;
    import java.util.Iterator;
    import java.util.List;

    import javax.naming.Context;
    import javax.naming.InitialContext;

    import com.dunkul.entity.Book;
    import com.dunkul.stateless.LibraryPersistentBeanRemote;;



    public class ClientEJB {

        LibraryPersistentBeanRemote lib;
        public static void main(String[] args) throws Exception {
            new ClientEJB().callEjb();
        }
        private void callEjb() throws Exception {
            // TODO Auto-generated method stub
            Hashtable<String, String> ctxProps = new Hashtable<String, String>();
            ctxProps.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.client.RemoteInitialContextFactory");
            ctxProps.put(Context.PROVIDER_URL, "http://localhost:8082/tomee/ejb");
            ctxProps.put(Context.URL_PKG_PREFIXES,"org.apache.naming");
            ctxProps.put("openejb.descriptors.output", "true");
            InitialContext context = new InitialContext(ctxProps);
            lib = (LibraryPersistentBeanRemote)context.lookup("LibraryPersistentBeanRemote");
            String bookName = "Sonali";

            Book newBook = new Book();
        //  newBook.setId(1);
        //  newBook.setName(bookName);
        //  lib.addBook(newBook);

            List ls = lib.getBooks();
            for (Iterator iterator = ls.iterator(); iterator.hasNext();) {
                Object object = (Object) iterator.next();
                System.out.println((String)object);

            }

        }

    }

I am getting following error

Client.java

{{1}}

SEVERE:EjbTransactionUtil.handleSystemException:“遇到”来自“字符1,但预期:[”DELETE“,”SELECT“,”UPDATE“]。”在解析JPQL“From Book”时。请参阅嵌套堆栈跟踪  原始解析错误。  org.apache.openjpa.persistence.ArgumentException:“遇到”来自“at character 1,but expected:[”DELETE“,”SELECT“,”UPDATE“]。”而 解析JPQL“From Book”。有关原始解析错误,请参阅嵌套堆栈跟踪。

1 个答案:

答案 0 :(得分:1)

"From Book"

无效 JPQL(JPA提供的查询语言)。

您使用的语法是“Hibernate HQL”,对JPA无效。毋庸置疑,Hibernate文档并没有做太多努力使这一点变得清晰,而且很多使用过Hibernate的人认为这个HQL是所有JPA实现都应该支持的。

兼容的JPA实现(例如OpenJPA)使用JPQL(正如任何体面的JPA教程所说)。有效的JPQL类似于

SELECT b FROM Book b