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>Persistent Objects</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_first.html" title="Chapter 3. Direct Persistence Layer First Steps" /> 11 <link rel="prev" href="persist_first.html" title="Chapter 3. Direct Persistence Layer First Steps" /> 12 <link rel="next" href="saveret.html" title="Saving a Retrieving Data" /> 13 </head> 14 <body> 15 <div class="navheader"> 16 <table width="100%" summary="Navigation header"> 17 <tr> 18 <th colspan="3" align="center">Persistent Objects</th> 19 </tr> 20 <tr> 21 <td width="20%" align="left"><a accesskey="p" href="persist_first.html">Prev</a> </td> 22 <th width="60%" align="center">Chapter 3. Direct Persistence Layer First Steps</th> 23 <td width="20%" align="right"> <a accesskey="n" href="saveret.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="persistobject"></a>Persistent Objects</h2> 33 </div> 34 </div> 35 </div> 36 <p> 37 When using the DPL, you store data in the underlying 38 DB databases by making objects 39 <span class="emphasis"><em>persistent</em></span>. You do this using Java 40 annotations that both identify the type of persistent 41 object you are declaring, as well as the primary and 42 secondary indices. 43 </p> 44 <p> 45 The following are the annotations you will use with your 46 DPL persistent classes: 47 </p> 48 <div class="informaltable"> 49 <table border="1" width="80%"> 50 <colgroup> 51 <col /> 52 <col /> 53 </colgroup> 54 <thead> 55 <tr> 56 <th>Annotation</th> 57 <th>Description</th> 58 </tr> 59 </thead> 60 <tbody> 61 <tr> 62 <td>@Entity</td> 63 <td> 64 Declares an entity class; that is, a class with a primary index 65 and optionally one or more indices. 66 </td> 67 </tr> 68 <tr> 69 <td>@Persistent</td> 70 <td> 71 Declares a persistent class; that is, a class used by an entity 72 class. They do not have indices but instead are are stored or 73 retrieved when an entity class makes direct use of them. 74 </td> 75 </tr> 76 <tr> 77 <td>@PrimaryKey</td> 78 <td> 79 Declares a specific data member in an entity class to be the 80 primary key for that object. This annotation must be used one 81 and only one time for every entity class. 82 </td> 83 </tr> 84 <tr> 85 <td>@SecondaryKey</td> 86 <td> 87 Declares a specific data member in an entity class to be a 88 secondary key for that object. This annotation is optional, and 89 can be used multiple times for an entity class. 90 </td> 91 </tr> 92 </tbody> 93 </table> 94 </div> 95 <p> 96 For example, the following is declared to be an entity class: 97 </p> 98 <pre class="programlisting">package persist.gettingStarted; 99 100import com.sleepycat.persist.model.Entity; 101import com.sleepycat.persist.model.PrimaryKey; 102 103@Entity 104public class ExampleEntity { 105 106 // The primary key must be unique in the database. 107 @PrimaryKey 108 private String aPrimaryKey; 109 110 @SecondaryKey(relate=MANY_TO_ONE) 111 private String aSecondaryKey; 112 113 ... 114 115 // The remainder of the class' implementation is purposefully 116 // omitted in the interest of brevity. 117 118 ... 119} </pre> 120 <p> 121 We discuss primary and secondary keys in more detail in <a class="xref" href="persist_index.html" title="Chapter 4. Working with Indices">Working with Indices</a>. 122 </p> 123 </div> 124 <div class="navfooter"> 125 <hr /> 126 <table width="100%" summary="Navigation footer"> 127 <tr> 128 <td width="40%" align="left"><a accesskey="p" href="persist_first.html">Prev</a> </td> 129 <td width="20%" align="center"> 130 <a accesskey="u" href="persist_first.html">Up</a> 131 </td> 132 <td width="40%" align="right"> <a accesskey="n" href="saveret.html">Next</a></td> 133 </tr> 134 <tr> 135 <td width="40%" align="left" valign="top">Chapter 3. Direct Persistence Layer First Steps </td> 136 <td width="20%" align="center"> 137 <a accesskey="h" href="index.html">Home</a> 138 </td> 139 <td width="40%" align="right" valign="top"> Saving a Retrieving Data</td> 140 </tr> 141 </table> 142 </div> 143 </body> 144</html> 145