Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 51517dc5b8b159e864fbece8dcf874b2399a9580 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii" />
<meta http-equiv="Content-Style-Type" content="text/css" />
<meta http-equiv="Content-Script-Type" content="text/javascript" />
<title>Creating a Java persistent entity with persistent fields</title>
<meta name="copyright" content="Copyright (c) 2000, 2009 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" />
<meta name="generator" content="Oracle DARB XHTML Converter (Mode = ohj/ohw) - Version 1.0.11" />
<meta name="date" content="2012-09-11T7:44:3Z" />
<meta name="robots" content="noarchive" />
<meta name="doctitle" content="Creating a Java persistent entity with persistent fields" />
<meta name="relnum" content="Release 3.2" />
<meta name="partnum" content="Release 3.2" />
<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" />
</head>
<body>
<p class="betadraftsubtitle">Beta Draft: 2012-09-11</p>
<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="sthref12" name="sthref12"></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>
<li>
<p>Select the JPA project in the Navigator or Project Explorer and then click <span class="bold">New &gt; Other</span>. The Select a Wizard dialog appears.</p>
</li>
<li>
<p>Select <span class="bold">JPA &gt; Entity</span> and then click <span class="bold">Next</span>. The <a href="ref_EntityClassPage.htm#CIAFEIGF">Entity Class page</a> 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="ref_EntityPropertiesPage.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="sthref13" name="sthref13"></a><a id="sthref14" name="sthref14"></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>
<p>You will also need to add the following columns to the <code>ADDRESS</code> database table:</p>
<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></td>
</tr>
</tbody>
</table>
<br /></div>
<!-- class="inftblnote" --></div>
</li>
<li>
<p>Click <span class="bold">Finish</span>. With the Create JPA Entity wizard completed, Eclipse displays the <span class="bold">Address</span> entity in the JPA Structure view.</p>
</li>
</ol>
<p><code>Address.java</code> 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>
<div class="figure"><a id="sthref15" name="sthref15"></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="sthref16" name="sthref16"></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" -->
<p>After creating the entity, you must associate it with a database table.</p>
<ol>
<li>
<p>Select the <span class="gui-object-action">Address</span> class in the Project Explorer view.</p>
</li>
<li>
<p>In the <span class="gui-object-title">JPA Details</span> view, notice that Dali has automatically associated the <code>ADDRESS</code> database table with the entity because they are named identically.</p>
<div class="figure"><a id="sthref17" name="sthref17"></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" -->
<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>
<p>Depending on your database connection type, you may need to specify the <span class="bold">Schema</span>.</p>
</td>
</tr>
</tbody>
</table>
<br /></div>
<!-- class="inftblnote" --></div>
</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>
<p>After associating the entity with the database table, you should update the <code>persistence.xml</code> file to include this JPA entity.</p>
<p>Right-click the <code>persistence.xml</code> file in the Project Explorer and select <span class="bold">JPA Tools &gt; Synchronize Class List</span>. Dali adds the following to the <code>persistence.xml</code> file:</p>
<p><code>&lt;class&gt;quickstart.demo.model.Address&lt;/class&gt;</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>
<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 &gt; 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="sthref18" name="sthref18"></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&rsquo;s id attribute." title="The JPA Details view for the Address entity&rsquo;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="hsides" rules="rows" cellpadding="3" cellspacing="0">
<col width="*" />
<col width="33%" />
<col width="33%" />
<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">
<p><code>city</code></p>
</td>
<td align="left" headers="r2c1-t8 r1c2-t8">
<p>Basic</p>
</td>
<td align="left" headers="r2c1-t8 r1c3-t8">
<p><code>CITY</code></p>
</td>
</tr>
<tr align="left" valign="top">
<td align="left" id="r3c1-t8" headers="r1c1-t8">
<p><code>country</code></p>
</td>
<td align="left" headers="r3c1-t8 r1c2-t8">
<p>Basic</p>
</td>
<td align="left" headers="r3c1-t8 r1c3-t8">
<p><code>COUNTRY</code></p>
</td>
</tr>
<tr align="left" valign="top">
<td align="left" id="r4c1-t8" headers="r1c1-t8">
<p><code>postalCode</code></p>
</td>
<td align="left" headers="r4c1-t8 r1c2-t8">
<p>Basic</p>
</td>
<td align="left" headers="r4c1-t8 r1c3-t8">
<p><code>P_CODE</code></p>
</td>
</tr>
<tr align="left" valign="top">
<td align="left" id="r5c1-t8" headers="r1c1-t8">
<p><code>provinceOrState</code></p>
</td>
<td align="left" headers="r5c1-t8 r1c2-t8">
<p>Basic</p>
</td>
<td align="left" headers="r5c1-t8 r1c3-t8">
<p><code>PROVINCE</code></p>
</td>
</tr>
<tr align="left" valign="top">
<td align="left" id="r6c1-t8" headers="r1c1-t8">
<p><code>street</code></p>
</td>
<td align="left" headers="r6c1-t8 r1c2-t8">
<p>Basic</p>
</td>
<td align="left" headers="r6c1-t8 r1c3-t8">
<p><code>STREET</code></p>
</td>
</tr>
</tbody>
</table>
<br /></div>
<!-- class="inftblhruleinformal" --></li>
</ol>
<p>Dali automatically maps some fields to the correct database column (such as the <code>city</code> field to the <code>City</code> 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%">
<col width="86%" />
<col width="*" />
<tr>
<td align="left"><span class="copyrightlogo">Copyright&nbsp;&copy;&nbsp;2011, 2012,&nbsp;Oracle&nbsp;and/or&nbsp;its&nbsp;affiliates.&nbsp;All&nbsp;rights&nbsp;reserved.</span><br />
<a href="dcommon/html/cpyr.htm"><span class="copyrightlogo">Legal Notices</span></a></td>
</tr>
</table>
</div>
<!-- class="footer" -->
</body>
</html>

Back to the top