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>Replacing 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_delete.html" title="Deleting Entity Objects" /> 12 <link rel="next" href="dpl_example.html" title="Chapter 6. A DPL Example" /> 13 </head> 14 <body> 15 <div class="navheader"> 16 <table width="100%" summary="Navigation header"> 17 <tr> 18 <th colspan="3" align="center">Replacing Entity Objects</th> 19 </tr> 20 <tr> 21 <td width="20%" align="left"><a accesskey="p" href="dpl_delete.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_example.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_replace"></a>Replacing Entity Objects</h2> 33 </div> 34 </div> 35 </div> 36 <p> 37 To modify a stored entity object, retrieve it, update 38 it, then put it back to the entity store: 39 </p> 40 <pre class="programlisting"> 41SimpleEntityClass sec = sda.pIdx.get("keyone"); 42sec.setSKey("skeyoneupdated"); 43sda.pIdx.put(sec); 44</pre> 45 <p> 46 Note that because we updated a field on the object that is 47 a secondary key, this object will now be accessible by the 48 secondary key of <code class="literal">skeyoneupdated</code> instead 49 of the previous value, which was <code class="literal">skeyone</code> 50 </p> 51 <p> 52 Be aware that if you modify the object's primary key, the behavior is 53 somewhat different. In this case, you cause a new instance 54 of the object to be created in the store, instead of 55 replacing an existing instance: 56 </p> 57 <pre class="programlisting">// Results in two objects in the store. One with a 58// primary index of "keyfive" and the other with primary index of 59//'keyfivenew'. 60SimpleEntityClass sec = sda.pIdx.get("keyfive"); 61sec.setPKey("keyfivenew"); 62sda.pIdx.put(sec); </pre> 63 <p> 64 Finally, if you are iterating over a collection of objects 65 using an <code class="classname">EntityCursor</code>, you can 66 update each object in turn using 67 <code class="methodname">EntityCursor.update()</code>. Note, 68 however, that you must be iterating using a 69 <code class="classname">PrimaryIndex</code>; this operation is not 70 allowed if you are using a 71 <code class="classname">SecondaryIndex</code>. 72 </p> 73 <p> 74 For example, the following iterates over every 75 <code class="classname">SimpleEntityClass</code> object in the entity 76 store, and it changes them all so that they have a 77 secondary index of <code class="literal">updatedskey</code>: 78 </p> 79 <pre class="programlisting">EntityCursor<SimpleEntityClass> sec_pcursor = sda.pIdx.entities(); 80for (SimpleEntityClass sec : sec_pcursor) { 81 sec.setSKey("updatedskey"); 82 sec_pcursor.update(item); 83} 84sec_pcursor.close(); </pre> 85 </div> 86 <div class="navfooter"> 87 <hr /> 88 <table width="100%" summary="Navigation footer"> 89 <tr> 90 <td width="40%" align="left"><a accesskey="p" href="dpl_delete.html">Prev</a> </td> 91 <td width="20%" align="center"> 92 <a accesskey="u" href="persist_access.html">Up</a> 93 </td> 94 <td width="40%" align="right"> <a accesskey="n" href="dpl_example.html">Next</a></td> 95 </tr> 96 <tr> 97 <td width="40%" align="left" valign="top">Deleting Entity Objects </td> 98 <td width="20%" align="center"> 99 <a accesskey="h" href="index.html">Home</a> 100 </td> 101 <td width="40%" align="right" valign="top"> Chapter 6. A DPL Example</td> 102 </tr> 103 </table> 104 </div> 105 </body> 106</html> 107