gpt4 book ai didi

java - 手动创建 EntityManagerFactory?

转载 作者:数据小太阳 更新时间:2023-10-29 02:46:34 26 4
gpt4 key购买 nike

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

最佳答案

我认为您遇到的问题是

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

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

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

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

关于java - 手动创建 EntityManagerFactory?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5911936/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com