je/docs/GettingStartedGuide/dpl_entityjoin.html

194 lines
7.3 KiB
HTML
Raw Permalink Normal View History

2021-06-06 17:46:45 +00:00
<?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 Java Edition" />
<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.2.7.5</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&lt;String,Automobiles&gt; vin_pidx;
SecondaryIndex&lt;String,String,Automobiles&gt; make_sidx;
SecondaryIndex&lt;String,String,Automobiles&gt; color_sidx;
EntityJoin&lt;String,Automobiles&gt; 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&lt;Automobiles&gt; 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>