持久性对象无法从persistence.xml中找到持久性单元

时间:2010-12-14 01:28:48

标签: java jpa netbeans-6.9

环境:Windows 7,NetBean 6.9(包括GlassFish v3,Java EE 6),MySQL服务器

我已经在MySQL数据库中创建了表,并通过右键单击项目并选择“create entity from database”来使用NetBean的功能(对不起,因为我的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!!

由于失败是在Factory尝试在persistence.xml中找到持久性单元的开始,我完全感到困惑。有什么我应该验证的吗?

如果您需要更多说明,请告诉我。

更新

我尝试过建议的可行解决方案,但没有运气..返回相同的错误消息。 是的,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>

似乎大多数人都对我的情况有所成功。我现在开始认为可能存在运行测试文件或文件所在位置的问题?

我从netbean执行的Tmp.java位于:

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

我不确定这是否重要。

更新2

我已将包含toplink-essential.jar和toplink-essential-agent.jar的toplink lib添加到我项目的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: sun.misc.Launcher$AppClassLoader@f6a746
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>

这很奇怪,因为我已经在上面的persistence.xml中指定了类:(

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

更新4

我不认为这被称为“已解决”,但经过长时间的尝试,我决定只在网络应用中运行我的测试代码,而不是从netbean单独运行。我必须自己创建web.xml,因为GlassFish不提供它。 (我看到了sun-web.xml,但它不是替换)然后创建了servlet,在web.xml中定义了映射,并将我的测试代码片段添加到servlet并在浏览器上尝试了。它工作成功,从数据库中获取数据。

我想知道当我尝试从Netbean运行Tmp.java(我的测试文件)时它无法工作的原因是什么......

4 个答案:

答案 0 :(得分:5)

请为<provider>

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

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

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

并使您的persistence-unit名称正确,文件路径也在正确的上下文路径中。

答案 1 :(得分:2)

我认为您可能需要指定持久性提供程序。见here

例如:

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

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

您的“原始”持久性单元依赖于服务器来连接提供程序等详细信息,但是当您在服务器上下文之外运行它时,您需要指定一些内容 - 其中一个是提供程序。

请参阅here

答案 2 :(得分:0)

我想问题就在这一行:

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

除非您在启用JNDI的上下文中运行,否则将导致您最初看到的错误类型。尝试在test / resources文件夹中创建persistence.xml文件,并在xml文件的测试版本中删除该行。

答案 3 :(得分:0)

我认为您可能需要添加jndi上下文,您应该在jar中创建一个jndi.properties文件,并在其中添加以下内容。

java.naming.factory.initial=bitronix.tm.jndi.BitronixInitialContextFactory