Hi everyone !
Being a new Hibernate user, I am trying to figure out the different ways to use it. My first use case works well with Hibernate 3.5.2-Final as the JPA 2.0 provider.
Here is an extract of my working configuration :
[META-INF/persistence.xml]
Code:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.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_2_0.xsd ">
<persistence-unit name="party" transaction-type="RESOURCE_LOCAL">
<provider>[color=#0040BF]org.hibernate.ejb.HibernatePersistence[/color]</provider>
<class>party.model.Party</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
<property name="hibernate.hbm2ddl.auto" value="update" />
...
</properties>
</persistence-unit>
</persistence>
[META-INF/orm.xml]
Code:
<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_2_0.xsd"
version="2.0">
<access>FIELD</access>
<entity class="party.model.Party">
<table name="PARTY" schema="MODEL" />
<inheritance strategy="JOINED" />
<attributes>
<id name="key">
<column name="PARTY_ID" />
<generated-value />
</id>
<one-to-many name="addresses" fetch="EAGER">
<join-column name="FK_PARTY" referenced-column-name="PARTY_ID" nullable="false" />
<cascade>
<cascade-persist />
<cascade-remove />
</cascade>
</one-to-many>
</attributes>
</entity>
...
</entity-mappings>
I made Spring's style DAO and Service in order to do my business stuff and everything works fine (databases tables created, CRUD operations works just fine, etc).
But when I specify the ORM file within the persistence.xml file, I get an exception when the mapping file is being read. Here is the new persistence.xml configuration (in fact, the only difference is the added mapping-file element):
[META-INF/persistence.xml]
Code:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.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_2_0.xsd ">
<persistence-unit name="party" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<mapping-file>META-INF/orm.xml</mapping-file>
<class>party.model.Party</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
<property name="hibernate.hbm2ddl.auto" value="update" />
...
</properties>
</persistence-unit>
</persistence>
While debbugging, I found out that the mapping file specified by this element is parsed by the
org.hibernate.ejb.Ejb3Configuration class. Within the
Ejb3Configuration.addXMLEntities(List<String>, PersistenceUnitInfo, List<String>) method, we can see that the XML parsing feature for validation specify the
http://java.sun.com/xml/ns/persistence/orm orm_1_0.xsd schema but mine is the 2.0 specification (
http://java.sun.com/xml/ns/persistence/ ... ce_2_0.xsd). So I get the following exception when the orm.xml is being parsed :
Code:
141 [main] INFO org.hibernate.ejb.Ejb3Configuration - Processing PersistenceUnitInfo [
name: party
...]
162923 [main] ERROR org.hibernate.util.XMLHelper - Error parsing XML: XML InputStream(4) cvc-complex-type.3.1: Value '2.0' of attribute 'version' of element 'entity-mappings' is not valid with respect to the corresponding attribute use. Attribute 'version' has a fixed value of '1.0'.
2010-06-04 09:31:58 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry destroySingletons
Since the schema may evolve over time, I guess this is really a bug : the validation schema should not be hardcoded, no ?