cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Cayenne Documentation > Modeling Enumerations
Date Sun, 27 Dec 2009 14:50:00 GMT
<html>
<head>
    <base href="http://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/1519/1/1/_/styles/combined.css?spaceKey=CAYDOC&amp;forWysiwyg=true"
type="text/css">
    </head>
<body style="background-color: white" bgcolor="white">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
     <h2><a href="http://cwiki.apache.org/confluence/display/CAYDOC/Modeling+Enumerations">Modeling
Enumerations</a></h2>
     <h4>Page <b>edited</b> by             <a href="http://cwiki.apache.org/confluence/display/~andrus">Andrus
Adamchik</a>
    </h4>
     
          <br/>
     <div class="notificationGreySide">
         <h2><a name="ModelingEnumerations-MappingandModelingJavaEnumerations"></a>Mapping
and Modeling Java Enumerations</h2>

<p>Cayenne allows to use any <a href="http://java.sun.com/j2se/1.5.0/docs/guide/language/enums.html"
rel="nofollow">Java enumeration</a> as an ObjAttribute type. In CayenneModeler's
ObjEntity editor, under the Attributes tab, enter the full class name for your enumeration
under the Java Type column, and this is it:</p>

<p><div align="center"><img src="/confluence/download/attachments/79675/ColorEnum.png"
border="0" /></div></p>

<p>To convert a DB column value to an enumeration instance and back Cayenne uses enumeration's
<tt>name</tt> for character colums (CHAR, VARCHAR, etc) or its <tt>ordinal</tt>
for numeric columns. Also Cayenne allows users to explicitly control what value in DB corresponds
to a given enumeration instance. To do that, a custom enumeration must implement <tt>org.apache.cayenne.ExtendedEnumeration</tt>
interface, overriding <tt>"getDatabaseValue()"</tt> method to provide the DB value.
Here is an example of a custom enumeration that maps to DB integers:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java"><span class="code-keyword">import</span> org.apache.cayenne.ExtendedEnumeration;

<span class="code-keyword">public</span> <span class="code-keyword">enum</span>
Color <span class="code-keyword">implements</span> ExtendedEnumeration {
  RED(3), GREEN(6), BLUE(9);

  <span class="code-keyword">private</span> <span class="code-object">Integer</span>
value;

  <span class="code-keyword">private</span> Color(<span class="code-object">Integer</span>
value) {
    <span class="code-keyword">this</span>.value = value;
  }

  <span class="code-keyword">public</span> <span class="code-object">Integer</span>
getDatabaseValue() {
    <span class="code-keyword">return</span> value;
  }
}</pre>
</div></div>

<p>This instructs Cayenne to read/write 3, 6, and 9 as RED, GREEN, and BLUE, respectively.
 The order is unimportant - if someone re-orders them to be BLUE, GREEN, and RED in the enum
class, all values will still map correctly.</p>

     </div>
     <div id="commentsSection" class="wiki-content pageSection">
       <div style="float: right;">
            <a href="http://cwiki.apache.org/confluence/users/viewnotifications.action"
class="grey">Change Notification Preferences</a>
       </div>

       <a href="http://cwiki.apache.org/confluence/display/CAYDOC/Modeling+Enumerations">View
Online</a>
       |
       <a href="http://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=79675&revisedVersion=4&originalVersion=3">View
Change</a>
              |
       <a href="http://cwiki.apache.org/confluence/display/CAYDOC/Modeling+Enumerations?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message