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