持久性对象无法从persistence.xml中找到持久性单元 [英] Persistence object cannot find persistence unit from persistence.xml

查看:119
本文介绍了持久性对象无法从persistence.xml中找到持久性单元的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Environment: Windows 7, NetBean 6.9 (the one including GlassFish v3, Java EE 6), MySQL server

我已经在MySQL数据库中创建了表,并通过右键单击项目并选择"create entity from database"来使用NetBeans的功能(抱歉,因为我的NetBean是日语,所以措辞不正确)

I've created tables in MySQL database and using NetBean's capability by right click on the project and choose "create entity from database" (Sorry if the wording is wrong because my NetBean is in Japanese)

这将创建实体.

现在我去测试是否可以通过实体管理器访问数据库.

Now I went to test if I can access DB through Entity Manager.

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时,我可以看到它在以下位置失败:

When I run Tmp.java, I can see it fails at:

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)

我检查了测试代码中与"persistence.xml"匹配的持久性单元"WebApplication1PU"的拼写,这是正确的.

I've checked the spelling of persistence unit "WebApplication1PU" in test code matches with with persistence.xml and it is correct.

persistence.xml位于...它是日文,我不知道如何用英语:(

persistence.xml is located ... it's in Japanese I don't know how in English :(

让我尝试一下.

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中定位持久性单元的开始,所以我完全感到困惑.我有什么要验证的吗?

Since the failure is at the beginning where Factory tries to locate persistence unit in persistence.xml, I am totally confused. Is there anything I should be verifying?

请让我知道是否需要更多说明.

please let me know if you need more clarification.

更新

我尝试了建议的可能解决方案,但没有运气.返回相同的错误消息. 是的,netbean 6.9的默认提供程序是toplink.

I've tried suggested possible solutions but no luck.. Same error message is returned. Yes, netbean 6.9 default provider is 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>

似乎大多数人都因我的情况而获得成功.我现在开始考虑如何运行测试文件或文件位于何处?

Seems like most of people are being success with my situation. I am now start thinking there may be problem with how I run the test file or where the file is located?

Tmp.java位于:

Tmp.java that I executes from netbean is located at:

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

我不确定这是否重要.

更新2

我已经将包含toplink-essential.jar和toplink-essential-agent.jar的toplink库添加到我的项目的lib目录中,现在在原始目录的顶部出现了新错误,但我相信靠近.

I've added toplink lib containing toplink-essential.jar and toplink-essential-agent.jar to the lib directory of my project and now I'm getting new error on top of original one but I believe I'm getting closer.

persistence.xml中的类似version属性有问题... ???

Looks like version attribute in persistence.xml has an issue...???

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,所以我应用了此修复程序并得到了新的错误.

After some research, find out version number needs to be 1.0 compliant so did I applied the fix and got new error.

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

It's weird because I already specified class in persistence.xml above :(

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

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

I don't think this is called "solved" but after long try, I decided to just run my test code in the web app instead of running individually from netbean. I had to create web.xml myself since GlassFish does not provide it. (I see sun-web.xml but it wasn't the substitution) Then created servlet, define mapping in the web.xml and added my test code snippet to the servlet and tried on the browser. It worked, successfully obtained data from database.

我想从Netbean中仅运行Tmp.java(我的测试文件)时,为什么它不起作用是什么原因?

I wonder what is the reason why it wasn't working when I try running just the Tmp.java (my test file) from Netbean...

推荐答案

我认为您可能需要指定持久性提供程序.请参见此处.

I think you may need to specify your persistence provider. See here.

例如:

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

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

您的原始"持久性单元依靠服务器来连接诸如提供程序之类的详细信息,但是当您在服务器上下文之外运行它时,您需要指定一些内容-其中之一就是提供程序.

Your "original" persistence unit relies on the server to wire in details like the provider, but when you run it outside of the server context you need to specify a few things - one of them being the provider.

请参见此处.

这篇关于持久性对象无法从persistence.xml中找到持久性单元的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆