persistence.xml - 解析XML时出错:XML InputStream(1)文件过早结束

时间:2013-04-15 21:53:20

标签: xml hibernate maven

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

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>

1 个答案:

答案 0 :(得分:0)

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