2013-04-15 68 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)。當我將機器連接到互聯網時,它開始工作。我試圖解決這個問題,所以我不需要連接到互聯網,但這是一個不同的問題。