| <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" |
| "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
| <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> |
| <head> |
| <!-- Run date = July 23, 2008 8:59:41 --> |
| <meta http-equiv="Content-Type" content="text/html; charset=us-ascii" /> |
| <title>Creating a Java persistent entity with persistent fields</title> |
| |
| <meta http-equiv="Content-Style-Type" content="text/css" /> |
| <meta http-equiv="Content-Script-Type" content="text/javascript" /> |
| <meta name="generator" content="Oracle DARB XHTML Converter (Mode = ohj/ohw) - Version 5.0" /> |
| <meta name="date" content="2005-07-10T12:57:20+08:00" /> |
| <meta name="robots" content="noarchive" /> |
| <meta name="doctitle" content="Creating a Java persistent entity with persistent fields" /> |
| <meta name="relnum" content="Release 2.0" /> |
| <meta name="copyright" content="Copyright (c) 2000, 2008 oracle . All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse Public License v1.0 which accompanies this distribution, and is available at http://www.eclipse.org/legal/epl-v10.html. Contributors: Oracle - initial API and implementation" /> |
| <link rel="copyright" href="dcommon/html/cpyr.htm" title="Copyright" type="text/html" /> |
| <link rel="stylesheet" href="dcommon/css/blafdoc.css" title="Oracle BLAFDoc" type="text/css" /> |
| <!-- start--> |
| <!-- contents --> |
| </head> |
| <body> |
| <p><a id="BABFGDDG" name="BABFGDDG"></a></p> |
| <div class="sect2"><!-- infolevel="all" infotype="General" --> |
| <h1>Creating a Java persistent entity with persistent fields</h1> |
| <p><a id="sthref10" name="sthref10"></a>This quick start shows how to create a new persistent Java entity. We will create an entity to associate with a database table. You will also need to add the ADDRESS table to your database.</p> |
| <ol start="1"> |
| <li> |
| <p>Select the JPA project in the Navigator or Package Explorer and then click <span class="bold">New > Other</span>. The Select a Wizard dialog appears.</p> |
| </li> |
| <li> |
| <p>Select <span class="bold">Entity</span> and then click <span class="bold">Next</span>. The Entity Class page appears.</p> |
| </li> |
| <li> |
| <p>Enter the package name (such as <code>quickstart.demo.model</code>), the class name (such as <code>Address</code>) and then click <span class="bold">Next</span>. The <a href="reference004.htm#CIADECIA">Entity Properties page</a> appears, which enables you to define the persistence fields, which you will map to the columns of a database table.</p> |
| </li> |
| <li> |
| <p><a id="sthref11" name="sthref11"></a><a id="sthref12" name="sthref12"></a>Use the Entity Fields dialog (invoked by clicking <span class="bold">Add</span>) to add persistence fields to the Address class:</p> |
| <pre xml:space="preserve" class="oac_no_warn"> |
| private Long id; |
| private String city; |
| private String country; |
| private String stateOrProvince; |
| private String postalCode; |
| private String street; |
| </pre> |
| <div align="center"> |
| <div class="inftblnote"><br /> |
| <table class="Note oac_no_warn" summary="" cellpadding="3" cellspacing="0"> |
| <tbody> |
| <tr> |
| <td align="left"> |
| <p class="notep1">Note:</p> |
| You will also need to add the following columns to the ADDRESS database table: |
| <pre xml:space="preserve" class="oac_no_warn"> |
| NUMBER(10,0) ADDRESS_ID (primary key) |
| VARCHAR2(80) PROVINCE |
| VARCHAR2(80) COUNTRY |
| VARCHAR2(20) P_CODE |
| VARCHAR2(80) STREET |
| VARCHAR2(80) CITY |
| </pre> |
| <pre xml:space="preserve" class="oac_no_warn"> |
| </pre></td> |
| </tr> |
| </tbody> |
| </table> |
| <br /></div> |
| <!-- class="inftblnote" --></div> |
| </li> |
| <li> |
| <p>Click <span class="bold">Finish</span>. With the Create JPA Entity completed, Eclipse displays the <span class="bold">Address</span> entity in the JPA Structure view.</p> |
| <p>Address.java includes the <code>@Entity</code> annotation, the persistence fields, as well as <code>getter</code> and <code>setter</code> methods for each of the fields.</p> |
| </li> |
| </ol> |
| <div class="figure"><a id="sthref13" name="sthref13"></a> |
| <p class="titleinfigure">Address Entity in Address.java</p> |
| <img src="img/java_editor_address.png" alt="Java editor with the Address entity." title="Java editor with the Address entity." /><br /></div> |
| <!-- class="figure" --> |
| <p>Eclipse also displays the <span class="bold">Address</span> entity in the JPA Structure view:</p> |
| <div class="figure"><a id="sthref14" name="sthref14"></a> |
| <p class="titleinfigure">Address Entity in the JPA Structure View</p> |
| <img src="img/address_java_JPA_structure_quickstart.png" alt="Address.java in the JPA Structure View." title="Address.java in the JPA Structure View." /><br /></div> |
| <!-- class="figure" --> |
| <ol start="1"> |
| <li> |
| <p>Select the <span class="gui-object-action">Address</span> class in the Package Explorer view.</p> |
| </li> |
| <li> |
| <p>In the <span class="gui-object-title">JPA Details</span> view, notice that Dali has automatically associated the ADDRESS database table with the entity because they are named identically.</p> |
| <div align="center"> |
| <div class="inftblnote"><br /> |
| <table class="Note oac_no_warn" summary="" cellpadding="3" cellspacing="0"> |
| <tbody> |
| <tr> |
| <td align="left"> |
| <p class="notep1">Note:</p> |
| Depending on your database connection type, you may need to specify the <span class="bold">Schema</span>.</td> |
| </tr> |
| </tbody> |
| </table> |
| <br /></div> |
| <!-- class="inftblnote" --></div> |
| <div class="figure"><a id="sthref15" name="sthref15"></a> |
| <p class="titleinfigure">JPA Details View for Address Entity</p> |
| <img src="img/address.java_jpa_details.png" alt="Address.java in the JPA Details view." title="Address.java in the JPA Details view." /><br /></div> |
| <!-- class="figure" --></li> |
| </ol> |
| <div align="center"> |
| <div class="inftblnotealso"><br /> |
| <table class="NoteAlso oac_no_warn" summary="" cellpadding="3" cellspacing="0"> |
| <tbody> |
| <tr> |
| <td align="left"> |
| <p class="notep1">Tip:</p> |
| After associating the entity with the database table, you should update the <code>persistence.xml</code> file to include this JPA entity. |
| <p>Right-click the <code>persistence.xml</code> file in the Package Explorer and select <span class="bold">JPA Tools > Synchronize Classes</span>. Dali adds the following to the <code>persistence.xml</code> file:</p> |
| <p><code><class>quickstart.demo.model.Address</class></code></p> |
| </td> |
| </tr> |
| </tbody> |
| </table> |
| <br /></div> |
| <!-- class="inftblnotealso" --></div> |
| <p>Now we are ready to map each fields in the Address class to a column in the database table.</p> |
| <ol start="1"> |
| <li> |
| <p>Select the <span class="gui-object-action">id</span> field in the JPA Details view.</p> |
| </li> |
| <li> |
| <p>Right click id and then select <span class="bold">Map As > id</span>.</p> |
| </li> |
| <li> |
| <p>In the JPA Details view, select <span class="bold">ADDRESS_ID</span> in the Name field:</p> |
| <div class="figure"><a id="sthref16" name="sthref16"></a> |
| <p class="titleinfigure">JPA Details View for the addressId Field</p> |
| <img src="img/address_id_details_quickstart.png" alt="The JPA Details view for the Address entity’s id attribute." title="The JPA Details view for the Address entity’s id attribute." /><br /></div> |
| <!-- class="figure" --> |
| <p>Eclipse adds the following annotations to the Address entity:</p> |
| <pre xml:space="preserve" class="oac_no_warn"> |
| @Id |
| @Column(name="ADDRESS_ID") |
| </pre></li> |
| <li> |
| <p>Map each of the following fields (as <span class="bold">Basic</span> mappings) to the appropriate database column:</p> |
| <div class="inftblhruleinformal"> |
| <table class="HRuleInformal" title="This table describes the mappings for each of the fields in the Address entity." summary="This table describes the mappings for each of the fields in the Address entity." dir="ltr" border="1" width="100%" frame="border" rules="all" cellpadding="3" cellspacing="0"> |
| <thead> |
| <tr align="left" valign="top"> |
| <th align="left" valign="bottom" id="r1c1-t8">Field</th> |
| <th align="left" valign="bottom" id="r1c2-t8">Map As</th> |
| <th align="left" valign="bottom" id="r1c3-t8">Database Column</th> |
| </tr> |
| </thead> |
| <tbody> |
| <tr align="left" valign="top"> |
| <td align="left" id="r2c1-t8" headers="r1c1-t8">city</td> |
| <td align="left" headers="r2c1-t8 r1c2-t8">Basic</td> |
| <td align="left" headers="r2c1-t8 r1c3-t8">CITY</td> |
| </tr> |
| <tr align="left" valign="top"> |
| <td align="left" id="r3c1-t8" headers="r1c1-t8">country</td> |
| <td align="left" headers="r3c1-t8 r1c2-t8">Basic</td> |
| <td align="left" headers="r3c1-t8 r1c3-t8">COUNTRY</td> |
| </tr> |
| <tr align="left" valign="top"> |
| <td align="left" id="r4c1-t8" headers="r1c1-t8">postalCode</td> |
| <td align="left" headers="r4c1-t8 r1c2-t8">Basic</td> |
| <td align="left" headers="r4c1-t8 r1c3-t8">P_CODE</td> |
| </tr> |
| <tr align="left" valign="top"> |
| <td align="left" id="r5c1-t8" headers="r1c1-t8">provinceOrState</td> |
| <td align="left" headers="r5c1-t8 r1c2-t8">Basic</td> |
| <td align="left" headers="r5c1-t8 r1c3-t8">PROVINCE</td> |
| </tr> |
| <tr align="left" valign="top"> |
| <td align="left" id="r6c1-t8" headers="r1c1-t8">street</td> |
| <td align="left" headers="r6c1-t8 r1c2-t8">Basic</td> |
| <td align="left" headers="r6c1-t8 r1c3-t8">STREET</td> |
| </tr> |
| </tbody> |
| </table> |
| <br /></div> |
| <!-- class="inftblhruleinformal" --></li> |
| </ol> |
| <p>Dali automatically maps some fields to the correct database column (such as the city field to the City column) if the names are identical.</p> |
| </div> |
| <!-- class="sect2" --> |
| <!-- class="sect1" --> |
| <!-- Start Footer --> |
| <div class="footer"> |
| <table class="simple oac_no_warn" summary="" cellspacing="0" cellpadding="0" width="100%"> |
| <tr> |
| <td align="left" width="86%"><a href="dcommon/html/cpyr.htm"><span class="copyrightlogo">Copyright © 2006, 2008, Oracle. All rights reserved.</span></a></td> |
| </tr> |
| </table> |
| </div> |
| <!-- class="footer" --> |
| </body> |
| </html> |