JPA - 禁用persistence.xml的验证 [英] JPA - disable validation of persistence.xml

查看:101
本文介绍了JPA - 禁用persistence.xml的验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用JPA(Hibernate)迈出第一步。总体思路是连接到传统数据库来完成复杂的查询。我认为,hibernate是这项任务的很好伴侣,但是......一开始,我创建了一个bean,即persistence.xml和hibernate。 cfg.xml配置文件和主要方法中的一些代码行,从以下开始:

  EntityManagerFactory emf = Persistence.createEntityManagerFactory(富); 

它读取persistence.xml文件,因为我断开连接从互联网上,它无法读取模式文件(xsd)来验证persistence.xml并抱怨异常。 (并且我没有在该环境中的persistence.xsd的本地副本)。我尝试删除schemalocation属性,但这并没有帮助。没有语法,没有JPA!?

有没有一种方法/一个技巧/解决方法来禁用文档验证,至少用于解析persistence.xml?

解决方案

从JPA规范中提取


(Hibernate)必须根据persistence_1_0.xsd模式验证persistence.xml文件并报告任何验证错误


...


持久性配置文件必须通过使用持久性命名空间来指示持久性模式




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


lockquote

并使用版本元素指示模式的版本,如图所示




 < 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


I'm taking my first steps with JPA (Hibernate). The overall idea is to connect to a legacy database to do complex queries. I think, hibernate is a great companion for this task, but...

... for a start, I created one bean, the persistence.xml and hibernate.cfg.xml configuration files and some lines of code in a main method, starting with:

EntityManagerFactory emf = Persistence.createEntityManagerFactory("foo");

It reads the persistence.xml file but, because I'm disconnected from the internet, it can't read the schema file (xsd) to validate persistence.xml and complains with exceptions. (And I don't have a local copy of persistence.xsd in that environment). I tried with removing the schemalocation attribute, but that didn't help. No grammar, no JPA!?

Is there a way / a trick / a workaround to disable document validation at least for parsing persistence.xml?

解决方案

Extracted from JPA specification

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

The element shown above is valid for JPA 1.0 Update to 2.0 if you are using JPA 2.0

这篇关于JPA - 禁用persistence.xml的验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆