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