views:

83

answers:

1

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?

+3  A: 

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

Arthur Ronald F D Garcia
The persistence tag looks familiar, just like mine - so there is **no way** using JPA if you don't have access to that schema file?
Andreas_D
@Andreas_D In Java SE environment, the persistence provider must validate the persistence.xml file against the persistence_1_0.xsd schema. If you are using Hibernate as persistence provider, it should contain the schema file (I am pretty sure the it is stored in the jar file). I have to say it because when i tried JPA for the first time (About 4 years ago), i forgot to declare version attribute and you know (a big headache)
Arthur Ronald F D Garcia
Bad luck - there's nothing in my hibernate jars - but there's a chance, that they're custom builds and contain nothing but the class files... looks like I have to print out the schema and key it into the other system now *sigh*
Andreas_D
@Andreas_D **The 1.0 provider contains persistence_1_0.xsd file**. I suppose you are using JPA 2.0 persistence_2_0.xsd (Not applied for me)
Arthur Ronald F D Garcia
@Arthur - either if it's version 1.0 or 2.0 - actually I have a bunch of hibernate (and other) jars but non of them contains any schema file. The standard schemalocation points to an internet URL, i could replace that easily to use a file if I just had one. So I think I'll just write a dummy schema just to satisfy the validator...
Andreas_D
@Andreas_D Very strange. I will see if there is some related issue in the JIRA. If not, i will complain
Arthur Ronald F D Garcia
@Arthur - before complaining - I'm not sure if 'my' hibernate3.jar really is complete. 'My' hibernate library is the one that has been distributed with the play framework. I think I first have to double-check if it equal to the libraries that we get from official sources. But I can't check now and here. For the moment I simply coded mimimal grammar files (xsd and dtd) and JPA is happy.
Andreas_D
+1 for the answer and the headache :)
Pascal Thivent