2011-05-06 83 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