【问题标题】:Creating an EntityManagerFactory manually?手动创建 EntityManagerFactory?
【发布时间】:2011-05-06 13:11:58
【问题描述】:

有人知道如何手动创建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 格式正确,这让我不知道为什么会出现此错误。

【问题讨论】:

  • 你能解决这个问题吗?

标签: java xml hibernate jpa


【解决方案1】:

我认为你遇到的问题是

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

可能无法访问,因此无法针对它验证 XML 文件。

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

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

【讨论】:

  • 感谢您的建议。但即使在进行更改后,我仍然会遇到同样的错误。我的 persistence.xml 文件的顶部现在看起来像您建议的那样。
猜你喜欢
  • 2018-09-07
  • 2016-06-25
  • 2021-12-19
  • 2011-02-16
  • 2020-02-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-30
相关资源
最近更新 更多