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
|
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Join Cursors</title>
<link rel="stylesheet" href="gettingStarted.css" type="text/css" />
<meta name="generator" content="DocBook XSL Stylesheets V1.73.2" />
<link rel="start" href="index.html" title="Getting Started with Berkeley DB" />
<link rel="up" href="persist_access.html" title="Chapter 5. Saving and Retrieving Objects" />
<link rel="prev" href="getmultiple.html" title="Retrieving Multiple Objects" />
<link rel="next" href="dpl_delete.html" title="Deleting Entity Objects" />
</head>
<body>
<div xmlns="" class="navheader">
<div class="libver">
<p>Library Version 12.1.6.1</p>
</div>
<table width="100%" summary="Navigation header">
<tr>
<th colspan="3" align="center">Join Cursors</th>
</tr>
<tr>
<td width="20%" align="left"><a accesskey="p" href="getmultiple.html">Prev</a> </td>
<th width="60%" align="center">Chapter 5. Saving and Retrieving Objects</th>
<td width="20%" align="right"> <a accesskey="n" href="dpl_delete.html">Next</a></td>
</tr>
</table>
<hr />
</div>
<div class="sect1" lang="en" xml:lang="en">
<div class="titlepage">
<div>
<div>
<h2 class="title" style="clear: both"><a id="dpl_entityjoin"></a>Join Cursors</h2>
</div>
</div>
</div>
<p>
If you have two or more secondary indexes set for
an entity object, then you can retrieve sets of
objects based on the intersection of multiple
secondary index values. You do this using an
<code class="classname">EntityJoin</code>
class.
</p>
<p>
For example, suppose you had an entity class that
represented automobiles. In that case, you might
be storing information about automobiles such as
color, number of doors, fuel mileage,
automobile type, number of passengers, make, model, and year,
to name just a few.
</p>
<p>
If you created a secondary index based this
information, then you could use an
<code class="classname">EntityJoin</code> to return
all those objects representing cars with, say, two
doors, that were built in 2002, and which are green
in color.
</p>
<p>
To create a join cursor, you:
</p>
<div class="orderedlist">
<ol type="1">
<li>
<p>
Open the primary index for the
entity class on which you want to
perform the join.
</p>
</li>
<li>
<p>
Open the secondary indexes that you
want to use for the join.
</p>
</li>
<li>
<p>
Instantiate an
<code class="classname">EntityJoin</code>
object (you use the primary index
to do this).
</p>
</li>
<li>
<p>
Use two or more calls to
<code class="methodname">EntityJoin.addCondition()</code>
to identify the secondary indexes
and their values that you want to use
for the equality match.
</p>
</li>
<li>
<p>
Call
<code class="methodname">EntityJoin.entities()</code>
to obtain a cursor that you can use
to iterate over the join results.
</p>
</li>
</ol>
</div>
<p>
For example, suppose we had an entity class
that included the following features:
</p>
<pre class="programlisting">package persist.gettingStarted;
import com.sleepycat.persist.model.Entity;
import com.sleepycat.persist.model.PrimaryKey;
import static com.sleepycat.persist.model.Relationship.*;
import com.sleepycat.persist.model.SecondaryKey;
@Entity
public class Automobiles {
// Primary key is the vehicle identification number
@PrimaryKey
private String vin;
// Secondary key is the vehicle's make
@SecondaryKey(relate=MANY_TO_ONE)
private String make;
// Secondary key is the vehicle's color
@SecondaryKey(relate=MANY_TO_ONE)
private String color;
...
public String getVIN() {
return vin;
}
public String getMake() {
return make;
}
public String getColor() {
return color;
}
... </pre>
<p>
Then we could perform an entity join that searches for all the
red automobiles made by Toyota as follows:
</p>
<pre class="programlisting">
PrimaryIndex<String,Automobiles> vin_pidx;
SecondaryIndex<String,String,Automobiles> make_sidx;
SecondaryIndex<String,String,Automobiles> color_sidx;
EntityJoin<String,Automobiles> join = new EntityJoin(vin_pidx);
join.addCondition(make_sidx,"Toyota");
join.addCondition(color_sidx,"Red");
// Now iterate over the results of the join operation
ForwardCursor<Automobiles> join_cursor = join.entities();
try {
for (Automobiles autoi : join_cursor) {
// do something with each object "autoi"
}
// Always make sure the cursor is closed when we are done with it.
} finally {
join_cursor.close();
} </pre>
</div>
<div class="navfooter">
<hr />
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left"><a accesskey="p" href="getmultiple.html">Prev</a> </td>
<td width="20%" align="center">
<a accesskey="u" href="persist_access.html">Up</a>
</td>
<td width="40%" align="right"> <a accesskey="n" href="dpl_delete.html">Next</a></td>
</tr>
<tr>
<td width="40%" align="left" valign="top">Retrieving Multiple Objects </td>
<td width="20%" align="center">
<a accesskey="h" href="index.html">Home</a>
</td>
<td width="40%" align="right" valign="top"> Deleting Entity Objects</td>
</tr>
</table>
</div>
</body>
</html>
|