cayenne-commits mailing list archives

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

<p>Cayenne allows to use any <a href=""
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><img src="/confluence/download/attachments/79675/ColorEnum.png" align="absmiddle"
border="0" /></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>

  <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;

<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 id="commentsSection" class="wiki-content pageSection">
       <div style="float: right;">
            <a href=""
class="grey">Change Notification Preferences</a>

       <a href="">View
       <a href="">View
       <a href=";showCommentArea=true#addcomment">Add

View raw message