je/docs/GettingStartedGuide/dpl_delete.html

115 lines
4.9 KiB
HTML
Raw 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>Deleting Entity Objects</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="dpl_entityjoin.html" title="Join Cursors" />
<link rel="next" href="dpl_replace.html" title="Replacing 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">Deleting Entity Objects</th>
</tr>
<tr>
<td width="20%" align="left"><a accesskey="p" href="dpl_entityjoin.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_replace.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_delete"></a>Deleting Entity Objects</h2>
</div>
</div>
</div>
<p>
The simplest way to remove an object from your entity store
is to delete it by its primary index. For example,
using the <code class="classname">SimpleDA</code> class that we
created earlier in this document
(see <a class="xref" href="simpleda.html" title="SimpleDA.class">SimpleDA.class</a>),
you can delete the <code class="classname">SimpleEntityClass</code>
object with a primary key of <code class="literal">keyone</code> as
follows:
</p>
<pre class="programlisting">sda.pIdx.delete("keyone");</pre>
<p>
You can also delete objects by their secondary keys. When
you do this, all objects related to the secondary key are
deleted, unless the key is a foreign object.
</p>
<p>
For example, the following deletes all
<code class="classname">SimpleEntityClass</code> with a secondary
key of <code class="literal">skeyone</code>:
</p>
<pre class="programlisting">sda.sIdx.delete("skeyone");</pre>
<p>
You can delete any single object by positioning a cursor to
that object and then calling the cursor's
<code class="methodname">delete()</code> method.
</p>
<pre class="programlisting">PrimaryIndex&lt;String,SimpleEntityClass&gt; pi =
store.getPrimaryIndex(String.class, SimpleEntityClass.class);
SecondaryIndex&lt;String,String,SimpleEntityClass&gt; si =
store.getSecondaryIndex(pi, String.class, "sKey");
EntityCursor&lt;SimpleEntityClass&gt; sec_cursor =
si.subIndex("skeyone").entities();
try {
SimpleEntityClass sec;
Iterator&lt;SimpleEntityClass&gt; i = sec_cursor.iterator();
while (sec = i.nextDup() != null) {
if (sec.getSKey() == "some value") {
i.delete();
}
}
// Always make sure the cursor is closed when we are done with it.
} finally {
sec_cursor.close(); } </pre>
<p>
Finally, if you are indexing by foreign key, then the
results of deleting the key is determined by the foreign
key constraint that you have set for the index. See
<a class="xref" href="dplindexcreate.html#foreignkey" title="Foreign Key Constraints">Foreign Key Constraints</a>
for more information.
</p>
</div>
<div class="navfooter">
<hr />
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left"><a accesskey="p" href="dpl_entityjoin.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_replace.html">Next</a></td>
</tr>
<tr>
<td width="40%" align="left" valign="top">Join Cursors </td>
<td width="20%" align="center">
<a accesskey="h" href="index.html">Home</a>
</td>
<td width="40%" align="right" valign="top"> Replacing Entity Objects</td>
</tr>
</table>
</div>
</body>
</html>