java - JPA - 禁用 persistence.xml 的验证

标签 java hibernate jpa

我正迈出 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/

相关文章:

sql - 如何在 JPA/Hibernate 中执行 native SQL 脚本?

java - @Basic(fetch = FetchType.LAZY) 不起作用?

java - 如何通过SSH远程启动jboss服务?

java - 有什么方法可以在 Android 中为 API 14 实现 showSoftInputOnFocus?

java - JNI Windows Netbeans 不满意的链接错误

java - 如何使 hbm2ddl schemaExport 将模式记录到标准输出?

postgresql - 为什么 PostgreSql 的 Hibernate 默认生成器是 "SequenceGenerator",而不是 "IdentityGenerator"?

java - 将实体类参数传递给 createNativeQuery 有何作用?

java - OpenHFT/Chronicle map 失败

java - Hibernate Session 更新未反射(reflect)在数据库中