我正迈出 JPA (Hibernate) 的第一步。总体思路是连接到遗留数据库以进行复杂查询。我认为,hibernate 是完成这项任务的好伙伴,但是......
...首先,我创建了一个 bean、persistence.xml 和 hibernate.cfg.xml 配置文件以及 main 方法中的一些代码行,从以下内容开始:
EntityManagerFactory emf = Persistence.createEntityManagerFactory("foo");
它读取 persistence.xml 文件但是,因为我与互联网断开连接,它无法读取模式文件 (xsd) 来验证 persistence.xml 并提示异常。 (而且我在该环境中没有 persistence.xsd 的本地副本)。我尝试删除 schemalocation 属性,但这没有帮助。没有语法,就没有 JPA!?
有没有一种方法/技巧/解决方法可以至少在解析 persistence.xml 时禁用文档验证?
最佳答案
从 JPA 规范中提取
The container/presistence provider (Hibernate) must validate the persistence.xml file against the persistence_1_0.xsd schema and report any validation errors
...
Persistence configuration files must indicate the persistence schema by using the persistence namespace
http://java.sun.com/xml/ns/persistence
And indicate the version of the schema by using the version element as shown below
<persistence 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"
version="1.0">
上面显示的元素对 JPA 1.0 有效如果您使用的是 JPA 2.0,请更新到 2.0
关于java - JPA - 禁用 persistence.xml 的验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3768725/