手动创建EntityManagerFactory?

发布于 2024-11-05 15:18:59 字数 2201 浏览 0 评论 0原文

有谁知道如何手动创建 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 格式良好,这让我不明白为什么会收到此错误。

Does anyone know how to create an EntityManagerFactory manually? When I say manually I mean have it consume a special persistence.xml file? This what I have tried and all has failed me.

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 looking like:

<?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>

I get the following error when running the java code.

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

It's like it is looking for hibernate specific syntax in the persistence.xml. I've also tried the EJB3Configuration. It looks like my XML is well formed which is throwing me off as to why I am getting this error.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ゝ杯具 2024-11-12 15:18:59

我认为您遇到了

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

可能无法访问的问题,因此无法根据它验证 XML 文件。

尝试不要给出这样的架构位置:

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

I think you are running into the issue that

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

may not be reachable, so that validating the XML file against it can not work.

Try to not give the schema location like this:

<persistence xmlns="http://java.sun.com/xml/ns/persistence">
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文