1<?xml version="1.0" encoding="UTF-8" standalone="no"?> 2<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 3<html xmlns="http://www.w3.org/1999/xhtml"> 4 <head> 5 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 6 <title>Deleting Entity Objects</title> 7 <link rel="stylesheet" href="gettingStarted.css" type="text/css" /> 8 <meta name="generator" content="DocBook XSL Stylesheets V1.73.2" /> 9 <link rel="start" href="index.html" title="Getting Started with Berkeley DB" /> 10 <link rel="up" href="persist_access.html" title="Chapter 5. Saving and Retrieving Objects" /> 11 <link rel="prev" href="dpl_entityjoin.html" title="Join Cursors" /> 12 <link rel="next" href="dpl_replace.html" title="Replacing Entity Objects" /> 13 </head> 14 <body> 15 <div class="navheader"> 16 <table width="100%" summary="Navigation header"> 17 <tr> 18 <th colspan="3" align="center">Deleting Entity Objects</th> 19 </tr> 20 <tr> 21 <td width="20%" align="left"><a accesskey="p" href="dpl_entityjoin.html">Prev</a> </td> 22 <th width="60%" align="center">Chapter 5. Saving and Retrieving Objects</th> 23 <td width="20%" align="right"> <a accesskey="n" href="dpl_replace.html">Next</a></td> 24 </tr> 25 </table> 26 <hr /> 27 </div> 28 <div class="sect1" lang="en" xml:lang="en"> 29 <div class="titlepage"> 30 <div> 31 <div> 32 <h2 class="title" style="clear: both"><a id="dpl_delete"></a>Deleting Entity Objects</h2> 33 </div> 34 </div> 35 </div> 36 <p> 37 The simplest way to remove an object from your entity store 38 is to delete it by its primary index. For example, 39 using the <code class="classname">SimpleDA</code> class that we 40 created earlier in this document 41 (see <a class="xref" href="simpleda.html" title="SimpleDA.class">SimpleDA.class</a>), 42 you can delete the <code class="classname">SimpleEntityClass</code> 43 object with a primary key of <code class="literal">keyone</code> as 44 follows: 45 </p> 46 <pre class="programlisting">sda.pIdx.delete("keyone");</pre> 47 <p> 48 You can also delete objects by their secondary keys. When 49 you do this, all objects related to the secondary key are 50 deleted, unless the key is a foreign object. 51 </p> 52 <p> 53 For example, the following deletes all 54 <code class="classname">SimpleEntityClass</code> with a secondary 55 key of <code class="literal">skeyone</code>: 56 </p> 57 <pre class="programlisting">sda.sIdx.delete("skeyone");</pre> 58 <p> 59 You can delete any single object by positioning a cursor to 60 that object and then calling the cursor's 61 <code class="methodname">delete()</code> method. 62 </p> 63 <pre class="programlisting">PrimaryIndex<String,SimpleEntityClass> pi = 64 store.getPrimaryIndex(String.class, SimpleEntityClass.class); 65 66SecondaryIndex<String,String,SimpleEntityClass> si = 67 store.getSecondaryIndex(pi, String.class, "sKey"); 68 69EntityCursor<SimpleEntityClass> sec_cursor = 70 si.subIndex("skeyone").entities(); 71 72try { 73 SimpleEntityClass sec; 74 Iterator<SimpleEntityClass> i = sec_cursor.iterator(); 75 while (sec = i.nextDup() != null) { 76 if (sec.getSKey() == "some value") { 77 i.delete(); 78 } 79 } 80// Always make sure the cursor is closed when we are done with it. 81} finally { 82 sec_cursor.close(); } </pre> 83 <p> 84 Finally, if you are indexing by foreign key, then the 85 results of deleting the key is determined by the foreign 86 key constraint that you have set for the index. See 87 <a class="xref" href="dplindexcreate.html#foreignkey" title="Foreign Key Constraints">Foreign Key Constraints</a> 88 for more information. 89 </p> 90 </div> 91 <div class="navfooter"> 92 <hr /> 93 <table width="100%" summary="Navigation footer"> 94 <tr> 95 <td width="40%" align="left"><a accesskey="p" href="dpl_entityjoin.html">Prev</a> </td> 96 <td width="20%" align="center"> 97 <a accesskey="u" href="persist_access.html">Up</a> 98 </td> 99 <td width="40%" align="right"> <a accesskey="n" href="dpl_replace.html">Next</a></td> 100 </tr> 101 <tr> 102 <td width="40%" align="left" valign="top">Join Cursors </td> 103 <td width="20%" align="center"> 104 <a accesskey="h" href="index.html">Home</a> 105 </td> 106 <td width="40%" align="right" valign="top"> Replacing Entity Objects</td> 107 </tr> 108 </table> 109 </div> 110 </body> 111</html> 112