-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfields.html
More file actions
294 lines (291 loc) · 8.95 KB
/
fields.html
File metadata and controls
294 lines (291 loc) · 8.95 KB
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>exedio persistence - Field Trail</title>
<meta name="keywords" content="Persistence, Java">
<meta name="description" content="persistence framework for java">
<link rel="stylesheet" href="cope.css">
</head>
<body>
<!-- HEADER -->
<div id="header_wrap" class="outer">
<header class="inner">
<a id="forkme_banner" href="https://github.com/exedio">View on GitHub</a>
<h1 id="project_title">exedio persistence</h1>
<h2 id="project_tagline">Persistence Framework for Java.</h2>
<a id="toplink" href="index.html">Go to top</a>
</header>
</div>
<!-- MAIN CONTENT -->
<div id="main_content_wrap" class="outer">
<section id="main_content" class="inner">
<h2>Field Trail</h2>
<p>
While the <a href="intro.html">Introduction Trail</a>
covered string fields only,
this trails visits all kinds of other fields
as well as the more special features of string fields.
</p>
<h3 id="contents">Contents</h3>
<ul>
<li><a href="#common">Common Primitives</a></li>
<li><a href="#null">Allowing Null</a></li>
<li><a href="#defaults">Default Values</a></li>
<li><a href="#unique">Unique Fields</a></li>
<li><a href="#enum">Enum Fields</a></li>
<li><a href="#item">Item Fields</a></li>
<li><a href="#further">Further Reading</a></li>
</ul>
<h3 id="common">Common Primitives</h3>
<p>
exedio persistence provides fields for all commonly used java primitives
as well as for <code>String</code> and <code>java.util.Date</code>.
This looks like this.
</p>
<pre>
class Customer extends Item
{
static final StringField email =
new StringField();
static final DateField registrationDate =
new DateField();
static final LongField loginCounter =
new LongField();
static final IntegerField subsequentLoginFailures =
new IntegerField();
static final DoubleField priceCoefficient =
new DoubleField();
}
</pre>
<p>
Run the instrumentor, and you get setters/getters for all
the new fields.
Also the <i>default creation constructor</i>
will have parameters for the initial values of these fields.
</p>
<h3 id="null">Allowing Null</h3>
<p>
You may have noticed, that you cannot store null
into any of the fields.
This is because fields are <i>mandatory</i> by default.
You can easily change this to <i>optional</i>.
</p>
<pre>
static final LongField loginCounter =
new LongField().<a href="api/com/exedio/cope/LongField.html#optional()"><b>optional</b></a>();
</pre>
<p>
When you run the instrumentor, you may notice
a few changes in the generated code.
</p>
<ol>
<li>
The <i>default creation constructor</i> does not
include a parameter for <code>loginCounter</code> anymore.
This is because a value for <code>loginCounter</code> is not needed
for constructing an instance of Customer anymore.
If you still want to have <code>loginCounter</code> included into
the <i>default creation constructor</i> add a
<code>@WrapperInitial</code> annotation to the field:
<pre>
@<a href="api/com/exedio/cope/instrument/WrapperInitial.html">WrapperInitial</a>
static final LongField loginCounter =
new LongField().<a href="api/com/exedio/cope/LongField.html#optional()">optional</a>();
</pre>
</li>
<li>
The signature of the getter and setter for <code>loginCounter</code>
changed.
Before, it used the primitive type <code>long</code>
<pre>
<b>long</b> getLoginCounter() { ... }
void setLoginCounter(<b>long</b> loginCounter) { ... }
</pre>
but now being optional it uses the wrapper class
<a href="https://docs.oracle.com/javase/8/docs/api/java/lang/Long.html">Long</a>.
<pre>
<b>Long</b> getLoginCounter() { ... }
void setLoginCounter(<b>Long</b> loginCounter) { ... }
</pre>
</li>
</ol>
<h3 id="defaults">Default Values</h3>
<p>
What we did above with the login counter is probably not,
what one would want in a real project.
One would probably rather want to make it <i>mandatory</i>
and initialize it to zero for any newly created customer.
This can be specified by:
</p>
<pre>
static final LongField loginCounter =
new LongField()<b>.defaultTo(0l)</b>;
</pre>
<p>
In the instrumented code the type for setter/getter
changes back to primitives again,
but the <i>default creation constructor</i> does also
not have a parameter for <code>loginCounter</code>.
</p>
<p>
If you want to make the current date the default value
of any date field,
then of course <code>defaultTo(new Date())</code> won't work.
Use instead:
</p>
<pre>
static final DateField registrationDate =
new DateField()<b>.defaultToNow()</b>;
</pre>
<h3 id="unique">Unique Fields</h3>
<p>
Probably you wouldn't want to have more than one customer
for any given email address.
Peace of cake:
</p>
<pre>
static final StringField email =
new StringField().<a href="api/com/exedio/cope/StringField.html#unique()"><b>unique</b></a>();
</pre>
<p>
If you violate this unique constraint either via a
creation constructor or via <code>setEmail</code>
this will throw a <a href="api/com/exedio/cope/UniqueViolationException.html">UniqueViolationException</a>.
</p>
<p>
In the instrumented code you will find a new convenience method
</p>
<pre>
static final Customer findByEmail(String email);
</pre>
<p>
which does, what it's name suggests.
</p>
<p>
For unique constraints across multiple fields
the notation is slightly different:
</p>
<pre>
class MatrixElement extends Item
{
static final IntegerField x =
new IntegerField();
static final IntegerField y =
new IntegerField();
static final UniqueConstraint xy =
new UniqueConstraint(x, y);
}
</pre>
<h3 id="enum">Enum Fields</h3>
<p>
A common pattern in persistent models are fields,
the can have a value out of a predefined set of values.
For that purpose java provides enums since JDK 1.5.
exedio persistence allows you to store such enums in fields
just like any other primitive:
</p>
<pre>
class Customer extends Item
{
static enum Status
{
STANDARD,
GOLD,
PLATINUM,
}
static final EnumField<Status> status =
newEnumField(Status.class);
}
</pre>
<h3 id="item">Item Fields</h3>
<p>
An item fields stores a reference to another persistent item.
It's the object oriented counter part of
a foreign key constraint in relational databases.
</p>
<p>
Here an example of a product referencing the product group it belongs to:
</p>
<pre>
class ProductGroup extends Item
{
static final StringField name =
new StringField();
}
</pre>
<pre>
class Product extends Item
{
static final ItemField<ProductGroup> productGroup =
newItemField(ProductGroup.class);
}
</pre>
<p>
As specified above, a product group cannot be deleted,
if there are still products referencing this product group.
This is because the default <i>delete policy</i>
of an item field is <i>forbid</i>.
Alternativly you may want to have all products of a product group to be deleted,
if that product group is deleted.
To achive this, change the <i>delete policy</i> to <i>cascade</i>.
</p>
<pre>
static final ItemField<ProductGroup> productGroup =
newItemField(ProductGroup.class, <b>CASCADE</b>);
</pre>
<p>
Yet another alternative is to set the item field to null
on all products referencing the product group to be deleted.
This is done by:
</p>
<pre>
static final ItemField<ProductGroup> productGroup =
newItemField(ProductGroup.class, <b>NULLIFY</b>);
</pre>
<p>
Of course <i>nullify</i> makes no sense for a <i>mandatory</i> field.
Therefore such item fields are <i>optional</i> by default.
</p>
<h3 id="further">Further Reading</h3>
<p>
This was the field trail of the tour.
You may now proceed to trails:
</p>
<ul>
<li>
<a href="transactions.html">Transactions Trail</a>
tells you how to use transactions.
</li>
<li>
<a href="searching.html">Searching Trail</a>
gives you an introduction into the searching capabilities of exedio persistence.
</li>
<li>
<a href="fields2.html">Field Reloaded Trail</a>
covers all the more specific possibilities to store data with exedio persistence.
</li>
<li>
<a href="webapplications.html">Web Application Trail</a>
shows you the little differences when using the framework within a web container.
</li>
</ul>
</section>
</div>
<!-- FOOTER -->
<div id="footer_wrap" class="outer">
<footer class="inner">
<p>
Maintained by
<a href="https://www.exedio.com/" target="_top">exedio</a>
Gesellschaft für Softwareentwicklung mbH.
</p>
<a href="https://validator.w3.org/check?uri=referer" class="validhtml">Valid HTML5</a>
<p>
Slate theme by <a href="https://github.com/jasoncostello">Jason Costello</a>.
Published with <a href="https://pages.github.com">GitHub Pages</a>.
</p>
</footer>
</div>
</body>
</html>