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>Retrieving Objects from 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="simpleput.html" title="Placing Objects in an Entity Store" />
12    <link rel="next" href="getmultiple.html" title="Retrieving Multiple Objects" />
13  </head>
14  <body>
15    <div class="navheader">
16      <table width="100%" summary="Navigation header">
17        <tr>
18          <th colspan="3" align="center">Retrieving Objects from an Entity Store</th>
19        </tr>
20        <tr>
21          <td width="20%" align="left"><a accesskey="p" href="simpleput.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="getmultiple.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="simpleget"></a>Retrieving Objects from an Entity Store</h2>
33          </div>
34        </div>
35      </div>
36      <p>
37                    You retrieve objects placed in an entity store by using
38                    either the object's primary index, or the appropriate
39                    secondary index if it exists. The following application
40                    illustrates this by retrieving some of the objects that
41                    we placed in an entity store in the previous section.
42            </p>
43      <p>
44                To begin, we import the Java classes that our example
45                needs. We also instantiate the private data members that we
46                require.
47           </p>
48      <pre class="programlisting">package persist.gettingStarted;
49
50import java.io.File;
51import java.io.FileNotFoundException;
52
53import com.sleepycat.db.DatabaseException;
54import com.sleepycat.db.Environment;
55import com.sleepycat.db.EnvironmentConfig;
56
57import com.sleepycat.persist.EntityStore;
58import com.sleepycat.persist.StoreConfig;
59
60public class SimpleStoreGet {
61
62    private static File envHome = new File("./JEDB");
63
64    private Environment envmnt;
65    private EntityStore store;
66    private SimpleDA sda; </pre>
67      <p>
68            Next we create a method that simply opens our database
69            environment and entity store for us. 
70    </p>
71      <pre class="programlisting">   // The setup() method opens the environment and store
72    // for us.
73    public void setup()
74        throws DatabaseException {
75
76        EnvironmentConfig envConfig = new EnvironmentConfig();
77        StoreConfig storeConfig = new StoreConfig();
78
79        envConfig.setAllowCreate(true);
80        storeConfig.setAllowCreate(true);
81
82        try {
83            // Open the environment and entity store
84            envmnt = new Environment(envHome, envConfig);
85            store = new EntityStore(envmnt, "EntityStore", storeConfig);
86        } catch (FileNotFoundException fnfe) {
87            System.err.println("setup(): " + fnfe.toString());
88            System.exit(-1);
89        }
90    } </pre>
91      <p>
92            We also need a method to close our environment and store.
93    </p>
94      <pre class="programlisting">    // Close our environment and store.
95    public void shutdown()
96        throws DatabaseException {
97
98        store.close();
99        envmnt.close();
100    } </pre>
101      <p>
102            Now we retrieve a few objects. To do this, we instantiate a
103            <code class="classname">SimpleDA</code> (see <a class="xref" href="simpleda.html" title="SimpleDA.class">SimpleDA.class</a>) class that we use to access
104            our primary and secondary indexes. Then we retrieve objects
105            based on a primary or secondary index value. And finally, we
106            display the retrieved objects.
107     </p>
108      <pre class="programlisting">    // Retrieve some SimpleEntityClass objects from the store.
109    private void run()
110        throws DatabaseException {
111
112        setup();
113
114        // Open the data accessor. This is used to store
115        // persistent objects.
116        sda = new SimpleDA(store);
117
118        // Instantiate and store some entity classes
119        SimpleEntityClass sec1 = sda.pIdx.get("keyone");
120        SimpleEntityClass sec2 = sda.pIdx.get("keytwo");
121
122        SimpleEntityClass sec4 = sda.sIdx.get("skeythree");
123
124        System.out.println("sec1: " + sec1.getPKey());
125        System.out.println("sec2: " + sec2.getPKey());
126        System.out.println("sec4: " + sec4.getPKey());
127
128
129        shutdown();
130    } </pre>
131      <p>
132            Finally, to complete our class, we need a
133            <code class="methodname">main()</code> method, which simply calls our
134            <code class="methodname">run()</code> method.
135    </p>
136      <pre class="programlisting">    // main
137    public static void main(String args[]) {
138        SimpleStoreGet ssg = new SimpleStoreGet();
139        try {
140            ssg.run();
141        } catch (DatabaseException dbe) {
142            System.err.println("SimpleStoreGet: " + dbe.toString());
143            dbe.printStackTrace();
144        } catch (Exception e) {
145            System.out.println("Exception: " + e.toString());
146            e.printStackTrace();
147        }
148        System.out.println("All done.");
149    }
150
151} </pre>
152    </div>
153    <div class="navfooter">
154      <hr />
155      <table width="100%" summary="Navigation footer">
156        <tr>
157          <td width="40%" align="left"><a accesskey="p" href="simpleput.html">Prev</a>��</td>
158          <td width="20%" align="center">
159            <a accesskey="u" href="persist_access.html">Up</a>
160          </td>
161          <td width="40%" align="right">��<a accesskey="n" href="getmultiple.html">Next</a></td>
162        </tr>
163        <tr>
164          <td width="40%" align="left" valign="top">Placing Objects in an Entity Store��</td>
165          <td width="20%" align="center">
166            <a accesskey="h" href="index.html">Home</a>
167          </td>
168          <td width="40%" align="right" valign="top">��Retrieving Multiple Objects</td>
169        </tr>
170      </table>
171    </div>
172  </body>
173</html>
174