1

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

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>
4

1 回答 1

1

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

于 2013-04-17T05:51:06.477 回答