简体   繁体   English

Eclipse 无法识别persistence.xml 的内容

[英]Eclipse does not recognize content of persistence.xml

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.我的persistence.xml 文件在我的应用程序中运行良好,但eclipse 不断给我这个错误。 I got this after moving the file and updating my project configuration with m2eclipse.在移动文件并使用 m2eclipse 更新我的项目配置后,我得到了这个。 I did not change the file itself.我没有更改文件本身。 Anyone knows how to solve this?有谁知道如何解决这个问题?

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通过设置将其显示为警告而不是错误的选项暂时解决

I fixed the problem the following way (I'm using RAD v8.5.1):我通过以下方式解决了这个问题(我使用的是 RAD v8.5.1):

  1. Windows --> Preferences --> Validation Windows --> 首选项 --> 验证
  2. Scroll down to "JPA Validator" and click on the Ellipses under Settings.向下滚动到“JPA Validator”并单击“设置”下的省略号。
  3. Click on the "Add Include Group..." button.单击“添加包含组...”按钮。
  4. Click on the new Include Group that appeared to highlight it.单击出现的新包含组以突出显示它。
  5. Click on the "Add Rule..." button.单击“添加规则...”按钮。
  6. A dialog menu pops up.弹出一个对话框菜单。 Click the "Folder or file name" radio button and click on "Next".单击“文件夹或文件名”单选按钮,然后单击“下一步”。
  7. 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.单击“浏览文件夹...”并导航到工作区中项目(我的项目是多模块应用程序中的 .war)中的 src 目录(在我的情况下,它是“src/main/resources”)。
  8. Click on "Finish".单击“完成”。
  9. Click on "OK".单击“确定”。 This will prompt a full build.这将提示一个完整的构建。
  10. The error will go away.错误将消失。

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.通过在eclipse中编辑文件来修复,任何编辑都可以。 And after saving error got away.保存后错误就消失了。

<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!为我工作!

I figured out what the problem is here.... the JPA Facet is assuming that the META-INF folder is directly under a source folder.我想出了这里的问题...... JPA Facet 假设 META-INF 文件夹直接位于源文件夹下。 If you are using a Maven project, you likely have something like src/main/resources or src/test/resources.如果您正在使用 Maven 项目,您可能有类似 src/main/resources 或 src/test/resources 的内容。 If your persistence.xml is under these folders it will complain.如果您的persistence.xml 在这些文件夹下,它会报错。 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.如果您创建一个新的源文件夹(称为“jpa”)并在其中创建一个 META-INF/persistence.xml,那么它将成功找到它。 (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.这当然是一种痛苦......而且我可以看到很多情况这不会很好地工作。

I had this same problem in Eclipse 3.6 (Helios).我在 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.原因是 JPA Facet 设置为 JPA Version 1.0,但是我的 persistence.xml 文件指定了 JPA Version 2。所以我将 JPA Facet 更改为 version 2.0,问题就消失了。 (Project --> Properties --> Project Facets --> "Configuration" drop down --> set this to "Minimal JPA 2.0 configuration" (项目 --> 属性 --> 项目构面 --> “配置”下拉菜单 --> 将其设置为“最小 JPA 2.0 配置”

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.将此错误转换为警告可能会使问题消失,但它不会解决 Eclipse Dali 插件的潜在问题。 It will also mean the extremely useful JPA Structure and JPA Details views will not work.这也意味着非常有用的 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. OP 提供的链接和此处大多数答案的方法实际上是为了解决一个不同的问题,其中 Maven 想要过滤 persistence.xml 文件,从而告诉 Eclipse 忽略它。 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. 此处解释了该问题的可行解决方法,包括从项目属性的类路径中的 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).回到 OP 的原始问题,我有这个完全相同的错误消息(它本身告诉您已经找到了 persistence.xml 文件)。 For me the solution was to change the persistence XML element from this:对我来说,解决方案是从这里更改持久性 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.一旦更改,错误就会消失,Dali 立即活跃起来,现在一切正常。 Hope this helps somebody someday.希望有一天这对某人有所帮助。

The bug has reappeared for certain Indigo (3.7) versions.该错误在某些 Indigo (3.7) 版本中再次出现。 See here: https://bugs.eclipse.org/bugs/show_bug.cgi?id=357810请参阅此处: https : //bugs.eclipse.org/bugs/show_bug.cgi?id=357810

Right-click project -> Maven -> Update Project Configuration...右键项目 -> Maven -> 更新项目配置...

This might destroy certain buildpath entries and/or the deployment assembly, very annoying, so only do this when changing Maven deps.可能会破坏某些构建路径条目和/或部署程序集,非常烦人,因此仅在更改 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 .要在补丁发布之前解决persistence.xml 消息,您需要在许多操作上清理项目,因为即使将错误级别设置为“忽略”也不起作用 This is a complete bummer.这是一个完全的无赖。

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.如果我创建一个 JPA 项目,转换为 Maven 项目,就会出现错误,我无法使用任何推荐的方法摆脱它。

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 to src/main/resources/META-INF it was resolved.好的,在移动一些文件夹之后,它以实际的Maven 目录格式,它仍然没有工作 - 但在src/main/java下创建一个名为 META-INF 的链接文件夹,链接到src/main/resources/META-INF它被解决了。

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.创建一个 Maven 项目并添加 JPA 方面也有效,但只是因为 JPA 方面将 META-INF 和 persistence.xml 文件添加到src/main/java ,我认为这不完全正确。

在此处输入图片说明No Need to change andthing in your xml file and no need to add xml in src folder.无需更改 xml 文件中的内容,也无需在 src 文件夹中添加 xml。 Put your persistance.xml file in META-INF folder and then put META-INF src/main/resource folder.将您的 persistance.xml 文件放在 META-INF 文件夹中,然后将 META-INF src/main/resource 文件夹。 Try following approach: Right click on project> Java Build path > Source Tab > ProjectName/src/main/resource > set Included(All) and Excluded(None).尝试以下方法:右键单击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.在 RAD 7 & 8..5 & Eclipse 上尝试了这种方法。

This problems occurs if you如果您

  • Convert a JPA project to a Maven project or将 JPA 项目转换为 Maven 项目或
  • Add the JPA facet to a Maven project.将 JPA facet 添加到 Maven 项目。

The solution is解决办法是

  • Select Windows > Preferences > Validation选择Windows > 首选项 > 验证
  • Disable the JPA Validator for both Manual validation and Build validation禁用手动验证和构建验证的 JPA 验证器

After changing these settings,then clean your project.更改这些设置后,然后清理您的项目。

I am using eclipse mars and had this same exact error.我正在使用 eclipse mars 并且有同样的错误。 What I had to do was 1 set the version of jpa for the project facet.我必须做的是 1 为项目方面设置 jpa 的版本。 right click project -> Properties -> Project Facets Set version of jpa to 2.1 in my case.在我的情况下, right click project -> Properties -> Project Facets将 jpa 的版本设置为 2.1。

Note: Just configuring the jpa project facet will not remove the error.注意:仅配置 jpa 项目 facet 不会消除错误。 You also need to configure the platform.您还需要配置平台。

Next I had to configure the JPA platform.接下来我必须配置 JPA 平台。 right click project -> Properties -> JPA . right click project -> Properties -> JPA Change platoform to be Hibernate 2.1 in my case.在我的情况下,将平台更改为 Hibernate 2.1。

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.当您使用 Persistence XML 编辑器打开 persistence.xml 并且底部只有一个Source选项卡而没有 General、Connection、Options、Properties 等其他选项卡时,此问题也会表现出来。

我通过转到 Project - Properties - Java Persistence: 然后将“Canonical metamodel (JPA 2.0)”部分中的“Source Folder”设置为:“src/generated/resources”来解决这个问题

I had src/main/java/META-INF/persistence.xml working fine in Eclipse with embedded Tomcat.我让src/main/java/META-INF/persistence.xml在带有嵌入式 Tomcat 的 Eclipse 中正常工作。 Because it was not being copied into the Maven-built WAR, I moved it to src/main/resources/META-INF .因为它没有被复制到 Maven 构建的 WAR 中,所以我将它移动到src/main/resources/META-INF

Now src/main/resources/META-INF/persistence.xml won't validate.现在src/main/resources/META-INF/persistence.xml不会验证。

If rename to xyz_persistence.xml, it validates fine.如果重命名为 xyz_persistence.xml,则验证正常。
If rename to persistence.xml, it fails to validate.如果重命名为persistence.xml,则验证失败。

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:要删除错误“persistence.xml 文件没有可识别的内容”错误,请禁用 JPA 验证器:

  • List item Select menu item Project > Properties > Preferences > Validation列表项 选择菜单项 Project > Properties > Preferences > Validation
  • List item Disable the JPA Validator for both Manual validation and Build validation.列表项禁用手动验证和构建验证的 JPA 验证器。

You might need to clean and rebuild.您可能需要清理和重建。 Your mileage may vary.您的里程可能会有所不同。

I got the issue fixed by creating a "symbolic link" in Eclipse.我通过在 Eclipse 中创建一个“符号链接”来解决这个问题。 So, my persistence.xml file remains in src/main/resources/META-INF as Maven expects and m2eclipse requires.因此,我的 persistence.xml 文件保留在 src/main/resources/META-INF 中,正如 Maven 所期望的和 m2eclipse 需要的那样。 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.但是,为了修复 Dali 问题,请在 src/main/java/META-INF 中创建一个新文件,该文件链接到 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.请注意,这不会在 META-INF 文件夹中创建物理文件,但是,您需要文件夹本身才能创建链接。

Hope that works for you.希望这对你有用。

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.问题是 META-INF 必须被视为一个源文件夹,因为它在 /ejbModule 下(我的是一个 EJB 项目),我认为它在构建路径上。 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.所以,我所做的只是在 Project Explorer 视图中右键单击 META-INF 文件夹,然后在 Build Path 菜单上选择 INCLUDE。 Then I asked Eclipse to clean the project and everything was right again.然后我让 Eclipse 清理项目,一切又恢复正常。

Best Regards.最好的问候。

I've had the same problem.我遇到了同样的问题。 My solution: right click on the persistence.xml file -> JPA-Tools -> Synchronize Class List.我的解决方案:在persistence.xml文件上右键->JPA-Tools->同步类列表。 After that it was resolved.之后就解决了。

In my case it was the Maven plugin which has a bug that sets "Exclude" to .就我而言,它是 Maven 插件,它有一个错误,将“排除”设置为. in the Build path for your resources folder.在资源文件夹的构建路径中。 Removing .删除. solved the issue for me为我解决了这个问题

I got it fixed by dragging META-INF folder to src/main/resources and drag it back to src/test/resources.我通过将 META-INF 文件夹拖到 src/main/resources 并将其拖回 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.我尝试了上面给出的所有步骤,但似乎没有一个对我有用,最后这个成功了。

just solved the issue by doing this:刚刚通过这样做解决了这个问题:

  1. in schemaLocation, i deleted "h ttp://java.sun.com/xml/ns/persistence/" making it:在 schemaLocation 中,我删除了“h ttp://java.sun.com/xml/ns/persistence/”,使其:

xsi:schemaLocation="http://java.sun.com/xml/ns/persistence persistence_2_0.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence persistence_2_0.xsd"

  1. i added <?xml version="1.0" encoding="UTF-8"?> at the top.我在顶部添加了<?xml version="1.0" encoding="UTF-8"?>

  2. i clean build the project.我清理构建项目。 error's gone.错误消失了。

  3. i added the "h ttp://java.sun.com/xml/ns/persistence/" again:我再次添加了“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 " xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd "

  1. i clean build the project.我清理构建项目。 error's not there.错误不存在。

  2. i deleted <?xml version="1.0" encoding="UTF-8"?> at the top.我删除了顶部的<?xml version="1.0" encoding="UTF-8"?>

  3. i clean build the project.我清理构建项目。 error's still not there.错误仍然不存在。

hope this helps someone in the future.希望这有助于将来的某人。

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:因为我使用了 Mercurial 并且可以轻松返回,而当前版本和最新版本没有任何显着差异,因此它的工作方式如下:

  • ( committed some important changes rather unrelated to this problem ) 提交了一些与此问题无关的重要更改

  • switching to previous repo version切换到以前的回购版本
  • ( auto-/refresh/-rebuild ran fine ) 自动/刷新/重建运行正常

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 .我认识到,问题可能是由于Java Builder 没有生成我的war/WEB-INF/classes/ files The following did not work to follow this trail in letting it build my project:以下内容无法按照这条线索构建我的项目:

  • disabling/reconfiguring Validators, Builders, Facets (till only the Java Builder/Facet was there)禁用/重新配置验证器、构建器、构面(直到只有 Java 构建器/构面在那里)
  • renaming the persistence.xml or editing it in various ways重命名persistence.xml或以各种方式编辑它

which did not help it as if some internal state could not be changed.这对它没有帮助,好像某些内部状态无法更改。

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/ 中有我的

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 )清理项目(“项目 > 清理”和 Maven clean
  • Force Eclipse to validate the XML file again (right-click, and select Validate XML file).强制 Eclipse 再次验证 XML 文件(右键单击并选择验证 XML 文件)。

我有几次遇到同样的问题,对我有用的是复制文件的内容,删除它并创建一个新的 persistence.xml 文件并将内容粘贴回。

I came accross the same problem.我遇到了同样的问题。 Goto project properties -> Java build path -> source , remove * / .java excluded attribute of src/main/resources source.转到项目属性 -> Java 构建路径 -> 源,删除 src/main/resources 源的 * / .java 排除属性。 Then add META-INF/persistence.xml.然后添加 META-INF/persistence.xml。 Then restart eclipse.然后重启eclipse。 You should persistence.xml under JPA Content in Project explorer.您应该在项目资源管理器中的 JPA 内容下使用 persistence.xml。 After that revert your changes.之后还原您的更改。

Well, what I suggest seem stupid, but magically it works.好吧,我的建议似乎很愚蠢,但它神奇地起作用了。 By the way my eclipse version is 3.6.顺便说一下,我的 Eclipse 版本是 3.6。

对我来说,一旦我清除了 eclipse 中 Markers 选项卡中的所有消息,它就起作用了

Add your JPA Version to the Project Facets to remove this error.将您的JPA 版本添加到Project Facet以消除此错误。

In my case i have used JPA 2.2 but in Project Facets JPA 1.0 was specified.就我而言,我使用了 JPA 2.2,但在 Project Facets 中指定了 JPA 1.0。 After fixing this, the error disappeared.解决这个问题后,错误消失了。

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 .该问题已在 Indigo(2011 年 6 月发布)中修复,请参阅https://bugs.eclipse.org/bugs/show_bug.cgi?id=251323#c24https://issues.sonatype.org/browse/MECLIPSEWTP -11

The "persistence.xml file does not have recognized content." “persistence.xml 文件没有可识别的内容。” error might still appear, randomly, on project import or Workspace startup, but it can be fixed with a project clean or maven project update.在项目导入或工作区启动时,错误可能仍会随机出现,但可以通过项目清理或 Maven 项目更新来修复。 This will be fixed in Dali in SR2.这将在 SR2 的 Dali 中修复。

我有同样的问题,为了让它消失,我必须确保这一行是文件顶部的第一行(没有前导空行或空格):

<?xml version="1.0" encoding="UTF-8"?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM