2013-04-15 70 views
1

我在尝试在代码中创建实体管理器时出现此错误。persistence.xml - 解析XML时出错:XML InputStream(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> 
+0

你可以发布你的persistence.xml吗? –

+0

刚更新的问题与persistence.xml –

+3

我会建议Maven资源过滤替换引入了一些无效的混淆XML解析器。你可以发布替代的结果吗?这应该在你的'target'目录中可用。 –

回答

0

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