-
Notifications
You must be signed in to change notification settings - Fork 130
/
jpa_overview_pc.xml
1359 lines (1327 loc) · 49.3 KB
/
jpa_overview_pc.xml
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2006 The Apache Software Foundation.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<chapter id="jpa_overview_pc">
<title>
Entity
</title>
<indexterm zone="jpa_overview_pc">
<primary>
persistent classes
</primary>
</indexterm>
<indexterm zone="jpa_overview_pc">
<primary>
Entity
</primary>
<seealso>
persistent classes
</seealso>
</indexterm>
<indexterm>
<primary>
class
</primary>
<secondary>
persistent
</secondary>
<see>
persistent classes
</see>
</indexterm>
<indexterm>
<primary>
persistence capable
</primary>
<see>
Entity
</see>
</indexterm>
<para>
JPA recognizes two types of persistent classes: <emphasis>entity</emphasis>
classes and <emphasis>embeddable</emphasis> classes. Each persistent instance of
an entity class - each <emphasis>entity</emphasis> - represents a unique
datastore record. You can use the <classname>EntityManager</classname> to find
an entity by its persistent identity (covered later in this chapter), or use a
<classname>Query</classname> to find entities matching certain criteria.
</para>
<para>
An instance of an embeddable class, on the other hand, is only stored as part of
a separate entity. Embeddable instances have no persistent identity, and are
never returned directly from the <classname>EntityManager</classname> or from a
<classname>Query</classname>.
</para>
<para>
Despite these differences, there are few distinctions between entity classes and
embeddable classes. In fact, writing either type of persistent class is a lot
like writing any other class. There are no special parent classes to
extend from, field types to use, or methods to write. This is one important way
in which JPA makes persistence transparent to you, the developer.
</para>
<note>
<para>
JPA supports both fields and JavaBean properties as persistent state. For
simplicity, however, we will refer to all persistent state as persistent fields,
unless we want to note a unique aspect of persistent properties.
</para>
</note>
<example id="jpa_overview_pc_pcclass">
<title>
Persistent Class
</title>
<programlisting>
package org.mag;
/**
* Example persistent class. Notice that it looks exactly like any other
* class. JPA makes writing persistent classes completely transparent.
*/
public class Magazine {
private String isbn;
private String title;
private Set articles = new HashSet();
private Article coverArticle;
private int copiesSold;
private double price;
private Company publisher;
private int version;
protected Magazine() {
}
public Magazine(String title, String isbn) {
this.title = title;
this.isbn = isbn;
}
public void publish(Company publisher, double price) {
this.publisher = publisher;
publisher.addMagazine(this);
this.price = price;
}
public void sell() {
copiesSold++;
publisher.addRevenue(price);
}
public void addArticle(Article article) {
articles.add(article);
}
// rest of methods omitted
}
</programlisting>
</example>
<section id="jpa_overview_pc_restrict">
<title>
Restrictions on Persistent Classes
</title>
<indexterm zone="jpa_overview_pc_restrict">
<primary>
persistent classes
</primary>
<secondary>
restrictions on
</secondary>
</indexterm>
<para>
There are very few restrictions placed on persistent classes. Still, it never
hurts to familiarize yourself with exactly what JPA does and does not support.
</para>
<section id="jpa_overview_pc_no_arg">
<title>
Default or No-Arg Constructor
</title>
<indexterm zone="jpa_overview_pc_no_arg">
<primary>
persistent classes
</primary>
<secondary>
no-arg constructor requirement
</secondary>
</indexterm>
<indexterm>
<primary>
constructor
</primary>
<secondary>
no-arg constructor requirement
</secondary>
</indexterm>
<para>
The JPA specification requires that all persistent classes have a no-arg
constructor. This constructor may be public or protected. Because the compiler
automatically creates a default no-arg constructor when no other constructor is
defined, only classes that define constructors must also include a no-arg
constructor.
</para>
<note>
<para>
OpenJPA's <emphasis>enhancer</emphasis> will automatically add a protected
no-arg constructor to your class when required. Therefore, this restriction does
not apply when using OpenJPA. See <xref linkend="ref_guide_pc_enhance"/>
of the Reference Guide for details.
</para>
</note>
</section>
<section id="jpa_overview_pc_final">
<title>
Final
</title>
<para>
Entity classes may not be final. No method of an entity class can be final.
</para>
<note>
<para>
OpenJPA supports final classes and final methods.
</para>
</note>
</section>
<section id="jpa_overview_pc_id">
<title>
Identity Fields
</title>
<indexterm zone="jpa_overview_pc_id">
<primary>
identity fields
</primary>
<seealso>
persistent fields
</seealso>
</indexterm>
<indexterm zone="jpa_overview_pc_id">
<primary>
persistent classes
</primary>
<secondary>
JPA id requirement
</secondary>
</indexterm>
<para>
All entity classes must declare one or more fields which together form the
persistent identity of an instance. These are called <emphasis>identity
</emphasis> or <emphasis>primary key</emphasis> fields. In our <classname>
Magazine</classname> class, <literal>isbn</literal> and <literal>title</literal>
are identity fields, because no two magazine records in the datastore can have
the same <literal>isbn</literal> and <literal>title</literal> values.
<xref linkend="jpa_overview_meta_id"/> will show you how to denote your
identity fields in JPA metadata. <xref linkend="jpa_overview_pc_identity"/>
below examines persistent identity.
</para>
<note>
<para>
OpenJPA fully supports identity fields, but does not require them. See
<xref linkend="ref_guide_pc_oid"/> of the Reference Guide for details.
</para>
</note>
</section>
<section id="jpa_overview_pc_version">
<title>
Version Field
</title>
<indexterm zone="jpa_overview_pc_version">
<primary>
version fields
</primary>
<seealso>
persistent fields
</seealso>
</indexterm>
<indexterm zone="jpa_overview_pc_version">
<primary>
persistent classes
</primary>
<secondary>
JPA version requirement
</secondary>
</indexterm>
<para>
The <literal>version</literal> field in our <classname>Magazine</classname>
class may seem out of place. JPA uses a version field in your entities to detect
concurrent modifications to the same datastore record. When the JPA runtime
detects an attempt to concurrently modify the same record, it throws an
exception to the transaction attempting to commit last. This prevents
overwriting the previous commit with stale data.
</para>
<para>
A version field is not required, but without one concurrent threads or
processes might succeed in making conflicting changes to the same record at the
same time. This is unacceptable to most applications.
<xref linkend="jpa_overview_meta_version"/> shows you how to designate a
version field in JPA metadata.
</para>
<para>
The version field must be an integral type (<classname> int</classname>,
<classname>Long</classname>, etc) or a <classname>
java.sql.Timestamp</classname>. You should consider version fields immutable.
Changing the field value has undefined results.
</para>
<note>
<para>
OpenJPA fully supports version fields, but does not require them for concurrency
detection. OpenJPA can maintain surrogate version values or use state
comparisons to detect concurrent modifications. See
<xref linkend="ref_guide_mapping_jpa"/> in the Reference Guide.
</para>
</note>
</section>
<section id="jpa_overview_pc_restrict_inheritance">
<title>
Inheritance
</title>
<indexterm zone="jpa_overview_pc_restrict_inheritance">
<primary>
persistent classes
</primary>
<secondary>
inheritance of
</secondary>
<seealso>
inheritance
</seealso>
</indexterm>
<indexterm>
<primary>
inheritance
</primary>
<secondary>
of persistent classes
</secondary>
</indexterm>
<para>
JPA fully supports inheritance in persistent classes. It allows persistent
classes to inherit from non-persistent classes, persistent classes to inherit
from other persistent classes, and non-persistent classes to inherit from
persistent classes. It is even possible to form inheritance hierarchies in which
persistence skips generations. There are, however, a few important limitations:
</para>
<itemizedlist>
<listitem>
<para>
Persistent classes cannot inherit from certain natively-implemented system
classes such as <classname>java.net.Socket</classname> and <classname>
java.lang.Thread</classname>.
</para>
</listitem>
<listitem>
<para>
If a persistent class inherits from a non-persistent class, the fields of the
non-persistent superclass cannot be persisted.
</para>
</listitem>
<listitem>
<para>
All classes in an inheritance tree must use the same identity type. We cover
entity identity in <xref linkend="jpa_overview_pc_identity"/>.
</para>
</listitem>
</itemizedlist>
</section>
<section id="jpa_overview_pc_restrict_fields">
<title>
Persistent Fields
</title>
<indexterm zone="jpa_overview_pc_restrict_fields">
<primary>
persistent classes
</primary>
<secondary>
field restrictions
</secondary>
<seealso>
persistent fields
</seealso>
</indexterm>
<indexterm>
<primary>
field
</primary>
<secondary>
persistent
</secondary>
<see>
persistent fields
</see>
</indexterm>
<indexterm zone="jpa_overview_pc_restrict_fields">
<primary>
persistent fields
</primary>
<secondary>
restrictions on
</secondary>
</indexterm>
<para>
JPA manages the state of all persistent fields. Before you access persistent
state, the JPA runtime makes sure that it has been loaded from the datastore.
When you set a field, the runtime records that it has changed so that the new
value will be persisted. This allows you to treat the field in exactly the same
way you treat any other field - another aspect of JPA's transparency.
</para>
<para>
JPA does not support static or final fields. It does, however, include built-in
support for most common field types. These types can be roughly divided into
three categories: immutable types, mutable types, and relations.
</para>
<para>
<indexterm>
<primary>
persistent fields
</primary>
<secondary>
immutable types
</secondary>
</indexterm>
<indexterm>
<primary>
immutable
</primary>
<secondary>
persistent field types
</secondary>
</indexterm>
<emphasis>Immutable</emphasis> types, once created, cannot be changed. The only
way to alter a persistent field of an immutable type is to assign a new value to
the field. JPA supports the following immutable types:
</para>
<itemizedlist>
<listitem>
<para>
All primitives (<classname>int, float, byte</classname>, etc)
</para>
</listitem>
<listitem>
<para>
All primitive wrappers (<classname>java.lang.Integer, java.lang.Float,
java.lang.Byte</classname>, etc)
</para>
</listitem>
<listitem>
<para>
<classname>java.lang.String</classname>
</para>
</listitem>
<listitem>
<para>
<classname>java.math.BigInteger</classname>
</para>
</listitem>
<listitem>
<para>
<classname>java.math.BigDecimal</classname>
</para>
</listitem>
</itemizedlist>
<para>
JPA also supports <classname>byte[]</classname>, <classname>Byte[]</classname>,
<classname>char[]</classname>, and <classname>Character[]</classname> as
immutable types. That is, you can persist fields of these types,
but you should not manipulate individual array indexes without resetting the
array into the persistent field.
</para>
<para>
<indexterm>
<primary>
persistent fields
</primary>
<secondary>
mutable types
</secondary>
<seealso>
proxies
</seealso>
</indexterm>
<indexterm>
<primary>
mutable
</primary>
<secondary>
persistent field types
</secondary>
<seealso>
persistent fields
</seealso>
<seealso>
proxies
</seealso>
</indexterm>
<indexterm>
<primary>
persistent fields
</primary>
<secondary>
user-defined types
</secondary>
</indexterm>
<indexterm>
<primary>
user-defined
</primary>
<secondary>
persistent field types
</secondary>
<seealso>
persistent fields
</seealso>
</indexterm>
Persistent fields of <emphasis>mutable</emphasis> types can be altered without
assigning the field a new value. Mutable types can be modified directly through
their own methods. The JPA specification requires that implementations support
the following mutable field types:
</para>
<itemizedlist>
<listitem>
<para>
<classname>java.util.Date</classname>
</para>
</listitem>
<listitem>
<para>
<classname>java.util.Calendar</classname>
</para>
</listitem>
<listitem>
<para>
<classname>java.sql.Date</classname>
</para>
</listitem>
<listitem>
<para>
<classname>java.sql.Timestamp</classname>
</para>
</listitem>
<listitem>
<para>
Enums
</para>
</listitem>
<listitem>
<para>
Entity types (relations between entities)
</para>
</listitem>
<listitem>
<para>
Embeddable types
</para>
</listitem>
<listitem>
<para>
<classname>java.util.Collection</classname>s of entities
</para>
</listitem>
<listitem>
<para>
<classname>java.util.Set</classname>s of entities
</para>
</listitem>
<listitem>
<para>
<classname>java.util.List</classname>s of entities
</para>
</listitem>
<listitem>
<para>
<classname>java.util.Map</classname>s in which each entry maps the value of one
of a related entity's fields to that entity.
</para>
</listitem>
</itemizedlist>
<para>
Collection and map types may be parameterized.
</para>
<para>
<indexterm>
<primary>
persistent fields
</primary>
<secondary>
of unknown types
</secondary>
</indexterm>
<indexterm>
<primary>
Object
</primary>
<secondary>
as persistent field type
</secondary>
<seealso>
persistent fields
</seealso>
</indexterm>
Most JPA implementations also have support for persisting serializable values as
binary data in the datastore. <xref linkend="jpa_overview_meta"/> has more
information on persisting serializable types.
</para>
<note>
<para>
OpenJPA also supports arrays, <classname>java.lang.Number</classname>,
<classname>java.util.Locale</classname>, all JDK 1.2 <classname>Set</classname>,
<classname>List</classname>, and <classname>Map</classname> types,
and many other mutable and immutable field types. OpenJPA also allows you to
plug in support for custom types.
</para>
</note>
</section>
<section id="jpa_overview_pc_restrict_conclusion">
<title>
Conclusions
</title>
<para>
This section detailed all of the restrictions JPA places on persistent classes.
While it may seem like we presented a lot of information, you will seldom find
yourself hindered by these restrictions in practice. Additionally, there are
often ways of using JPA's other features to circumvent any limitations you run
into.
</para>
</section>
</section>
<section id="jpa_overview_pc_identity">
<title>
Entity Identity
</title>
<indexterm zone="jpa_overview_pc_identity">
<primary>
JPA
</primary>
<secondary>
identity
</secondary>
<seealso>
identity
</seealso>
</indexterm>
<indexterm>
<primary>
entity identity
</primary>
<see>
identity
</see>
</indexterm>
<indexterm zone="jpa_overview_pc_identity">
<primary>
identity
</primary>
<secondary>
JPA
</secondary>
</indexterm>
<para>
<indexterm>
<primary>
identity
</primary>
<secondary>
numeric
</secondary>
</indexterm>
<indexterm>
<primary>
identity
</primary>
<secondary>
qualitative
</secondary>
</indexterm>
<indexterm>
<primary>
numeric identity
</primary>
<seealso>
identity
</seealso>
</indexterm>
<indexterm>
<primary>
qualitative identity
</primary>
<seealso>
identity
</seealso>
</indexterm>
Java recognizes two forms of object identity: numeric identity and qualitative
identity. If two references are <emphasis>numerically</emphasis> identical, then
they refer to the same JVM instance in memory. You can test for this using the
<literal>==</literal> operator. <emphasis>Qualitative</emphasis> identity, on
the other hand, relies on some user-defined criteria to determine whether two
objects are "equal". You test for qualitative identity using the <methodname>
equals</methodname> method. By default, this method simply relies on numeric
identity.
</para>
<para>
JPA introduces another form of object identity, called <emphasis>entity
identity</emphasis> or <emphasis>persistent identity</emphasis>. Entity
identity tests whether two persistent objects represent the same state in the
datastore.
</para>
<para>
<indexterm>
<primary>
persistent fields
</primary>
<secondary>
id
</secondary>
</indexterm>
<indexterm>
<primary>
id
</primary>
<secondary>
fields
</secondary>
<seealso>
persistent fields
</seealso>
</indexterm>
The entity identity of each persistent instance is encapsulated in its
<emphasis>identity field(s)</emphasis>. If two entities of the same type have
the same identity field values, then the two entities represent the same state
in the datastore. Each entity's identity field values must be unique among all
other entites of the same type.
</para>
<para>
Identity fields must be primitives, primitive wrappers, <classname>
String</classname>s, <classname>Date</classname>s, <classname>
Timestamp</classname>s, or embeddable types.
</para>
<note>
<para>
OpenJPA supports entities as identity fields, as the Reference Guide discusses
in <xref linkend="ref_guide_pc_oid_entitypk"/>. For legacy schemas with binary
primary key columns, OpenJPA also supports using identity fields of type
<classname>byte[]</classname>. When you use a <classname>byte[]</classname>
identity field, you must create an identity class. Identity classes are
covered below.
</para>
</note>
<warning>
<para>
Changing the fields of an embeddable instance while it is assigned to an
identity field has undefined results. Always treat embeddable identity instances
as immutable objects in your applications.
</para>
</warning>
<para>
<indexterm>
<primary>
identity
</primary>
<secondary>
uniqueness requirement
</secondary>
</indexterm>
<indexterm>
<primary>
uniquness requirement
</primary>
<seealso>
identity
</seealso>
</indexterm>
If you are dealing with a single persistence context (see
<xref linkend="jpa_overview_emfactory_perscontext"/>), then you do not
have to compare identity fields to test whether two entity references represent
the same state in the datastore. There is a much easier way: the <literal>==
</literal> operator. JPA requires that each persistence context maintain only
one JVM object to represent each unique datastore record. Thus, entity identity
is equivalent to numeric identity within a persistence context. This is referred
to as the <emphasis>uniqueness requirement</emphasis>.
</para>
<para>
The uniqueness requirement is extremely important - without it, it would be
impossible to maintain data integrity. Think of what could happen if two
different objects in the same transaction were allowed to represent the same
persistent data. If you made different modifications to each of these objects,
which set of changes should be written to the datastore? How would your
application logic handle seeing two different "versions" of the same data?
Thanks to the uniqueness requirement, these questions do not have to be
answered.
</para>
<section id="jpa_overview_pc_identitycls">
<title>
Identity Class
</title>
<para>
<indexterm zone="jpa_overview_pc_identitycls">
<primary>
identity class
</primary>
<seealso>
identity
</seealso>
</indexterm>
<indexterm zone="jpa_overview_pc_identitycls">
<primary>
identity
</primary>
<secondary>
class requirements
</secondary>
</indexterm>
If your entity has only one identity field, you can use the value of that field
as the entity's identity object in all <link linkend="jpa_overview_em">
<classname>EntityManager</classname></link> APIs. Otherwise, you must supply an
identity class to use for identity objects. Your identity class must meet the
following criteria:
</para>
<itemizedlist>
<listitem>
<para>
The class must be public.
</para>
</listitem>
<listitem>
<para>
The class must be serializable.
</para>
</listitem>
<listitem>
<para>
The class must have a public no-args constructor.
</para>
</listitem>
<listitem>
<para>
The names of the non-static fields or properties of the class must be the same
as the names of the identity fields or properties of the corresponding entity
class, and the types must be identical.
</para>
</listitem>
<listitem>
<para>
The <methodname>equals</methodname> and <methodname>hashCode</methodname>
methods of the class must use the values of all fields or properties
corresponding to identity fields or properties in the entity class.
</para>
</listitem>
<listitem>
<para>
If the class is an inner class, it must be <literal>static</literal>.
</para>
</listitem>
<listitem>
<para>
All entity classes related by inheritance must use the same identity class, or
else each entity class must have its own identity class whose inheritance
hierarchy mirrors the inheritance hierarchy of the owning entity classes (see
<xref linkend="jpa_overview_pc_identity_hierarchy"/>).
</para>
</listitem>
</itemizedlist>
<note>
<para>
Though you may still create identity classes by hand, OpenJPA provides the
<classname>appidtool</classname> to automatically generate proper identity
classes based on your identity fields. See
<xref linkend="ref_guide_pc_oid_application"/> of the Reference Guide.
</para>
</note>
<example id="jpa_overview_pc_identity_appidcode">
<title>
Identity Class
</title>
<para>
This example illustrates a proper identity class for an entity with multiple
identity fields.
</para>
<programlisting>
/**
* Persistent class using application identity.
*/
public class Magazine {
private String isbn; // identity field
private String title; // identity field
// rest of fields and methods omitted
/**
* Application identity class for Magazine.
*/
public static class MagazineId {
// each identity field in the Magazine class must have a
// corresponding field in the identity class
public String isbn;
public String title;
/**
* Equality must be implemented in terms of identity field
* equality, and must use instanceof rather than comparing
* classes directly (some JPA implementations may subclass the
* identity class).
*/
public boolean equals(Object other) {
if (other == this)
return true;
if (!(other instanceof MagazineId))
return false;
MagazineId mi = (MagazineId) other;
return (isbn == mi.isbn
|| (isbn != null && isbn.equals(mi.isbn)))
&& (title == mi.title
|| (title != null && title.equals(mi.title)));
}
/**
* Hashcode must also depend on identity values.
*/
public int hashCode() {
return ((isbn == null) ? 0 : isbn.hashCode())
^ ((title == null) ? 0 : title.hashCode());
}
public String toString() {
return isbn + ":" + title;
}
}
}
</programlisting>
</example>
<section id="jpa_overview_pc_identity_hierarchy">
<title>
Identity Hierarchies
</title>
<indexterm zone="jpa_overview_pc_identity_hierarchy">
<primary>
identity
</primary>
<secondary>
hierarchy
</secondary>
</indexterm>
<mediaobject>
<imageobject>
<!-- PNG image data, 320 x 267 (see README) -->
<imagedata fileref="img/appid-hierarchy.png" width="213px"/>
</imageobject>
</mediaobject>
<para>
An alternative to having a single identity class for an entire inheritance
hierarchy is to have one identity class per level in the inheritance hierarchy.
The requirements for using a hierarchy of identity classes are as follows:
</para>
<itemizedlist>
<listitem>
<para>
The inheritance hierarchy of identity classes must exactly mirror the hierarchy
of the persistent classes that they identify. In the example pictured above,
abstract class <classname>Person</classname> is extended by abstract class
<classname>Employee</classname>, which is extended by non-abstract class
<classname> FullTimeEmployee</classname>, which is extended by non-abstract
class <classname>Manager</classname>. The corresponding identity classes, then,
are an abstract <classname>PersonId</classname> class, extended by an abstract
<classname>EmployeeId</classname> class, extended by a non-abstract <classname>
FullTimeEmployeeId</classname> class, extended by a non-abstract <classname>
ManagerId</classname> class.
</para>
</listitem>
<listitem>
<para>
Subclasses in the identity hierarchy may define additional identity fields until
the hierarchy becomes non-abstract. In the aforementioned example, <classname>
Person</classname> defines an identity field <literal>ssn</literal>, <classname>
Employee</classname> defines additional identity field <literal>userName
</literal>, and <classname>FullTimeEmployee</classname> adds a final identity
field, <literal>empId</literal>. However, <classname>Manager</classname> may not
define any additional identity fields, since it is a subclass of a non-abstract
class. The hierarchy of identity classes, of course, must match the identity
field definitions of the persistent class hierarchy.
</para>
</listitem>
<listitem>
<para>
It is not necessary for each abstract class to declare identity fields. In the
previous example, the abstract <classname>Person</classname> and <classname>
Employee</classname> classes could declare no identity fields, and the first
concrete subclass <classname>FullTimeEmployee</classname> could define one or
more identity fields.
</para>
</listitem>
<listitem>
<para>
All subclasses of a concrete identity class must be <methodname>equals
</methodname> and <methodname>hashCode</methodname>-compatible with the
concrete superclass. This means that in our example, a <classname>ManagerId
</classname> instance and a <classname>FullTimeEmployeeId</classname> instance
with the same identity field values should have the same hash code, and should
compare equal to each other using the <methodname>equals</methodname> method of
either one. In practice, this requirement reduces to the following coding
practices:
</para>
<orderedlist>
<listitem>
<para>
Use <literal>instanceof</literal> instead of comparing <classname>Class
</classname> objects in the <methodname>equals</methodname> methods of your
identity classes.
</para>
</listitem>
<listitem>
<para>
An identity class that extends another non-abstract identity class should not
override <methodname>equals</methodname> or <methodname>hashCode</methodname>.
</para>
</listitem>
</orderedlist>
</listitem>
</itemizedlist>
</section>
</section>
</section>
<section id="jpa_overview_pc_callbacks">
<title>
Lifecycle Callbacks
</title>
<indexterm zone="jpa_overview_pc_callbacks">
<primary>
lifecycle callbacks
</primary>