2010-12-14 100 views
3

環境找到持久化單元:Windows 7中,NetBean 6.9(一個包括GlassFish的第三版,Java EE 6的),MySQL服務器持久性對象不能從persistence.xml中

我創建了一個MySQL數據庫表和使用NetBean的能力通過右鍵單擊該項目並選擇「create entity from database」(對不起,如果措辭不對,因爲我的NetBean是日文版)

這將創建實體。

現在我去測試我是否可以通過實體管理器訪問數據庫。

Tmp.java

package local.test.tmp; 
import Resources.Users; 
import javax.persistence.EntityManagerFactory; 
import javax.persistence.Persistence; 
import javax.persistence.EntityManager; 
import javax.persistence.Query; 

import java.util.List; 
/** 
* 
* @author m-t 
*/ 
public class Tmp { 

    private static EntityManagerFactory factory; 
    private static final String WEB_PU = "WebApplication1PU"; 

    public static void main(String args[]) { 
     factory = Persistence.createEntityManagerFactory(WEB_PU); 
     EntityManager em = factory.createEntityManager(); 
     //read existing entries and write to concole 
     Query q = em.createQuery("select * from users"); 

     List<Users> userList = q.getResultList(); 
     for(Users u : userList) { 
      System.out.println(u); 
     } 
    } 
} 

的persistence.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"> 
    <!-- orignal 
    <persistence-unit name="WebApplication1PU" transaction-type="JTA"> 
    <jta-data-source>masatoJNDI</jta-data-source> 
    <properties/> 
    </persistence-unit> 
    --> 

    <persistence-unit name="WebApplication1PU" transaction-type="JTA"> 
    <jta-data-source>masatoJNDI</jta-data-source> 
    <properties> 
     <property name="toplink.jdbc.user" value="masato" /> 
     <property name="toplink.jdbc.password" value="foobar" /> 
    </properties> 
    </persistence-unit> 
</persistence> 

當我運行Tmp.java,我可以看到它失敗的:

factory = Persistence.createEntityManagerFactory(WEB_PU); 

錯誤消息:

Exception in thread "main" javax.persistence.PersistenceException: No Persistence 
provider for EntityManager named WebApplication1PU 
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:84) 
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54) 
at local.test.tmp.Tmp.main(Tmp.java:24) 

我檢查持久性單元「WebApplication1PU」的測試代碼拼寫用的persistence.xml相匹配,並且它是正確的。

的persistence.xml所在......這是在日本我不知道如何用英語:(

讓我來試試..

project 
|-------web page 
|-------test package 
|-------lib 
|-------enterprise bean 
|-------*** file //blah file, I can't translate.. 
       |-----MNIFEST.MF 
       |-----faces-cofig.xml 
       |-----persistence.xml //HERE!! 

由於故障是在開始在那裏廠試圖在persistence.xml中查找持久化單元,我完全糊塗了。有什麼我要驗證?

請讓我知道如果你需要更多的澄清。

UPDATE

我試過建議可能的解決方案,但沒有運氣..返回相同的錯誤消息。 是的,netbean 6.9默認提供商是toplink。

我做了什麼:

新增提供商persistence.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="NoJSFPU" transaction-type="JTA"> 
    <provider>oracle.toplink.essentials.PersistenceProvider</provider> 
    <jta-data-source>masatoJNDI</jta-data-source> 

    <properties> 
     <property name="toplink.jdbc.url" value="jdbc:mysql://localhost:3306/mytest"/> 
     <property name="toplink.jdbc.driver" value="com.mysql.jdbc.Driver"/> 
     <property name="toplink.jdbc.user" value="masato" /> 
     <property name="toplink.jdbc.password" value="mocha123" /> 
    </properties> 
    </persistence-unit> 
</persistence> 

好像大多數的人正在與我的情況成功。我現在開始考慮如何運行測試文件或文件位於何處可能存在問題?

Tmp.java,我從netbean執行位於:

project 
|------web page 
|------source package 
|   |-----------local.test.session 
      |-----------local.test.tmp 
          |------------------Tmp.java //HERE 

我不知道,如果它雖然很重要。

更新2

我已經添加了排名靠前的lib含TopLink的Essential.jar中和TopLink的必要agent.jar中我的項目的lib目錄,現在我之上得到新的錯誤原來的一個,但我相信我越來越近了。

看起來像persistence.xml中的版本屬性有問題... ???

Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named WebApplication1PU: Provider named oracle.toplink.essentials.PersistenceProvider threw unexpected exception at create EntityManagerFactory: 
oracle.toplink.essentials.exceptions.PersistenceUnitLoadingException 
Local Exception Stack: 
Exception [TOPLINK-30005] (Oracle TopLink Essentials - 2.0.1 (Build b09d-fcs (12/06/2007))): oracle.toplink.essentials.exceptions.PersistenceUnitLoadingException 
Exception Description: An exception was thrown while searching for persistence archives with ClassLoader: [email protected] 
Internal Exception: Exception [TOPLINK-30004] (Oracle TopLink Essentials - 2.0.1 (Build b09d-fcs (12/06/2007))): oracle.toplink.essentials.exceptions.PersistenceUnitLoadingException 
Exception Description: An exception was thrown while processing persistence.xml from URL: file:/C:/Users/m-takayashiki/Documents/NetBeansProjects/NoJSF/build/web/WEB-INF/classes/ 
Internal Exception: 
(1. cvc-complex-type.3.1: Value '2.0' of attribute 'version' of element 'persistence' is not valid with respect to the corresponding attribute use. Attribute 'version' has a fixed value of '1.0'.) 
     at oracle.toplink.essentials.exceptions.PersistenceUnitLoadingException.exceptionSearchingForPersistenceResources(PersistenceUnitLoadingException.java:143) 
     at oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider.createEntityManagerFactory(EntityManagerFactoryProvider.java:169) 
     at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:110) 
     at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:83) 
     at local.test.tmp.Tmp.main(Tmp.java:24) 

更新3

經過一番研究,發現版本號必須是1.0兼容所以我才修補程序應用,得到了新的錯誤。

<?xml version="1.0" encoding="UTF-8"?> 
<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="NoJSFPU" transaction-type="JTA"> 
    <provider>oracle.toplink.essentials.PersistenceProvider</provider> 
    <jta-data-source>masatoJNDI</jta-data-source> 
    <class>local.test.session.Addresses</class> 
    <class>local.test.session.Users</class> 
    <properties> 
     <property name="toplink.jdbc.url" value="jdbc:mysql://localhost:3306/mytest"/> 
     <property name="toplink.jdbc.driver" value="com.mysql.jdbc.Driver"/> 
     <property name="toplink.jdbc.user" value="masato" /> 
     <property name="toplink.jdbc.password" value="mocha123" /> 
    </properties> 
    </persistence-unit> 
</persistence> 

,因爲我前面已經:(

Exception in thread "main" javax.persistence.PersistenceException: Exception [TOPLINK-7060] 
(Oracle TopLink Essentials - 2.0.1 (Build b09d-fcs (12/06/2007))): oracle.toplink.essentials.exceptions.ValidationException 
Exception Description: Cannot acquire data source [masatoJNDI]. 
Internal Exception: javax.naming.NoInitialContextException: Need to specify class name in 
environment or system property, or as an applet parameter, or in an application resource 
file: java.naming.factory.initial 

更新persistence.xml中指定的類這很奇怪4

我不認爲這就是所謂的 「解決」,但不久後嘗試,我決定在web應用中運行我的測試代碼,而不是單獨從netbean運行,因爲GlassFish沒有提供它,所以我必須自己創建web.xml(我看到sun-web.xml,但它不是替換)然後創建servlet,定義ma在web.xml中添加我的測試代碼片段並將其添加到servlet並在瀏覽器上嘗試。它的工作,成功地從數據庫中獲得數據。

我不知道究竟是爲什麼,當我嘗試從Netbean運行只是Tmp.java(我的測試文件)不工作我想你可能需要指定你的持久性提供了理由...

+1

你的文件位於哪裏? persistence.xml應該在classpath的meta-inf – Steven 2010-12-14 01:33:00

+0

可能的重複中:http://stackoverflow.com/questions/1158159/no-persistence-provider-for-entitymanager-named – javamonkey79 2010-12-14 02:02:53

回答

2

。見here

例如:

<provider>org.hibernate.ejb.HibernatePersistence</provider> 

<provider>oracle.toplink.essentials.PersistenceProvider</provider> 

你的「原始」的持久性單元依靠服務器導線上,如供應商的詳細信息,但是當你的服務器環境之外運行你需要指定一些東西 - 其中一個是提供者。

請參閱here

+1

@masato-san你可能還需要添加將持久性管理器的jar文件添加到您的類路徑中。如果我沒有記錯的話,Glassfish會自帶toplink。這僅僅是因爲你在應用服務器之外運行它。如果你通過一個應用程序來完成與你給予和部署的課程相同的課程,我敢打賭它會起作用。 – 2010-12-14 02:17:52

+0

@Jschoen:你知道這個jar的名字是什麼嗎?我在我的項目的lib目錄中查找了GlasshFish服務器3目錄,它具有javax.persistence.jar,但我不認爲這是你所說的。 – 2010-12-14 02:54:12

+1

@ masato-san我認爲這只是toplink.jar,但你可能需要其他人。這裏有一個列表http://download.oracle.com/docs/cd/B32110_01/web.1013/b28217/install.htm#CHDDABJI。你也可以從那裏下載它們。 – 2010-12-14 03:02:17

5

請您<persistence-unit>指定<provider>

<!-- EclipseLink --> 
<provider> 
    org.eclipse.persistence.jpa.PersistenceProvider 
</provider> 

<!-- Hibernate --> 
<provider> 
    org.hibernate.ejb.HibernatePersistence 
</provider> 

<!-- Toplink --> 
<provider> 
oracle.toplink.essentials.PersistenceProvider 
</provider> 

,讓你persistence-unit名稱是否正確,文件路徑也是正確的上下文路徑。

+0

感謝您的提示! – 2010-12-14 06:23:57

0

我想象中的問題是這一行:

<jta-data-source>masatoJNDI</jta-data-source> 

它,除非你在啓用JNDI的上下文中運行將導致你最初看到錯誤的類型。嘗試在test/resources文件夾中創建一個persistence.xml文件,並在xml文件的測試版本中刪除該行。

0

我想你可能需要添加一個jndi上下文,你應該在你的jar中創建一個jndi.properties文件,並在其中添加下面的內容。