【问题标题】:persistence.xml - Error parsing XML: XML InputStream(1) Premature end of filepersistence.xml - 解析 XML 时出错:XML InputStream(1) 文件过早结束
【发布时间】:2013-04-08 03:58:50
【问题描述】:

当我尝试在我的代码中创建实体管理器时遇到此错误。

EntityManagerFactory emf = Persistence.createEntityManagerFactory("puDS1", myproperties);

关于为什么会发生这种情况的任何提示?我的 persistence.xml 似乎是一个有效的 xml。

ERROR [main] (PersistenceXmlLoader.java:251) - Error parsing XML: XML InputStream(1) Premature end of file.
javax.persistence.PersistenceException: Unable to configure EntityManagerFactory
        at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:265)
        at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:125)
        at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:52)
Caused by: org.xml.sax.SAXParseException: Premature end of file.
        at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(Unknown Source)
        at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(Unknown Source)
        at org.hibernate.ejb.packaging.PersistenceXmlLoader.loadURL(PersistenceXmlLoader.java:70)
        at org.hibernate.ejb.packaging.PersistenceXmlLoader.deploy(PersistenceXmlLoader.java:89)
        at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:222)
        ... 4 more

持久性 xml 看起来像这样。请注意,我在这个 persistence.xml 上使用了 maven 资源过滤,因此在编译期间 ${jar_file_name_ds1} 和 ${jar_file_name_ds2} 被替换为有效名称。我已经验证了最终的 persistence.xml 已正确替换了值。我不知道 maven 资源过滤是否搞砸了。

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <persistence-unit name="puDS1" transaction-type="RESOURCE_LOCAL">
      <provider>org.hibernate.ejb.HibernatePersistence</provider>
      <jar-file>${jar_file_name_ds1}</jar-file>      
      <properties>         
         <property name="hibernate.max_fetch_depth" value="3"/>
      </properties>
   </persistence-unit>   
   <persistence-unit name="puDS2" transaction-type="RESOURCE_LOCAL">
      <provider>org.hibernate.ejb.HibernatePersistence</provider>
      <jar-file>${jar_file_name_ds2}</jar-file>
      <properties>
         <property name="hibernate.max_fetch_depth" value="3"/>
      </properties>
   </persistence-unit>   
</persistence>

【问题讨论】:

  • 你能发布你的 persistence.xml 吗?
  • 刚刚用persistence.xml更新了问题
  • 我建议 Maven 资源过滤替换引入了一些无效的东西,这会混淆 XML 解析器。你能发布替换的结果吗?这应该在您的target 目录中可用。

标签: xml hibernate maven


【解决方案1】:

问题是代码试图连接到互联网(我假设让 xsd 验证 persistence.xml)。当我将机器连接到互联网时,它开始工作。我正在尝试解决这个问题,这样我就不需要连接到互联网,但这是一个不同的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-28
    • 1970-01-01
    • 2018-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-20
    • 1970-01-01
    相关资源
    最近更新 更多