mirror of https://github.com/apache/openjpa.git
OPENJPA-240 add XMLMapping documentation to ref_guide_mapping.xml
Committing Catalina's openjpa-project.patch git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@560330 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
8c1710ed63
commit
8586e0fef3
|
@ -2372,6 +2372,302 @@ supported by the database.
|
||||||
</itemizedlist>
|
</itemizedlist>
|
||||||
</section>
|
</section>
|
||||||
</section>
|
</section>
|
||||||
|
<section id="ref_guide_xmlmapping">
|
||||||
|
<title>
|
||||||
|
XML Column Mapping
|
||||||
|
</title>
|
||||||
|
<indexterm zone="ref_guide_xmlmapping">
|
||||||
|
<primary>
|
||||||
|
mapping metadata
|
||||||
|
</primary>
|
||||||
|
<secondary>
|
||||||
|
xml column mapping
|
||||||
|
</secondary>
|
||||||
|
</indexterm>
|
||||||
|
<indexterm zone="ref_guide_xmlmapping">
|
||||||
|
<primary>
|
||||||
|
xml mapping column
|
||||||
|
</primary>
|
||||||
|
</indexterm>
|
||||||
|
<para>
|
||||||
|
DB2, Oracle and SQLServer support XML column types and
|
||||||
|
XPath queries and indexes over these columns.OpenJPA supports mapping of an
|
||||||
|
entity property mapped to an XML column.
|
||||||
|
</para>
|
||||||
|
<para>
|
||||||
|
Annotate the entity property using the XMLValueHandler strategy:
|
||||||
|
</para>
|
||||||
|
<programlisting>
|
||||||
|
@Persistent
|
||||||
|
@Strategy("org.apache.openjpa.jdbc.meta.strats.XMLValueHandler")
|
||||||
|
</programlisting>
|
||||||
|
<para>
|
||||||
|
The default fetch type is EAGER but can be changed to LAZY by using:
|
||||||
|
</para>
|
||||||
|
<programlisting>
|
||||||
|
@Persistence(fetch=FetchType.LAZY)
|
||||||
|
</programlisting>
|
||||||
|
<para>
|
||||||
|
The entity property class is required to have
|
||||||
|
jaxb binding annotations. This is produced when the classes are generated
|
||||||
|
from an xml schema using the jaxb generator XJC.Ensure that <classname>@XmlRootElement</classname>
|
||||||
|
appears in the root class. In some case this annotation needs to be added manually if it is missing.
|
||||||
|
</para>
|
||||||
|
<para>
|
||||||
|
The jaxb jar files must be on the application classpath (jaxb-api.jar,
|
||||||
|
jaxb-impl.jar, jsr173_1.0_api.jar or equivalent).
|
||||||
|
</para>
|
||||||
|
<para>
|
||||||
|
EJB Query path expressions can navigate into the mapped class and its
|
||||||
|
subfields to any level.
|
||||||
|
</para>
|
||||||
|
<para>
|
||||||
|
The path expression is rewritten into an equivalent XPATH expression using SQL
|
||||||
|
XML functions.
|
||||||
|
</para>
|
||||||
|
<para>
|
||||||
|
The path expression must be single valued.Path expressions over xml
|
||||||
|
mapped classes can only be used in WHERE as an operand to a simple predicate
|
||||||
|
(= <> < > >= <=).
|
||||||
|
</para>
|
||||||
|
<para>
|
||||||
|
Path expressions over XML mapped fields can not be:
|
||||||
|
</para>
|
||||||
|
<itemizedlist>
|
||||||
|
<listitem>
|
||||||
|
<para>
|
||||||
|
an input to a EJB query scalar function
|
||||||
|
</para>
|
||||||
|
</listitem>
|
||||||
|
<listitem>
|
||||||
|
<para>
|
||||||
|
an operand of BETWEEN, IS NULL, LIKE or IN predicate
|
||||||
|
</para>
|
||||||
|
</listitem>
|
||||||
|
<listitem>
|
||||||
|
<para>
|
||||||
|
used to project out subfields in the SELECT clause
|
||||||
|
</para>
|
||||||
|
</listitem>
|
||||||
|
<listitem>
|
||||||
|
<para>
|
||||||
|
used in the FROM , GROUP BY, HAVING, ORDER BY clauses
|
||||||
|
</para>
|
||||||
|
</listitem>
|
||||||
|
</itemizedlist>
|
||||||
|
<para>
|
||||||
|
XML schema must not contain namespace declarations. The EJB query path
|
||||||
|
expressions can not refer to java fields generated from XML ANY type or
|
||||||
|
XML mixed element types.
|
||||||
|
</para>
|
||||||
|
<para>
|
||||||
|
The datatype generated by JAXB must be a valid EJB query type
|
||||||
|
to use the property in an EJB query predicate.
|
||||||
|
</para>
|
||||||
|
<para>
|
||||||
|
Shown below is a sample XML schema <link linkend="ref_guide_xmlmapping_myaddress">myaddress.xsd</link>,
|
||||||
|
in which the JPA entity Order has <classname><shipAddress></classname> persistent field that maps to an XML column.
|
||||||
|
</para>
|
||||||
|
<example id="ref_guide_xmlmapping_myaddress">
|
||||||
|
<title>
|
||||||
|
myaddress.xsd
|
||||||
|
</title>
|
||||||
|
<programlisting>
|
||||||
|
<?xml version="1.0" ?>
|
||||||
|
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" >
|
||||||
|
|
||||||
|
<xs:complexType name="Address">
|
||||||
|
<xs:sequence>
|
||||||
|
<xs:element name="Name" type="xs:string" />
|
||||||
|
<xs:element name="Street" type="xs:string"
|
||||||
|
minOccurs="1" maxOccurs="3" />
|
||||||
|
<xs:element name="City" type="xs:string" />
|
||||||
|
</xs:sequence>
|
||||||
|
</xs:complexType>
|
||||||
|
|
||||||
|
<xs:complexType name="CAN_Address">
|
||||||
|
<xs:complexContent>
|
||||||
|
<xs:extension base="Address">
|
||||||
|
<xs:sequence>
|
||||||
|
<xs:element name="Province" type="xs:string" />
|
||||||
|
<xs:element name="PostalCode" type="xs:string" />
|
||||||
|
</xs:sequence>
|
||||||
|
</xs:extension>
|
||||||
|
</xs:complexContent>
|
||||||
|
</xs:complexType>
|
||||||
|
|
||||||
|
<xs:simpleType name="USPS_ZIP">
|
||||||
|
<xs:restriction base="xs:integer">
|
||||||
|
<xs:minInclusive value="01000" />
|
||||||
|
<xs:maxInclusive value="99999" />
|
||||||
|
</xs:restriction>
|
||||||
|
</xs:simpleType>
|
||||||
|
|
||||||
|
<xs:complexType name="USA_Address">
|
||||||
|
<xs:complexContent>
|
||||||
|
<xs:extension base="Address">
|
||||||
|
<xs:sequence>
|
||||||
|
<xs:element name="State" type="xs:string" />
|
||||||
|
<xs:element name="ZIP" type="USPS_ZIP" />
|
||||||
|
</xs:sequence>
|
||||||
|
</xs:extension>
|
||||||
|
</xs:complexContent>
|
||||||
|
</xs:complexType>
|
||||||
|
|
||||||
|
<xs:element name="MailAddress" type="Address" />
|
||||||
|
<xs:element name="AddrCAN" type="CAN_Address"
|
||||||
|
substitutionGroup="MailAddress" />
|
||||||
|
<xs:element name="AddrUSA" type="USA_Address"
|
||||||
|
substitutionGroup="MailAddress" />
|
||||||
|
</xs:schema>
|
||||||
|
</programlisting>
|
||||||
|
</example>
|
||||||
|
<para>
|
||||||
|
Java classes <link linkend="ref_guide_xmlmapping_address">Address</link>,
|
||||||
|
<link linkend="ref_guide_xmlmapping_usaaddress">USAAddress</link> and
|
||||||
|
<link linkend="ref_guide_xmlmapping_canaddress">CANAddress</link>
|
||||||
|
are produced using jaxb XJC generator from myaddress schema.
|
||||||
|
</para>
|
||||||
|
<example id="ref_guide_xmlmapping_address">
|
||||||
|
<title>
|
||||||
|
Address.Java
|
||||||
|
</title>
|
||||||
|
<programlisting>
|
||||||
|
...
|
||||||
|
@XmlRootElement
|
||||||
|
@XmlAccessorType(XmlAccessType.FIELD)
|
||||||
|
@XmlType(name = "Address", propOrder = {
|
||||||
|
"name",
|
||||||
|
"street",
|
||||||
|
"city"
|
||||||
|
})
|
||||||
|
public class Address {
|
||||||
|
@XmlElement(name = "Name", required = true)
|
||||||
|
protected String name;
|
||||||
|
@XmlElement(name = "Street", required = true)
|
||||||
|
protected List<String> street;
|
||||||
|
@XmlElement(name = "City", required = true)
|
||||||
|
protected String city;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Getter and Setter methods.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
...
|
||||||
|
}
|
||||||
|
</programlisting>
|
||||||
|
</example>
|
||||||
|
<example id="ref_guide_xmlmapping_usaaddress">
|
||||||
|
<title>
|
||||||
|
USAAddress.java
|
||||||
|
</title>
|
||||||
|
<programlisting>
|
||||||
|
...
|
||||||
|
@XmlRootElement
|
||||||
|
@XmlAccessorType(XmlAccessType.FIELD)
|
||||||
|
@XmlType(name = "USA_Address", propOrder = {
|
||||||
|
"state",
|
||||||
|
"zip"
|
||||||
|
})
|
||||||
|
public class USAAddress
|
||||||
|
extends Address
|
||||||
|
{
|
||||||
|
|
||||||
|
@XmlElement(name = "State")
|
||||||
|
protected String state;
|
||||||
|
@XmlElement(name = "ZIP")
|
||||||
|
protected int zip;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Getter and Setter methods.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
...
|
||||||
|
}
|
||||||
|
</programlisting>
|
||||||
|
</example>
|
||||||
|
<example id="ref_guide_xmlmapping_canaddress">
|
||||||
|
<title>
|
||||||
|
CANAddress.java
|
||||||
|
</title>
|
||||||
|
<programlisting>
|
||||||
|
...
|
||||||
|
@XmlRootElement
|
||||||
|
@XmlAccessorType(XmlAccessType.FIELD)
|
||||||
|
@XmlType(name = "CAN_Address", propOrder = {
|
||||||
|
"province",
|
||||||
|
"postalCode"
|
||||||
|
})
|
||||||
|
public class CANAddress
|
||||||
|
extends Address
|
||||||
|
{
|
||||||
|
|
||||||
|
@XmlElement(name = "Province")
|
||||||
|
protected String province;
|
||||||
|
@XmlElement(name = "PostalCode")
|
||||||
|
protected String postalCode;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Getter and Setter methods.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
...
|
||||||
|
}
|
||||||
|
</programlisting>
|
||||||
|
</example>
|
||||||
|
<example id="ref_guide_xmlmapping_annorder">
|
||||||
|
<title>
|
||||||
|
Showing annotated Order entity with XML mapping strategy
|
||||||
|
</title>
|
||||||
|
<programlisting>
|
||||||
|
@Entity
|
||||||
|
public class Order {
|
||||||
|
@Id private into id;
|
||||||
|
@Persistent
|
||||||
|
@Strategy ("org.apache.openjpa.jdbc.meta.strats.XMLValueHandler")
|
||||||
|
private Address shipAddress;
|
||||||
|
...
|
||||||
|
}
|
||||||
|
</programlisting>
|
||||||
|
</example>
|
||||||
|
<example id="ref_guide_xmlmapping_createorder">
|
||||||
|
<title>
|
||||||
|
Showing creation of Order Entity having shipAddress mapped to XML column
|
||||||
|
</title>
|
||||||
|
<programlisting>
|
||||||
|
...
|
||||||
|
myaddress.ObjectFactory addressFactory = new myaddress.ObjectFactory();
|
||||||
|
Customer c1 = new Customer();
|
||||||
|
c1.setCid( new Customer.CustomerKey("USA", 1) );
|
||||||
|
c1.setName("Harry's Auto");
|
||||||
|
Order o1 = new Order( 850, false, c1);
|
||||||
|
USAAddress addr1 = addressFactory.createUSAAddress();
|
||||||
|
addr1.setCity("San Jose");
|
||||||
|
addr1.setState("CA");
|
||||||
|
addr1.setZIP(new Integer("95141"));
|
||||||
|
addr1.getStreet().add("12500 Monterey");
|
||||||
|
addr1.setName( c1.getName());
|
||||||
|
o1.setShipAddress(addr1);
|
||||||
|
em.persist(o1);
|
||||||
|
...
|
||||||
|
</programlisting>
|
||||||
|
</example>
|
||||||
|
<example id="ref_guide_xmlmapping_ejbquery">
|
||||||
|
<title>
|
||||||
|
Sample EJB Queries for XML Column mapping
|
||||||
|
</title>
|
||||||
|
<programlisting>
|
||||||
|
. select o from Order o where o.shipAddress.city = "San Jose" or
|
||||||
|
o.shipAddress.city = "San Francisco" (OK)
|
||||||
|
|
||||||
|
. select o.shipaAddress from Order o (OK)
|
||||||
|
|
||||||
|
. select o.shipAddress.city from Order o (INVALID)
|
||||||
|
|
||||||
|
. select o from Order o where o.shipAddress.street = "San Jose" (INVALID multi valued)
|
||||||
|
</programlisting>
|
||||||
|
</example>
|
||||||
|
</section>
|
||||||
</section>
|
</section>
|
||||||
<section id="ref_guide_mapping_limits">
|
<section id="ref_guide_mapping_limits">
|
||||||
<title>
|
<title>
|
||||||
|
|
Loading…
Reference in New Issue