手动创建EntityManagerFactory?

时间:2011-05-06 13:11:58

标签: java xml hibernate jpa

有谁知道如何手动创建EntityManagerFactory?当我手动说我的意思是让它消耗一个特殊的persistence.xml文件?我所尝试的一切都让我失望。

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中寻找hibernate特定语法一样。我也尝试过EJB3Configuration。看起来我的XML格式正确,这让我不知道为什么会收到这个错误。

1 个答案:

答案 0 :(得分:0)

我认为你遇到的问题是

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

可能无法访问,因此根据它验证XML文件是行不通的。

尽量不给出这样的架构位置:

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