Notice: This Wiki is now read only and edits are no longer possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.
Difference between revisions of "EclipseLink/Examples/DBWS/DBWSIntermediateAttribute"
(12 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
<css> | <css> | ||
− | .source-java5 {border- | + | .source-java5 {padding:4px;border:1px solid black; background-color: white;} |
+ | .source-xml {padding:4px;border:1px solid black; background-color: white;} | ||
+ | .source-text {padding:4px;border:1px solid black; background-color: white;} | ||
</css> | </css> | ||
+ | __NOTOC__ | ||
+ | |||
== Naming Convention for schema elements == | == Naming Convention for schema elements == | ||
As seen in the Basic Table example, the {{:EclipseLink/Examples/DBWS/DBWSBasicTable#gen_schema}} | As seen in the Basic Table example, the {{:EclipseLink/Examples/DBWS/DBWSBasicTable#gen_schema}} | ||
Line 40: | Line 44: | ||
=== User-Provided NamingConventionTransformer === | === User-Provided NamingConventionTransformer === | ||
− | It is possible to change an <element-tag> to an "attribute" with a custom transformer. The user sub-classes <tt>o.e.p.tools.dbws.DefaultNamingConventionTransformer</tt>. For example, | + | It is possible to change an <element-tag> to an "attribute" with a custom transformer. The user sub-classes <tt>o.e.p.tools.dbws.DefaultNamingConventionTransformer</tt>. <br/> |
+ | For example, given the table <tt>SIMPLE_TABLE</tt> | ||
<source lang=sql enclose="div"> | <source lang=sql enclose="div"> | ||
CREATE TABLE SIMPLE_TABLE ( | CREATE TABLE SIMPLE_TABLE ( | ||
Line 48: | Line 53: | ||
PRIMARY KEY (ID) | PRIMARY KEY (ID) | ||
) | ) | ||
− | </source> | + | </source> the following <code>NamingConventionTransformer</code> will: |
− | + | ||
* name the schema element '<tt>simple</tt>' | * name the schema element '<tt>simple</tt>' | ||
* make the element names lowercase | * make the element names lowercase | ||
− | * have the <tt>ID</tt> field represented as an attribute | + | * have the <tt>ID</tt> field represented as an attribute |
− | + | ||
<source lang=java5> | <source lang=java5> | ||
+ | package foo.bar.blah; | ||
+ | |||
import org.eclipse.persistence.tools.dbws.DefaultNamingConventionTransformer; | import org.eclipse.persistence.tools.dbws.DefaultNamingConventionTransformer; | ||
import static org.eclipse.persistence.tools.dbws.NamingConventionTransformer.ElementStyle.ATTRIBUTE; | import static org.eclipse.persistence.tools.dbws.NamingConventionTransformer.ElementStyle.ATTRIBUTE; | ||
Line 76: | Line 81: | ||
if ("id".equalsIgnoreCase(elementName)) { | if ("id".equalsIgnoreCase(elementName)) { | ||
return ATTRIBUTE; | return ATTRIBUTE; | ||
− | |||
− | |||
− | |||
} | } | ||
return ELEMENT; | return ELEMENT; | ||
Line 84: | Line 86: | ||
} | } | ||
</source> | </source> | ||
+ | produces the schema: | ||
+ | <source lang=xml enclose="div"> | ||
+ | <?xml version="1.0" encoding="UTF-8"?> | ||
+ | <xsd:schema targetNamespace="urn:simple" xmlns="urn:simple" elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> | ||
+ | <xsd:complexType name="simple"> | ||
+ | <xsd:sequence> | ||
+ | <xsd:element name="name" type="xsd:string" minOccurs="0"/> | ||
+ | <xsd:element name="since" type="xsd:date" minOccurs="0"/> | ||
+ | </xsd:sequence> | ||
+ | <xsd:attribute name="id" type="xsd:decimal" use="required"/> | ||
+ | </xsd:complexType> | ||
+ | <xsd:element name="simple" type="simple"/> | ||
+ | </xsd:schema> | ||
+ | </source> | ||
+ | |||
+ | The user <b>must</b> package their custom transformer in a <tt>.jar</tt>-file and identify it as a service by creating a file called <tt>org.eclipse.persistence.tools.dbws.NamingConventionTransformer</tt> in the <tt>META-INF/services</tt> directory with the full-qualified classname of the transformer: | ||
+ | <source lang="text" enclose="div"> | ||
+ | customtransformer.jar | ||
+ | | | ||
+ | +---META-INF | ||
+ | | | MANIFEST.MF | ||
+ | | | | ||
+ | | \---services | ||
+ | | org.eclipse.persistence.tools.dbws.NamingConventionTransformer | ||
+ | contains 1 line: | ||
+ | foo.bar.blah.MyNamingConventionTransformer | ||
+ | | | ||
+ | \---foo | ||
+ | \---bar | ||
+ | \---blah | ||
+ | | MyNamingConventionTransformer.class | ||
+ | </source> | ||
+ | <br /> | ||
+ | <b>NB</b> - the custom transformer's <tt>customtransformer.jar</tt> file <b>must</b> be <i>ahead</i> of the <tt>eclipselink-dbwsutils.jar</tt> in order for it to be picked up by the <code>DBWSBuilder</code> utility. In addition, the <code>DBWSBuilder</code> utility <b>always</b> re-builds the <i>Chain-of-Responsibility</i> with the <tt>SQLX2003Transformer</tt> at the end to ensure that any transformer, default or custom, produces legal characters according to the SQL/X(2003) specification. | ||
+ | |||
+ | [[Category:EclipseLink/Example/DBWS]] |
Latest revision as of 10:06, 7 October 2010
Naming Convention for schema elements
As seen in the Basic Table example, the DBWSBuilder-generated eclipselink-dbws-schema.xsd file derives <element-tag> names from the Database table metadata:
<xsd:schema
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
>
<xsd:complexType name="empType">
<xsd:sequence>
<xsd:element name="empno" type="xsd:int" xsi:nil="false"/>
<xsd:element name="ename" type="xsd:string" xsi:nil="true"/>
<xsd:element name="job" type="xsd:string" xsi:nil="true"/>
<xsd:element name="mgr" type="xsd:int" minOccurs="0" xsi:nil="true"/>
<xsd:element name="hiredate" type="xsd:dateTime" xsi:nil="true"/>
<xsd:element name="sal" type="xsd:decimal" xsi:nil="true"/>
<xsd:element name="comm" type="xsd:int" minOccurs="0" xsi:nil="true"/>
<xsd:element name="deptno" type="xsd:int" xsi:nil="true"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
Converting the database metadata into the names of <element-tag>'s is the job of o.e.p.tools.dbws.NamingConventionTransformer
's
public interface NamingConventionTransformer { public enum ElementStyle { ELEMENT, ATTRIBUTE, NONE }; public String generateSchemaName(String tableName); public String generateElementAlias(String originalElementName); public ElementStyle styleForElement(String originalElementName); }
In the eclipselink-dbwsutils.jar, the built-in transformers form a Chain-of-Responsibility
In the o.e.p.tools.dbws.NamingConventionTransformer services file, the transformer chain is listed in order:
|
+---META-INF
| | MANIFEST.MF
| |
| \---services
| org.eclipse.persistence.tools.dbws.NamingConventionTransformer
contains 3 lines:
org.eclipse.persistence.tools.dbws.ToLowerTransformer
org.eclipse.persistence.tools.dbws.TypeSuffixTransformer
org.eclipse.persistence.tools.dbws.SQLX2003Transformer
User-Provided NamingConventionTransformer
It is possible to change an <element-tag> to an "attribute" with a custom transformer. The user sub-classes o.e.p.tools.dbws.DefaultNamingConventionTransformer.
For example, given the table SIMPLE_TABLE
ID NUMERIC NOT NULL,
NAME VARCHAR(25),
SINCE DATE,
PRIMARY KEY (ID)
)
NamingConventionTransformer
will:
- name the schema element 'simple'
- make the element names lowercase
- have the ID field represented as an attribute
package foo.bar.blah; import org.eclipse.persistence.tools.dbws.DefaultNamingConventionTransformer; import static org.eclipse.persistence.tools.dbws.NamingConventionTransformer.ElementStyle.ATTRIBUTE; import static org.eclipse.persistence.tools.dbws.NamingConventionTransformer.ElementStyle.ELEMENT; import static org.eclipse.persistence.tools.dbws.NamingConventionTransformer.ElementStyle.NONE; public class MyNamingConventionTransformer extends DefaultNamingConventionTransformer { @Override public String generateSchemaAlias(String tableName) { return super.generateSchemaAlias("simple"); } @Override public String generateElementAlias(String originalElementName) { return super.generateElementAlias(originalElementName.toLowerCase()); } @Override public ElementStyle styleForElement(String elementName) { if ("id".equalsIgnoreCase(elementName)) { return ATTRIBUTE; } return ELEMENT; } }
produces the schema:
<xsd:schema targetNamespace="urn:simple" xmlns="urn:simple" elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:complexType name="simple">
<xsd:sequence>
<xsd:element name="name" type="xsd:string" minOccurs="0"/>
<xsd:element name="since" type="xsd:date" minOccurs="0"/>
</xsd:sequence>
<xsd:attribute name="id" type="xsd:decimal" use="required"/>
</xsd:complexType>
<xsd:element name="simple" type="simple"/>
</xsd:schema>
The user must package their custom transformer in a .jar-file and identify it as a service by creating a file called org.eclipse.persistence.tools.dbws.NamingConventionTransformer in the META-INF/services directory with the full-qualified classname of the transformer:
|
+---META-INF
| | MANIFEST.MF
| |
| \---services
| org.eclipse.persistence.tools.dbws.NamingConventionTransformer
contains 1 line:
foo.bar.blah.MyNamingConventionTransformer
|
\---foo
\---bar
\---blah
| MyNamingConventionTransformer.class
NB - the custom transformer's customtransformer.jar file must be ahead of the eclipselink-dbwsutils.jar in order for it to be picked up by the DBWSBuilder
utility. In addition, the DBWSBuilder
utility always re-builds the Chain-of-Responsibility with the SQLX2003Transformer at the end to ensure that any transformer, default or custom, produces legal characters according to the SQL/X(2003) specification.