Jump to: navigation, search

Difference between revisions of "EclipseLink/UserGuide/JPA/Basic JPA Development/Configuration/JPA/orm.xml"

(12 intermediate revisions by 2 users not shown)
Line 2: Line 2:
 
|info=y
 
|info=y
 
|toc=n
 
|toc=n
 +
|api=y
 +
|apis=
 +
*[http://java.sun.com/xml/ns/persistence/orm_2_0.xsd orm.xml]
 
|eclipselink=y
 
|eclipselink=y
 
|eclipselinktype=JPA}}
 
|eclipselinktype=JPA}}
 
=Specifying Object-Relational Mappings Using orm.xml=
 
=Specifying Object-Relational Mappings Using orm.xml=
Use the '''orm.xml''' file to apply the metadata to the persistence unit. This metadata is a union of all the mapping files and the annotations (if there is no '''xml-mapping-metadata-complete''' element). If you use one mapping '''orm.xml''' file for your metadata and place this file in a META-INF directory on the classpath, then you do not need to explicitly list it. The EclipseLink persistence provider will automatically search for this file ('''orm.xml''') and use it.  
+
Use the <tt>orm.xml</tt> file to apply the metadata to the persistence unit. This metadata is a union of all the mapping files and the annotations (if there is no <tt>xml-mapping-metadata-complete</tt> element). If you use one mapping <tt>orm.xml</tt> file for your metadata and place this file in a <tt>META-INF</tt> directory on the classpath, then you do not need to explicitly list it. The EclipseLink persistence provider will automatically search for this file (<tt>orm.xml</tt>) and use it.  
 +
 
 +
The schema for the JPA 2.0 orm.xml is [http://java.sun.com/xml/ns/persistence/orm_2_0.xsd <tt>orm_2_0.xsd</tt>].
 +
 
 +
If you use a different name for your mapping files or place them in a different location, you must list them in the <tt>mapping-file</tt> element of the [[EclipseLink/UserGuide/JPA/Basic_JPA_Development/Configuration/JPA/persistence.xml|<tt>persistence.xml</tt>]] file, as shown here:
  
If you use a different name for your mapping files or place them in a different location, you must list them in the '''mapping-file''' element of the [[EclipseLink/UserGuide/JPA/Basic_JPA_Development/Configuration/JPA/persistence.xml|persistence.xml file]], as shown here:
 
 
<source lang="xml">
 
<source lang="xml">
 
  <persistence-unit name="EmployeeService">
 
  <persistence-unit name="EmployeeService">
Line 15: Line 21:
 
</source>
 
</source>
 
{{EclipseLink_Note
 
{{EclipseLink_Note
|note=In this example, the '''orm.xml''' file is not listed -- the persistence provider finds it by default.  
+
|note=In this example, the <tt>orm.xml</tt> file is not listed -- the persistence provider finds it by default.  
 
}}
 
}}
  
 +
The following is an example of an <tt>orm.xml</tt> file.
  
 +
<source lang="xml">
 +
<?xml version="1.0" encoding="UTF-8"?>
 +
<entity-mappings version="2.0"
 +
        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 orm_2_0.xsd">
 +
    <description>The minimal mappings for a persistent entity in XML.</description>
 +
    <entity name="Employee" class="org.acme.Employee" access="FIELD">
 +
        <attributes>
 +
            <id name="id"/>
 +
        </attributes>
 +
    </entity>
 +
</entity-mappings>
 +
</source>
 +
 +
==XML Schema Validation==
 +
By default the content of your orm XML file is not validated against the JPA orm XML schema.
 +
 +
During development it is a good idea to validate your orm XML file against the schema to ensure it is valid.
 +
In EclipseLink orm XML schema validation can be enabled using the persistence unit property <code>"eclipselink.orm.validate.schema"</code> in your <code>persistence.xml</code> file.
 +
 +
<source lang="xml">
 +
<property name="eclipselink.orm.validate.schema" value="true"/>
 +
</source>
  
 
==Advantages and Disadvantages of Using XML==
 
==Advantages and Disadvantages of Using XML==
Line 32: Line 63:
  
 
{{EclipseLink_JPA
 
{{EclipseLink_JPA
|previous=[[EclipseLink/UserGuide/JPA/Basic JPA Development/Configuration/JPA/Annotations|Annotations]]
+
|previous=[[EclipseLink/UserGuide/JPA/Basic JPA Development/Configuration/JPA/Annotations|Adding Metadata Using Annotations]]
|next=[[EclipseLink/UserGuide/JPA/Basic JPA Development/Configuration/JPA/Defaults|Defaults]]
+
|next=[[EclipseLink/UserGuide/JPA/Basic JPA Development/Configuration/JPA/Defaults|About the Default Configuration Values]]
 
|up=[[EclipseLink/UserGuide/JPA/Basic JPA Development/Configuration|Configuration]]
 
|up=[[EclipseLink/UserGuide/JPA/Basic JPA Development/Configuration|Configuration]]
 
|version=2.2.0 DRAFT}}
 
|version=2.2.0 DRAFT}}

Revision as of 08:45, 23 June 2011

EclipseLink JPA

link="http://wiki.eclipse.org/EclipseLink"
EclipseLink
Website
Download
Community
Mailing ListForumsIRC
Bugzilla
Open
Help Wanted
Bug Day
Contribute
Browse Source

Elug api package icon.png Key API

Specifying Object-Relational Mappings Using orm.xml

Use the orm.xml file to apply the metadata to the persistence unit. This metadata is a union of all the mapping files and the annotations (if there is no xml-mapping-metadata-complete element). If you use one mapping orm.xml file for your metadata and place this file in a META-INF directory on the classpath, then you do not need to explicitly list it. The EclipseLink persistence provider will automatically search for this file (orm.xml) and use it.

The schema for the JPA 2.0 orm.xml is orm_2_0.xsd.

If you use a different name for your mapping files or place them in a different location, you must list them in the mapping-file element of the persistence.xml file, as shown here:

 <persistence-unit name="EmployeeService">
     <jta-data-source>jdbc/EmployeeServiceDS</jta-data-source>
     <mapping-file>META-INF/employee_service_queries.xml</mapping-file>
 </persistence-unit>

Elug note icon.png

Note: In this example, the orm.xml file is not listed -- the persistence provider finds it by default.

The following is an example of an orm.xml file.

<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings version="2.0"
        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 orm_2_0.xsd">
    <description>The minimal mappings for a persistent entity in XML.</description>
    <entity name="Employee" class="org.acme.Employee" access="FIELD">
        <attributes>
            <id name="id"/>
        </attributes>
    </entity>
</entity-mappings>

XML Schema Validation

By default the content of your orm XML file is not validated against the JPA orm XML schema.

During development it is a good idea to validate your orm XML file against the schema to ensure it is valid. In EclipseLink orm XML schema validation can be enabled using the persistence unit property "eclipselink.orm.validate.schema" in your persistence.xml file.

<property name="eclipselink.orm.validate.schema" value="true"/>

Advantages and Disadvantages of Using XML

Some advantages of using XML instead of Annotations include:

  • No coupling between the metadata and the source code
  • Compliance with the existing, pre-EJB 3.0 development process
  • Support in IDEs and source control systems

The main disadvantages of mapping with XML include:

  • Its inherent complexitiy (when compared to annotations)
  • The need for replication of the code context


Eclipselink-logo.gif
Version: 2.2.0 DRAFT
Other versions...