Java 持久化对象无法从 persistence.xml 中找到持久化单元
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4435124/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Persistence object cannot find persistence unit from persistence.xml
提问by Meow
Environment: Windows 7, NetBean 6.9 (the one including GlassFish v3, Java EE 6), MySQL server
环境:Windows 7、NetBean 6.9(包括GlassFish v3、Java EE 6)、MySQL服务器
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)
我已经在 MySQL 数据库中创建了表,并通过右键单击项目并选择“ create entity from database
”来使用 NetBean 的功能(如果措辞有误,很抱歉,因为我的 NetBean 是日语)
This will create Entities.
这将创建实体。
Now I went to test if I can access DB through Entity Manager.
现在我去测试我是否可以通过实体管理器访问数据库。
Tmp.java
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>
When I run Tmp.java, I can see it fails at:
当我运行 Tmp.java 时,我可以看到它在以下位置失败:
factory = Persistence.createEntityManagerFactory(WEB_PU);
Error msg:
错误消息:
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)
I've checked the spelling of persistence unit "WebApplication1PU" in test code matches with with persistence.xml and it is correct.
我检查了测试代码中持久性单元“WebApplication1PU”的拼写与persistence.xml 匹配,并且它是正确的。
persistence.xml is located ... it's in Japanese I don't know how in English :(
persistence.xml 位于...它是日文的,我不知道英文如何:(
Let me try..
让我试试..
project
|-------web page
|-------test package
|-------lib
|-------enterprise bean
|-------*** file //blah file, I can't translate..
|-----MNIFEST.MF
|-----faces-cofig.xml
|-----persistence.xml //HERE!!
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?
由于失败是在工厂尝试在persistence.xml 中定位持久性单元的开始,我完全感到困惑。有什么我应该验证的吗?
please let me know if you need more clarification.
如果您需要更多说明,请告诉我。
UPDATE
更新
I've tried suggested possible solutions but no luck.. Same error message is returned. Yes, netbean 6.9 default provider is toplink.
我已经尝试了建议的可能解决方案,但没有运气.. 返回相同的错误消息。是的,netbean 6.9 默认提供程序是 toplink。
What I did:
我做了什么:
Added provider to persistence.xml
向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 that I executes from netbean is located at:
我从 netbean 执行的 Tmp.java 位于:
project
|------web page
|------source package
| |-----------local.test.session
|-----------local.test.tmp
|------------------Tmp.java //HERE
I'm not sure if it matters though.
我不确定这是否重要。
UPDATE 2
更新 2
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.
我已将包含 toplink-essential.jar 和 toplink-essential-agent.jar 的 toplink lib 添加到我的项目的 lib 目录中,现在我在原始错误之上遇到了新错误,但我相信我越来越近了。
Looks like version attribute in persistence.xml has an issue...???
看起来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)
UPDATE 3
更新 3
After some research, find out version number needs to be 1.0 compliant so did I applied the fix and got new error.
经过一番研究,发现版本号需要符合 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>
It's weird because I already specified class in persistence.xml above :(
这很奇怪,因为我已经在上面的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
UPDATE 4
更新 4
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.
我不认为这被称为“已解决”,但经过长时间的尝试,我决定只在 Web 应用程序中运行我的测试代码,而不是从 netbean 单独运行。我必须自己创建 web.xml,因为 GlassFish 不提供它。(我看到 sun-web.xml 但它不是替代品)然后创建 servlet,在 web.xml 中定义映射并将我的测试代码片段添加到 servlet 并在浏览器上尝试。它起作用了,成功地从数据库中获取了数据。
I wonder what is the reason why it wasn't working when I try running just the Tmp.java (my test file) from Netbean...
我想知道当我尝试仅从 Netbean 运行 Tmp.java(我的测试文件)时它不起作用的原因是什么......
采纳答案by javamonkey79
I think you may need to specify your persistence provider. See here.
我认为您可能需要指定您的持久性提供程序。见这里。
For example:
例如:
<provider>org.hibernate.ejb.HibernatePersistence</provider>
or
或者
<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.
您的“原始”持久性单元依赖于服务器来连接提供者等细节,但是当您在服务器上下文之外运行它时,您需要指定一些内容 - 其中之一是提供者。
See here.
见这里。
回答by Mohamed Saligh
Please specify the <provider>
for your <persistence-unit>
:
请<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>
And make your persistence-unit
name is correct and file path is also in correct context path.
并使您的persistence-unit
名称正确并且文件路径也在正确的上下文路径中。
回答by sofend
I imagine the problem is this line:
我想问题是这一行:
<jta-data-source>masatoJNDI</jta-data-source>
which, unless you're running in a JNDI-enabled context will cause the type of error you initially saw. Try creating a persistence.xml file in your test/resources folder and removing that line in the test version of the xml file.
除非您在启用 JNDI 的上下文中运行,否则将导致您最初看到的错误类型。尝试在您的 test/resources 文件夹中创建一个 persistence.xml 文件,并在 xml 文件的测试版本中删除该行。
回答by Xinyuan.Yan
I think you may need to add a jndi context,you should create a jndi.properties file in your jar, and add the below content in it.
我认为您可能需要添加一个 jndi 上下文,您应该在 jar 中创建一个 jndi.properties 文件,并在其中添加以下内容。
java.naming.factory.initial=bitronix.tm.jndi.BitronixInitialContextFactory