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>DataAccessor.java</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="dpl_example.html" title="Chapter 6. A DPL Example" /> 11 <link rel="prev" href="mydbenv-persist.html" title="MyDbEnv" /> 12 <link rel="next" href="dpl_exampledatabaseput.html" title="ExampleDatabasePut.java" /> 13 </head> 14 <body> 15 <div class="navheader"> 16 <table width="100%" summary="Navigation header"> 17 <tr> 18 <th colspan="3" align="center">DataAccessor.java</th> 19 </tr> 20 <tr> 21 <td width="20%" align="left"><a accesskey="p" href="mydbenv-persist.html">Prev</a> </td> 22 <th width="60%" align="center">Chapter 6. A DPL Example</th> 23 <td width="20%" align="right"> <a accesskey="n" href="dpl_exampledatabaseput.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="dataaccessorclass"></a>DataAccessor.java</h2> 33 </div> 34 </div> 35 </div> 36 <p> 37 Now that we have implemented our data classes, 38 we can write a class that will provide 39 convenient access to our primary and 40 secondary indexes. 41 Note that like our data classes, this class is shared by both our 42 example programs. 43 </p> 44 <p> 45 If you compare this class against our 46 <code class="classname">Vendor</code> and 47 <code class="classname">Inventory</code> 48 class implementations, you will see that the 49 primary and secondary indices declared there are 50 referenced by this class. 51 </p> 52 <p> 53 See <a class="xref" href="dpl_example.html#vendorclass" title="Vendor.java">Vendor.java</a> 54 and 55 <a class="xref" href="inventoryclass.html" title="Inventory.java">Inventory.java</a> 56 for those implementations. 57 </p> 58 <pre class="programlisting">package persist.gettingStarted; 59 60import java.io.File; 61 62import com.sleepycat.db.DatabaseException; 63import com.sleepycat.persist.EntityStore; 64import com.sleepycat.persist.PrimaryIndex; 65import com.sleepycat.persist.SecondaryIndex; 66 67public class DataAccessor { 68 // Open the indices 69 public DataAccessor(EntityStore store) 70 throws DatabaseException { 71 72 // Primary key for Inventory classes 73 inventoryBySku = store.getPrimaryIndex( 74 String.class, Inventory.class); 75 76 // Secondary key for Inventory classes 77 // Last field in the getSecondaryIndex() method must be 78 // the name of a class member; in this case, an Inventory.class 79 // data member. 80 inventoryByName = store.getSecondaryIndex( 81 inventoryBySku, String.class, "itemName"); 82 83 // Primary key for Vendor class 84 vendorByName = store.getPrimaryIndex( 85 String.class, Vendor.class); 86 } 87 88 // Inventory Accessors 89 PrimaryIndex<String,Inventory> inventoryBySku; 90 SecondaryIndex<String,String,Inventory> inventoryByName; 91 92 // Vendor Accessors 93 PrimaryIndex<String,Vendor> vendorByName; 94} </pre> 95 </div> 96 <div class="navfooter"> 97 <hr /> 98 <table width="100%" summary="Navigation footer"> 99 <tr> 100 <td width="40%" align="left"><a accesskey="p" href="mydbenv-persist.html">Prev</a> </td> 101 <td width="20%" align="center"> 102 <a accesskey="u" href="dpl_example.html">Up</a> 103 </td> 104 <td width="40%" align="right"> <a accesskey="n" href="dpl_exampledatabaseput.html">Next</a></td> 105 </tr> 106 <tr> 107 <td width="40%" align="left" valign="top">MyDbEnv </td> 108 <td width="20%" align="center"> 109 <a accesskey="h" href="index.html">Home</a> 110 </td> 111 <td width="40%" align="right" valign="top"> ExampleDatabasePut.java</td> 112 </tr> 113 </table> 114 </div> 115 </body> 116</html> 117