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>Placing Objects in an Entity Store</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="simpleda.html" title="SimpleDA.class" /> 12 <link rel="next" href="simpleget.html" title="Retrieving Objects from an Entity Store" /> 13 </head> 14 <body> 15 <div class="navheader"> 16 <table width="100%" summary="Navigation header"> 17 <tr> 18 <th colspan="3" align="center">Placing Objects in an Entity Store</th> 19 </tr> 20 <tr> 21 <td width="20%" align="left"><a accesskey="p" href="simpleda.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="simpleget.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="simpleput"></a>Placing Objects in an Entity Store</h2> 33 </div> 34 </div> 35 </div> 36 <p> 37 In order to place an object in a DPL entity store, 38 you must: 39 </p> 40 <div class="orderedlist"> 41 <ol type="1"> 42 <li> 43 <p> 44 Open the environment and store. 45 </p> 46 </li> 47 <li> 48 <p> 49 Instantiate the object. 50 </p> 51 </li> 52 <li> 53 <p> 54 Put the object to the store using the 55 <code class="methodname">put()</code> method 56 for the object's primary index. 57 </p> 58 </li> 59 </ol> 60 </div> 61 <p> 62 The following example uses the <code class="classname">SimpleDA</code> 63 class that we show in <a class="xref" href="simpleda.html" title="SimpleDA.class">SimpleDA.class</a> to put a 64 <code class="classname">SimpleEntityClass</code> object (see 65 <a class="xref" href="persist_access.html#simpleentity" title="A Simple Entity Class">A Simple Entity Class</a>) to the 66 entity store. 67 </p> 68 <p> 69 To begin, we import the Java classes that our example 70 needs. We also instantiate the private data members that we 71 require. 72 </p> 73 <pre class="programlisting">package persist.gettingStarted; 74 75import java.io.File; 76import java.io.FileNotFoundException; 77 78import com.sleepycat.db.DatabaseException; 79import com.sleepycat.db.Environment; 80import com.sleepycat.db.EnvironmentConfig; 81 82import com.sleepycat.persist.EntityStore; 83import com.sleepycat.persist.StoreConfig; 84 85public class SimpleStorePut { 86 87 private static File envHome = new File("./JEDB"); 88 89 private Environment envmnt; 90 private EntityStore store; 91 private SimpleDA sda; </pre> 92 <p> 93 Next we create a method that simply opens our database 94 environment and entity store for us. 95 </p> 96 <pre class="programlisting"> // The setup() method opens the environment and store 97 // for us. 98 public void setup() 99 throws DatabaseException { 100 101 EnvironmentConfig envConfig = new EnvironmentConfig(); 102 StoreConfig storeConfig = new StoreConfig(); 103 104 envConfig.setAllowCreate(true); 105 storeConfig.setAllowCreate(true); 106 107 try { 108 // Open the environment and entity store 109 envmnt = new Environment(envHome, envConfig); 110 store = new EntityStore(envmnt, "EntityStore", storeConfig); 111 } catch (FileNotFoundException fnfe) { 112 System.err.println("setup(): " + fnfe.toString()); 113 System.exit(-1); 114 } 115 } </pre> 116 <p> 117 We also need a method to close our environment and store. 118 </p> 119 <pre class="programlisting"> // Close our environment and store. 120 public void shutdown() 121 throws DatabaseException { 122 123 store.close(); 124 envmnt.close(); 125 } </pre> 126 <p> 127 Now we need to create a method to actually write objects to our 128 store. This method creates a <code class="classname">SimpleDA</code> 129 object (see <a class="xref" href="simpleda.html" title="SimpleDA.class">SimpleDA.class</a> that we 130 will use to access our indexes. Then we instantiate a serious 131 of <code class="classname">SimpleEntityClass</code> (see <a class="xref" href="persist_access.html#simpleentity" title="A Simple Entity Class">A Simple Entity Class</a>) 132 instances that we 133 will place in our store. Finally, we use our primary index 134 (obtained from the <code class="classname">SimpleDA</code> class 135 instance) to actually place these objects in our store. 136 </p> 137 <p> 138 In <a class="xref" href="simpleget.html" title="Retrieving Objects from an Entity Store">Retrieving Objects from an Entity Store</a> 139 we show a class that is used to retrieve these objects. 140 </p> 141 <pre class="programlisting"> // Populate the entity store 142 private void run() 143 throws DatabaseException { 144 145 setup(); 146 147 // Open the data accessor. This is used to store 148 // persistent objects. 149 sda = new SimpleDA(store); 150 151 // Instantiate and store some entity classes 152 SimpleEntityClass sec1 = new SimpleEntityClass(); 153 SimpleEntityClass sec2 = new SimpleEntityClass(); 154 SimpleEntityClass sec3 = new SimpleEntityClass(); 155 SimpleEntityClass sec4 = new SimpleEntityClass(); 156 SimpleEntityClass sec5 = new SimpleEntityClass(); 157 158 sec1.setPKey("keyone"); 159 sec1.setSKey("skeyone"); 160 161 sec2.setPKey("keytwo"); 162 sec2.setSKey("skeyone"); 163 164 sec3.setPKey("keythree"); 165 sec3.setSKey("skeytwo"); 166 167 sec4.setPKey("keyfour"); 168 sec4.setSKey("skeythree"); 169 170 sec5.setPKey("keyfive"); 171 sec5.setSKey("skeyfour"); 172 173 sda.pIdx.put(sec1); 174 sda.pIdx.put(sec2); 175 sda.pIdx.put(sec3); 176 sda.pIdx.put(sec4); 177 sda.pIdx.put(sec5); 178 179 shutdown(); 180 } </pre> 181 <p> 182 Finally, to complete our class, we need a 183 <code class="methodname">main()</code> method, which simply calls our 184 <code class="methodname">run()</code> method. 185 </p> 186 <pre class="programlisting"> // main 187 public static void main(String args[]) { 188 SimpleStorePut ssp = new SimpleStorePut(); 189 try { 190 ssp.run(); 191 } catch (DatabaseException dbe) { 192 System.err.println("SimpleStorePut: " + dbe.toString()); 193 dbe.printStackTrace(); 194 } catch (Exception e) { 195 System.out.println("Exception: " + e.toString()); 196 e.printStackTrace(); 197 } 198 System.out.println("All done."); 199 } 200 201} </pre> 202 </div> 203 <div class="navfooter"> 204 <hr /> 205 <table width="100%" summary="Navigation footer"> 206 <tr> 207 <td width="40%" align="left"><a accesskey="p" href="simpleda.html">Prev</a>��</td> 208 <td width="20%" align="center"> 209 <a accesskey="u" href="persist_access.html">Up</a> 210 </td> 211 <td width="40%" align="right">��<a accesskey="n" href="simpleget.html">Next</a></td> 212 </tr> 213 <tr> 214 <td width="40%" align="left" valign="top">SimpleDA.class��</td> 215 <td width="20%" align="center"> 216 <a accesskey="h" href="index.html">Home</a> 217 </td> 218 <td width="40%" align="right" valign="top">��Retrieving Objects from an Entity Store</td> 219 </tr> 220 </table> 221 </div> 222 </body> 223</html> 224