2010-02-12 147 views
2

我想通過GlassFish在EJB中使用JPA創建並運行一個簡單的示例。我有以下persistence.xmlJava持久性問題

<persistence version="1.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_1_0.xsd"> 
    <persistence-unit name="default" transaction-type="RESOURCE_LOCAL"> 
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> 
    <jta-data-source>jdbc/wms</jta-data-source> 
    <class>com.xxx.xxx.datamodel.MyTest</class> 
    <exclude-unlisted-classes /> 
    <properties> 
     <property name="eclipselink.target-server" value="SunAS9"/> 
     <property name="eclipselink.logging.level" value="FINEST"/> 
     <property name="eclipselink.target-database" value="Oracle"/> 

     <property name="eclipselink.jdbc.driver" value="oracle.jdbc.OracleDriver" /> 
     <property name="eclipselink.jdbc.url" value="[dbconnectionstring]" /> 
     <property name="eclipselink.jdbc.user" value="user" /> 
     <property name="eclipselink.jdbc.password" value="password" /> 
    </properties> 
    </persistence-unit> 
</persistence> 

一個簡單的實體:

package com.xxx.xxx.datamodel; 

import javax.persistence.Entity; 
import javax.persistence.Id; 
import javax.persistence.Table; 

@Entity 
@Table(name = "move_task") 
public class MyTest { 

    private int key; 
    private String description; 

    @Id 
    public int getKey(){ 
     return key; 
    } 

    public void setKey(int key){ 
     this.key = key; 
    } 

    public String getDescription(){ 
     return this.description; 
    } 

    public void setDescription(String description){ 
     this.description = description; 
    } 

    @Override 
    public String toString(){ 
     return "Key: " + key + " Description: " + description; 
    } 

} 

最後下面的代碼嘗試使用上面:

private void jpaCall() { 
     try{ 
      emf = Persistence.createEntityManagerFactory("default"); 
      em = emf.createEntityManager(); 
      log.info("JPA init complete"); 

      final List<MyTest> list = em.createQuery("select p from MyTest p").getResultList(); 

      for (MyTest current : list) { 
       final String description = current.getDescription(); 
       log.info("JPA: Desc: " + description); 
      } 

     } 
     catch(Exception e){ 
      log.error("Error on JPA", e); 
     } 

    } 

當這個運行我的EJB的一部分初始化我得到以下錯誤:

java.lang.IllegalArgumentException: An exception occurred while creating a query in EntityManager: 
Exception Description: Error compiling the query [select p from MyTest p]. Unknown entity type [MyTest]. 
... 
Caused by: Exception [EclipseLink-8034] (Eclipse Persistence Services - 2.0.0.v20091127-r5931): org.eclipse.persistence.exceptions.JPQLException 
Exception Description: Error compiling the query [select p from MyTest p]. Unknown entity type [MyTest]. 

我不知道我做錯了什麼,並會感謝任何幫助。

乾杯,

詹姆斯

+0

你如何包裝所有這一切?你把你的persistence.xml放在哪裏? – 2010-02-12 13:57:13

+0

它從日食中部署到glassfish。 persistence.xml位於EJB的META-INF目錄中。 – James 2010-02-12 14:18:19

+0

我知道這是找到的persistence.xml,因爲如果我改變它有一個無效的供應商,我得到此時,相應的例外: org.eclipse.persistence.jpa.PersistenceProviderjhjh javax.persistence.PersistenceException:沒有持久性提供者爲EntityManager命名爲默認 – James 2010-02-12 14:45:03

回答

0

因此,作爲上述評論表明這似乎是在爲GlassFish的Eclipse插件的問題。手動部署耳朵時沒有問題。

謝謝大家的幫助。

James

+0

請報告錯誤https://glassfishplugins.dev.java.net/issues/ :) – 2010-02-17 16:08:40

+0

完成:https://glassfishplugins.dev.java.net/issues/show_bug.cgi?id = 307 – James 2010-02-17 17:18:05