環境找到持久化單元: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(我的測試文件)不工作我想你可能需要指定你的持久性提供了理由...
你的文件位於哪裏? persistence.xml應該在classpath的meta-inf – Steven 2010-12-14 01:33:00
可能的重複中:http://stackoverflow.com/questions/1158159/no-persistence-provider-for-entitymanager-named – javamonkey79 2010-12-14 02:02:53