Java Eclipse 无法识别persistence.xml 的内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3701901/
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
Eclipse does not recognize content of persistence.xml
提问by Mark Baijens
Im getting the following error in eclipse:
我在 Eclipse 中收到以下错误:
The persistence.xml file does not have recognized content.
persistence.xml 文件没有可识别的内容。
My persistence.xml file works great in my application but eclipse keeps giving me this error. I got this after moving the file and updating my project configuration with m2eclipse. I did not change the file itself. Anyone knows how to solve this?
我的persistence.xml 文件在我的应用程序中运行良好,但eclipse 不断给我这个错误。在移动文件并使用 m2eclipse 更新我的项目配置后,我得到了这个。我没有更改文件本身。有谁知道如何解决这个问题?
persistence.xml:
持久性.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="localDB" transaction-type="RESOURCE_LOCAL">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<class>package.Users</class>
<properties>
<!-- enable warnings for debugging -->
<property name="openjpa.Log" value="DefaultLevel=TRACE, Runtime=INFO, Tool=INFO, SQL=TRACE"/>
<!-- connection properties -->
<property name="openjpa.ConnectionURL" value="jdbc:mysql://localhost/test"/>
<property name="openjpa.ConnectionDriverName" value="com.mysql.jdbc.Driver"/>
<property name="openjpa.ConnectionUserName" value="root"/>
<property name="openjpa.ConnectionPassword" value=""/>
</properties>
</persistence-unit>
</persistence>
Update
更新
Looks like a bug in m2eclipse in combination with jpa.
看起来像 m2eclipse 中与 jpa 结合的错误。
https://bugs.eclipse.org/bugs/show_bug.cgi?id=251323
https://bugs.eclipse.org/bugs/show_bug.cgi?id=251323
Temporarily solved by setting the option to show it as a warning instead of an error
通过设置将其显示为警告而不是错误的选项暂时解决
采纳答案by Chris Harris
I fixed the problem the following way (I'm using RAD v8.5.1):
我通过以下方式解决了这个问题(我使用的是 RAD v8.5.1):
- Windows --> Preferences --> Validation
- Scroll down to "JPA Validator" and click on the Ellipses under Settings.
- Click on the "Add Include Group..." button.
- Click on the new Include Group that appeared to highlight it.
- Click on the "Add Rule..." button.
- A dialog menu pops up. Click the "Folder or file name" radio button and click on "Next".
- Click on "Browse Folder..." and navigate to your src directory (in my case, it was "src/main/resources") within your project (my project was a .war in a multi module app) in your workspace.
- Click on "Finish".
- Click on "OK". This will prompt a full build.
- The error will go away.
- Windows --> 首选项 --> 验证
- 向下滚动到“JPA Validator”并单击“设置”下的省略号。
- 单击“添加包含组...”按钮。
- 单击出现的新包含组以突出显示它。
- 单击“添加规则...”按钮。
- 弹出一个对话框菜单。单击“文件夹或文件名”单选按钮,然后单击“下一步”。
- 单击“浏览文件夹...”并导航到工作区中项目(我的项目是多模块应用程序中的 .war)中的 src 目录(在我的情况下,它是“src/main/resources”)。
- 单击“完成”。
- 单击“确定”。这将提示一个完整的构建。
- 错误将消失。
回答by Qwerky
Whereabouts in your project structure is the file located?
您的项目结构中的文件位于何处?
For ref, I have mine in maven-module-name/src/main/resources/META-INF/
对于 ref,我在 maven-module-name/src/main/resources/META-INF/ 中有我的
回答by Pascal Thivent
Hard to say since hard to reproduce (your persistence.xml
is perfectly valid) but maybe try to:
很难说,因为很难复制(你persistence.xml
是完全有效的),但也许尝试:
- Clean the project (both an "Project > Clean" and a Maven
clean
) - Force Eclipse to validate the XML file again (right-click, and select Validate XML file).
- 清理项目(“项目>清理”和 Maven
clean
) - 强制 Eclipse 再次验证 XML 文件(右键单击并选择验证 XML 文件)。
回答by willcodejavaforfood
I've had the same problem on a few occasions and what worked for me was to copy the contents of the file, delete it and create a new persistence.xml file and paste the contents back in.
我有几次遇到同样的问题,对我有用的是复制文件的内容,删除它并创建一个新的 persistence.xml 文件并将内容粘贴回。
回答by jadrake
I figured out what the problem is here.... the JPA Facet is assuming that the META-INF folder is directly under a source folder. If you are using a Maven project, you likely have something like src/main/resources or src/test/resources. If your persistence.xml is under these folders it will complain. If you create a new source folder (call it "jpa") and create a META-INF/persistence.xml in there, then it will find it successfully. (I also had to do a clean/close/open project)
我想出了这里的问题...... JPA Facet 假设 META-INF 文件夹直接位于源文件夹下。如果您正在使用 Maven 项目,您可能有类似 src/main/resources 或 src/test/resources 的内容。如果您的persistence.xml 在这些文件夹下,它会报错。如果您创建一个新的源文件夹(称为“jpa”)并在其中创建一个 META-INF/persistence.xml,那么它将成功找到它。(我还必须做一个清理/关闭/打开项目)
This is certainly a pain.... and I can see many situations this won't work well.
这当然是一种痛苦......而且我可以看到很多情况这不会很好地工作。
回答by HDave
Turning this error into a warning may make the problem go away, but it will not fix the underlying problem with the Eclipse Dali plugin. It will also mean the extremely useful JPA Structure and JPA Details views will not work.
将此错误转换为警告可能会使问题消失,但它不会解决 Eclipse Dali 插件的潜在问题。这也意味着非常有用的 JPA Structure 和 JPA Details 视图将不起作用。
The link offered by OP and the approach of most of the answers here are actually meant to get around a different problem wherein Maven wants to filter the persistence.xml file and thus tells Eclipse to ignore it. A viable work-around for that problem is explained hereand involves removing the exclused: '**' from the src/main/resources entry in the classpath in the project properties.
OP 提供的链接和此处大多数答案的方法实际上是为了解决一个不同的问题,其中 Maven 想要过滤 persistence.xml 文件,从而告诉 Eclipse 忽略它。此处解释了该问题的可行解决方法, 包括从项目属性的类路径中的 src/main/resources 条目中删除 exclused: '**'。
Back to the OP's original question, I had this EXACT same error message (which in and off itself tells you that the persistence.xml file has been located). For me the solution was to change the persistence XML element from this:
回到 OP 的原始问题,我有这个完全相同的错误消息(它本身告诉您已经找到了 persistence.xml 文件)。对我来说,解决方案是从这里更改持久性 XML 元素:
<persistence 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"
version="1.0">
to this:
对此:
<persistence 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"
version="2.0">
Once changed, the error went away and Dali instantly came alive and now everything Just Works. Hope this helps somebody someday.
一旦更改,错误就会消失,Dali 立即活跃起来,现在一切正常。希望有一天这对某人有所帮助。
回答by Gursel Koca
I came accross the same problem. Goto project properties -> Java build path -> source , remove */.java excluded attribute of src/main/resources source. Then add META-INF/persistence.xml. Then restart eclipse. You should persistence.xml under JPA Content in Project explorer. After that revert your changes.
我遇到了同样的问题。转到项目属性 -> Java 构建路径 -> 源,删除src/main/resources 源的* /.java 排除属性。然后添加 META-INF/persistence.xml。然后重启eclipse。您应该在项目资源管理器中的 JPA 内容下使用 persistence.xml。之后还原您的更改。
Well, what I suggest seem stupid, but magically it works. By the way my eclipse version is 3.6.
好吧,我的建议看起来很愚蠢,但它神奇地起作用了。顺便说一下,我的 Eclipse 版本是 3.6。
回答by Rick
<persistence 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"
version="2.0">
Worked for me!
为我工作!
回答by Martin Trummer
I fixed this by going to Project - Properties - Java Persistence: and then setting the "Source Folder" in the "Canonical metamodel (JPA 2.0)" section to: "src/generated/resources"
我通过转到 Project - Properties - Java Persistence: 然后将“Canonical metamodel (JPA 2.0)”部分中的“Source Folder”设置为:“src/generated/resources”来解决这个问题
回答by willtardy
I had this same problem in Eclipse 3.6 (Helios). The cause was the JPA Facet was set to JPA Version 1.0, however my persistence.xml file was specifying JPA Version 2. So I changed the JPA Facet to version 2.0 and the problem went away. (Project --> Properties --> Project Facets --> "Configuration" drop down --> set this to "Minimal JPA 2.0 configuration"
我在 Eclipse 3.6 (Helios) 中遇到了同样的问题。原因是 JPA Facet 设置为 JPA Version 1.0,但是我的 persistence.xml 文件指定了 JPA Version 2。所以我将 JPA Facet 更改为 version 2.0,问题就消失了。(项目 --> 属性 --> 项目构面 --> “配置”下拉菜单 --> 将其设置为“最小 JPA 2.0 配置”
回答by YoK
Had same problem with error
有同样的错误问题
The persistence.xml file does not have recognized content
persistence.xml 文件没有可识别的内容
Fixed by editing file in eclipse, any edit will do. And after saving error got away.
通过在eclipse中编辑文件来修复,任何编辑都可以。保存后错误就消失了。