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

3

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

我已在MySQL数据库中创建了表,并使用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!!

由于工厂尝试在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文件中的version属性有问题...???

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中指定了class :(

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单独运行。由于GlassFish没有提供web.xml,所以我不得不自己创建它。(我看到了sun-web.xml,但它不是替代品) 然后创建Servlet,在web.xml中定义映射,并将我的测试代码片段添加到Servlet中,在浏览器上进行测试。它工作了,成功地从数据库中获取数据。

我想知道为什么当我尝试从Netbean运行只有Tmp.java(我的测试文件)时它不能工作...


1
你的文件存放在哪里?persistence.xml 应该放在类路径的 meta-inf 目录下。 - Steven
可能是重复问题:https://dev59.com/8HM_5IYBdhLWcg3w8IHR - javamonkey79
4个回答

5
请指定您的的:
<!-- EclipseLink -->
<provider>
  org.eclipse.persistence.jpa.PersistenceProvider
</provider>

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

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

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


3

我认为你需要指定你的持久化提供者。请参考这里

例如:

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

或者

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

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

请参见此处


1
@masato-san,您可能还需要将持久性管理器的jar文件添加到类路径中。如果我没记错的话,Glassfish带有Toplink。这仅是因为您正在应用服务器外运行它。如果您编写一个应用程序来执行与您提供的类相同的操作并部署它,我敢打赌它会起作用。 - Jacob Schoen
@Jschoen:你知道持久化管理器的JAR包名称是什么吗?我在我的项目中查看了GlassFish服务器3目录下的lib目录,发现有javax.persistence.jar,但我不认为这就是你所说的。 - Meow
1
@masato-san 我认为只需要 toplink.jar,但你可能需要其他的。这里有一个列表 http://download.oracle.com/docs/cd/B32110_01/web.1013/b28217/install.htm#CHDDABJI。你也可以直接从那里下载它们。 - Jacob Schoen
@Jschoen:嗯,我将 toplink.jar 添加到了我的项目的 lib 目录中,但没有效果...也许我认为正确的东西实际上是错误的。 - Meow

0

我想问题出在这一行:

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

这会导致初始错误类型,除非您在JNDI启用的上下文中运行。尝试在test/resources文件夹中创建一个persistence.xml文件,并删除xml文件的测试版本中的那行代码。


0

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

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接