2011-05-06 101 views
3

有誰知道如何手動創建EntityManagerFactory?當我手動說,我的意思是它消耗一個特殊的persistence.xml文件?這是我所嘗試過的,都使我失敗了。手動創建一個EntityManagerFactory?

Configuration configuration = new Configuration(); 
InputStream is = JPAUtil.class.getResourceAsStream("/s-persistence.xml"); 
configuration.addInputStream(is); 
_entityManagerFactory = Persistence.createEntityManagerFactory(
     puType.persistenceName, configuration.getProperties()); 

s-persistence.xml看起來像:

<?xml version="1.0"?> 
<persistence version="1.0" 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_1_0.xsd"> 
    <persistence-unit name="S_DATABASE" transaction-type="RESOURCE_LOCAL"> 
     <provider>org.hibernate.ejb.HibernatePersistence</provider> 
     <properties> 
      <property value="false" name="hibernate.show_sql"/> 
      <property value="org.hibernate.dialect.OracleDialect" name="hibernate.dialect"/> 
      <property value="oracle.jdbc.driver.OracleDriver" name="hibernate.connection.driver_class "/> 
      <property value="jdbc:oracle:thin:@xxx.xxx.xxx.xxx:TEST" name="hibernate.connection.url"/> 
      <property value="TESTOWNER" name="hibernate.connection.username"/> 
      <property value="TEST" name="hibernate.connection.password"/> 
      <property value="2" name="hibernate.connection.isolation"/> 
     </properties> 
    </persistence-unit> 
</persistence> 

運行的Java代碼時,我碰到下面的錯誤。

ERROR :: org.hibernate.util.XMLHelper|Error parsing XML: XML InputStream(2) Document  is invalid: no grammar found. 
ERROR :: org.hibernate.util.XMLHelper|Error parsing XML: XML InputStream(2) Document root element "persistence", must match DOCTYPE root "null". 
org.hibernate.MappingException: invalid mapping 

這就像它正在尋找在persistence.xml休眠特定的語法。我也嘗試了EJB3Configuration。它看起來像我的XML格式良好,這是爲什麼我得到這個錯誤。

+0

你曾經能夠解決這個問題? – CMR 2011-07-29 21:22:05

回答

0

我認爲你正在運行到這個問題是

http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd 

可能不能到達,所以驗證XML文件,對不能正常工作。

儘量不給模式位置是這樣的:

<persistence xmlns="http://java.sun.com/xml/ns/persistence"> 
+0

感謝您的建議。但即使進行了更改,我仍然得到同樣的確切錯誤。我的persistence.xml文件的頂部現在看起來像你所建議的。 – jen 2011-05-06 14:40:13