Eclipse 无法识别 persistence.xml 的内容
我在 eclipse 中收到以下错误:
persistence.xml 文件没有可识别的内容。
我的 persistence.xml 文件在我的应用程序中工作得很好,但 eclipse 一直给我这个错误。我在移动文件并使用 m2eclipse 更新我的项目配置后得到了这个。我没有更改文件本身。有人知道如何解决这个问题吗?
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="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>
更新
看起来像是 m2eclipse 与 jpa 结合使用时的一个错误。
https://bugs.eclipse.org/bugs/show_bug.cgi?id=251323
通过设置选项将其显示为警告而不是错误来暂时解决
Im getting the following error in eclipse:
The persistence.xml file does not have recognized content.
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:
<?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.
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(28)
我也遇到过同样的问题。我的解决方案:右键点击persistence.xml文件-> JPA-工具->同步班级列表。之后就解决了。
I've had the same problem. My solution: right click on the persistence.xml file -> JPA-Tools -> Synchronize Class List. After that it was resolved.
在我的例子中,Maven 插件有一个错误,该错误将资源文件夹的构建路径中的“排除”设置为 .。删除 . 为我解决了这个问题
In my case it was the Maven plugin which has a bug that sets "Exclude" to . in the Build path for your resources folder. Removing . solved the issue for me
只是通过这样做解决了问题:
xsi:schemaLocation="http://java.sun.com/ xml/ns/persistence persistence_2_0.xsd"
我在顶部添加了
。
我干净地构建了该项目。错误消失了。
我再次添加了“h ttp://java.sun.com/xml/ns/persistence/”:
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
我干净地构建了该项目。错误不存在。
我删除了顶部的
。
我干净地构建了该项目。错误仍然不存在。
希望这对将来的人有帮助。
just solved the issue by doing this:
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence persistence_2_0.xsd"
i added
<?xml version="1.0" encoding="UTF-8"?>
at the top.i clean build the project. error's gone.
i added the "h ttp://java.sun.com/xml/ns/persistence/" again:
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
i clean build the project. error's not there.
i deleted
<?xml version="1.0" encoding="UTF-8"?>
at the top.i clean build the project. error's still not there.
hope this helps someone in the future.
好吧...另一个相当强大的解决方案对我有用,因为最顶层的解决方案没有:
因为我使用了 Mercurial 并且可以轻松返回,而当前版本和上一个版本没有任何显着差异,它的工作方式如下:
(提交了一些与此问题无关的重要更改)
什么我认识到,问题可能是由于 Java Builder 没有生成我的
war/WEB-INF/classes/
文件。以下内容无法遵循此线索来构建我的项目:无济于事,就好像某些内部状态无法改变一样。
Ok ... another rather robust solution that worked for me since the top-most ones did not:
since I used Mercurial and could go back easily without any notable differences in the current and last versions it worked this way:
(committed some important changes rather unrelated to this problem)
What I recognized was, that the problem may have been due to the fact that the Java Builder did not generate my
war/WEB-INF/classes/
files. The following did not work to follow this trail in letting it build my project:persistence.xml
or editing it in various wayswhich did not help it as if some internal state could not be changed.
对我来说,一旦我清除了 Eclipse 中“标记”选项卡中的所有消息,它就起作用了
For me it worked once I have cleared all messages from Markers tab in eclipse
将您的 JPA 版本 添加到 Project Facets 以消除此错误。
就我而言,我使用了 JPA 2.2,但在 Project Facets 中指定了 JPA 1.0。
修复此问题后,错误消失了。
Add your JPA Version to the Project Facets to remove this error.
In my case i have used JPA 2.2 but in Project Facets JPA 1.0 was specified.
After fixing this, the error disappeared.
我通过转到项目 - 属性 - Java 持久性来修复此问题:然后将“规范元模型 (JPA 2.0)”部分中的“源文件夹”设置为:“src/ generated/resources”
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"
有这方面的更新吗?这似乎仍然是一个问题吗?如果我创建一个 JPA 项目,转换为 Maven 项目,则会出现错误,并且我无法使用任何推荐的方法摆脱它。
编辑
好的,在处理了一些文件夹之后,它就在一个实际的 Maven目录格式,它仍然不起作用 - 但在
src/main/java
下创建一个名为META-INF的链接文件夹,链接到src/main/resources/ META-INF
已解决。创建 Maven 项目并添加 JPA 方面也可以,但只是因为 JPA 方面将 META-INF 和 persistence.xml 文件添加到
src/main/java
中,我认为这并不完全正确。Any updates on this? This is still an issue it seems right? If I create a JPA project, convert to Maven project, the error appears and I can't get rid of it with any of the recommended methods.
Edit
Ok so after maneuvering some folders around so its in an actual Maven directory format, it still didn't work - but creating a linked folder under
src/main/java
named META-INF, linked tosrc/main/resources/META-INF
it was resolved.Creating a Maven project and adding the JPA facet works also, but only because the JPA facet adds the META-INF and persistence.xml file to
src/main/java
which I don't think is exactly right.无需更改 xml 文件中的内容,也无需在 src 文件夹中添加 xml。将 persistance.xml 文件放入 META-INF 文件夹中,然后放入 META-INF src/main/resource 文件夹中。
尝试以下方法:
右键单击
项目>; Java构建路径>来源选项卡>项目名称/src/main/resource >设置包含(全部)和排除(无)。
然后清理并构建您的项目。它会对你有用。在 RAD 7 和 RAD 7 上尝试过这种方法。 8..5&蚀。No Need to change andthing in your xml file and no need to add xml in src folder. Put your persistance.xml file in META-INF folder and then put META-INF src/main/resource folder.
Try following approach:
Right click on
project> Java Build path > Source Tab > ProjectName/src/main/resource > set Included(All) and Excluded(None).
then clean and build your project. It will work for you. Tried this approach on RAD 7 & 8..5 & Eclipse.则会出现此问题
。解决办法是
更改这些设置后,然后清理您的项目。
This problems occurs if you
The solution is
After changing these settings,then clean your project.
我正在使用 eclipse mars 并遇到了同样的错误。我所要做的就是 1 设置项目方面的 jpa 版本。
右键单击项目 ->属性-> Project Facets
在我的例子中将 jpa 版本设置为 2.1。注意:
仅配置 jpa 项目方面不会消除错误。您还需要配置平台。
接下来我必须配置 JPA 平台。
右键单击项目 ->属性-> JPA
。在我的例子中,将平台更改为 Hibernate 2.1。当您使用持久性 XML 编辑器打开 persistence.xml 并且底部只有一个
Source
选项卡,而没有常规、连接、选项、属性等其他选项卡时,此问题也会显现出来。I am using eclipse mars and had this same exact error. What I had to do was 1 set the version of jpa for the project facet.
right click project -> Properties -> Project Facets
Set version of jpa to 2.1 in my case.Note:
Just configuring the jpa project facet will not remove the error. You also need to configure the platform.
Next I had to configure the JPA platform.
right click project -> Properties -> JPA
. Change platoform to be Hibernate 2.1 in my case.This problem also manifests itself when you open the persistence.xml with the Persistence XML Editor and you only have a single
Source
tab at the bottom and not the other tabs for General, Connection, Options, Properties etc.我通过在 Eclipse 中创建“符号链接”解决了该问题。因此,我的 persistence.xml 文件保留在 src/main/resources/META-INF 中,正如 Maven 所期望的和 m2eclipse 所要求的那样。但是,为了解决 Dali 问题,请在 src/main/java/META-INF 中创建一个新文件,链接到 src/main/resources/META-INF 中的文件。您可以通过在创建新文件时点击“高级”选项并使用“浏览...”按钮找到该文件来完成此操作。请注意,这不会在 META-INF 文件夹中创建物理文件,但是,您需要该文件夹本身才能创建链接。
希望这对你有用。
I got the issue fixed by creating a "symbolic link" in Eclipse. So, my persistence.xml file remains in src/main/resources/META-INF as Maven expects and m2eclipse requires. However, in order to fix the Dali issues, create a new file in src/main/java/META-INF that links to the one in src/main/resources/META-INF. You do this by hitting the "Advanced" options when creating the new file and locating the file using the "Browse..." button. Note that this will not create a physical file in the META-INF folder, however, you need the folder itself in order to create the link.
Hope that works for you.
我的 src/main/java/META-INF/persistence.xml 在带有嵌入式 Tomcat 的 Eclipse 中工作得很好。因为它没有被复制到 Maven 构建的 WAR 中,所以我将其移动到 src/main/resources/META-INF 中。
现在 src/main/resources/META-INF/persistence.xml 无法验证。
如果重命名为 xyz_persistence.xml,则验证正常。
如果重命名为 persistence.xml,则验证失败。
当这些恼人的情况出现时,我经常会找到许多像我一样的不同“解决方案”,其中大多数并不普遍适用。
所以,这是我的“解决方案”,我认为这是解决错误的一种技巧:
要删除错误“persistence.xml 文件没有识别的内容”错误,请禁用 JPA 验证器:
选择菜单项“项目”>“项目”属性>首选项>验证
禁用手动验证和构建验证的 JPA 验证器。
您可能需要清理和重建。您的里程可能会有所不同。
I had
src/main/java/META-INF/persistence.xml
working fine in Eclipse with embedded Tomcat. Because it was not being copied into the Maven-built WAR, I moved it tosrc/main/resources/META-INF
.Now
src/main/resources/META-INF/persistence.xml
won't validate.If rename to xyz_persistence.xml, it validates fine.
If rename to persistence.xml, it fails to validate.
When these annoying situations arise, I often find numerous different "solutions" like mine, most of which are not universally applicable.
So, here's my "solution", which I think it a hack to get around a bug:
To remove the error "persistence.xml file does not have recognized content" error, disable the JPA Validator:
Select menu item Project > Properties > Preferences > Validation
Disable the JPA Validator for both Manual validation and Build validation.
You might need to clean and rebuild. Your mileage may vary.
我通过将 META-INF 文件夹拖到 src/main/resources 并将其拖回到 src/test/resources 来修复它。这就是我解决问题所需要的一切。我尝试了上面给出的所有步骤,但这些步骤似乎都不适合我,最后这个成功了。
I got it fixed by dragging META-INF folder to src/main/resources and drag it back to src/test/resources. Thats all it took for me to fix the issue. I tried above given all steps but none of those seem to be working for me and finally this one done the trick.
该文件位于您的项目结构中的什么位置?
作为参考,我在 maven-module-name/src/main/resources/META-INF/ 中有我的
Whereabouts in your project structure is the file located?
For ref, I have mine in maven-module-name/src/main/resources/META-INF/
很难说,因为很难重现(您的
persistence.xml
完全有效),但也许可以尝试:clean
>)Hard to say since hard to reproduce (your
persistence.xml
is perfectly valid) but maybe try to:clean
)我曾多次遇到过同样的问题,对我来说有效的方法是复制文件的内容,将其删除并创建一个新的 persistence.xml 文件并将内容粘贴回来。
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.
我遇到了同样的问题。转到项目属性 -> Java构建路径-> source ,删除 src/main/resources 源的 */.java 排除属性。然后添加 META-INF/persistence.xml。然后重新启动eclipse。您应该在项目资源管理器中的 JPA 内容下使用 persistence.xml。之后恢复您的更改。
好吧,我的建议看起来很愚蠢,但神奇的是它确实有效。顺便说一句,我的eclipse版本是3.6。
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.
Well, what I suggest seem stupid, but magically it works. By the way my eclipse version is 3.6.
该问题已在 Indigo(2011 年 6 月发布)中得到修复,请参阅 https: //bugs.eclipse.org/bugs/show_bug.cgi?id=251323#c24 和 https://issues.sonatype.org/browse/MECLIPSEWTP-11。
“persistence.xml 文件没有可识别的内容。”在项目导入或工作区启动时,错误可能仍会随机出现,但可以通过项目清理或 Maven 项目更新来修复。这将在SR2的大理得到修复。
The problem has been fixed in Indigo (released in June 2011), see https://bugs.eclipse.org/bugs/show_bug.cgi?id=251323#c24 and https://issues.sonatype.org/browse/MECLIPSEWTP-11.
The "persistence.xml file does not have recognized content." error might still appear, randomly, on project import or Workspace startup, but it can be fixed with a project clean or maven project update. This will be fixed in Dali in SR2.
我遇到了同样的问题,为了让它消失,我必须确保这一行是文件顶部的第一行(没有前导空行或空格):
I had the same problem, to make it go away I had to make sure this line was the very first line at the top of the file (with no leading empty lines or spaces):
我有同样的错误,但以不同的方式解决了它。
问题是 META-INF 必须被视为源文件夹,并且因为它位于 /ejbModule 下(我的是一个 EJB 项目),所以我假设它位于构建路径上。但事实并非如此。实际上它位于构建路径的“排除”列表中。因此,我所做的就是右键单击 Project Explorer 视图上的 META-INF 文件夹,然后在“构建路径”菜单上选择“INCLUDE”。然后我要求 Eclipse 清理该项目,一切又恢复正常了。
此致。
I had the same error, but solved it in a different way.
The thing is that META-INF must be considered a source folder, and as it was under /ejbModule (mine is a EJB project) I assumed it was on the build path. And it wasn't. Actually it was on "exclude" list on build path. So, all I did was right-click the META-INF folder on Project Explorer view and selected INCLUDE on Build Path menu. Then I asked Eclipse to clean the project and everything was right again.
Best Regards.
我通过以下方式修复了问题(我使用的是 RAD v8.5.1):
I fixed the problem the following way (I'm using RAD v8.5.1):
遇到同样的错误问题
通过在 eclipse 中编辑文件修复,任何编辑都可以。保存后错误消失了。
Had same problem with error
Fixed by editing file in eclipse, any edit will do. And after saving error got away.
为我工作!
Worked for me!
我弄清楚问题出在哪里...... JPA Facet 假设 META-INF 文件夹直接位于源文件夹下。如果您使用 Maven 项目,则可能有 src/main/resources 或 src/test/resources 之类的内容。如果你的 persistence.xml 位于这些文件夹下,它会抱怨。如果您创建一个新的源文件夹(称为“jpa”)并在其中创建 META-INF/persistence.xml,那么它将成功找到它。 (我还必须做一个干净/关闭/打开的项目)
这当然是一种痛苦......而且我可以看到很多情况这不会很好地工作。
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)
This is certainly a pain.... and I can see many situations this won't work well.
我在 Eclipse 3.6 (Helios) 中遇到了同样的问题。原因是 JPA Facet 设置为 JPA 版本 1.0,但我的 persistence.xml 文件指定了 JPA 版本 2。因此,我将 JPA Facet 更改为版本 2.0,问题就消失了。 (项目 --> 属性 --> 项目方面 -->“配置”下拉列表 --> 将其设置为“最小 JPA 2.0 配置”
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 Dali 插件的根本问题。这也意味着极其有用的 JPA Structure 和 JPA Details 视图将无法工作。
OP 提供的链接和此处大多数答案的方法实际上是为了解决另一个问题,其中 Maven 想要过滤 persistence.xml 文件,从而告诉 Eclipse 忽略它。 此处 并涉及从项目属性的类路径中的 src/main/resources 条目中删除排除的:“**”。
回到OP最初的问题,我收到了完全相同的错误消息(它本身告诉您已经找到了 persistence.xml 文件)。对我来说,解决方案是将持久性 XML 元素从以下位置更改为
:
更改后,错误消失,Dali 立即恢复正常,现在一切正常。希望有一天这可以帮助别人。
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.
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 here and involves removing the exclused: '**' from the src/main/resources entry in the classpath in the project properties.
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:
to this:
Once changed, the error went away and Dali instantly came alive and now everything Just Works. Hope this helps somebody someday.
该错误在某些 Indigo (3.7) 版本中再次出现。请参阅此处: https://bugs.eclipse.org/bugs/show_bug.cgi ?id=357810
右键项目-> Maven->更新项目配置...
这可能会破坏某些构建路径条目和/或部署程序集,非常烦人,因此仅在更改 Maven deps 时才执行此操作。
要在补丁发布之前解决 persistence.xml 消息,您需要清理项目的许多操作,因为即使将错误级别设置为“忽略”也不起作用 。这真是太糟糕了。
The bug has reappeared for certain Indigo (3.7) versions. See here: https://bugs.eclipse.org/bugs/show_bug.cgi?id=357810
Right-click project -> Maven -> Update Project Configuration...
This might destroy certain buildpath entries and/or the deployment assembly, very annoying, so only do this when changing Maven deps.
To workaround the persistence.xml message until the patch is released, you need to clean the project on many operations as even setting the error level to "ignore" doesn't work. This is a complete bummer.