ibatis-user-java mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Paul Benedict <pbened...@apache.org>
Subject Re: Enums and iBatis - still not clear
Date Thu, 14 Jun 2007 02:18:10 GMT
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
There are two ways of persisting enums. You can persist their name
value or ordinal. A standard solution must allow the developer to
choose the persistence strategy.<br>
<br>
Tom Duffey wrote:
<blockquote
 cite="mid:F5907322-30D6-4AFA-9E09-227F8D630CE0@utilivisor.com"
 type="cite"><br>
  <div>
  <div>On Jun 12, 2007, at 9:32 PM, Paul Benedict wrote:</div>
  <br class="Apple-interchange-newline">
  <blockquote type="cite">For each enum you want to write to the
database, you need to write yourself an iBatis type call back handler.
This will translate the enum to whatever data type you want (and int or
a string, etc.), and vice-versa.</blockquote>
  <div><br class="webkit-block-placeholder">
  </div>
This is more of a developer list question but is there any reason why
we can't make iBATIS handle the simple enum case where the name maps
directly to/from the DB automatically? &nbsp;It's a major pain to write all
these type handlers.</div>
  <div><br class="webkit-block-placeholder">
  </div>
  <div>Tom</div>
  <div><br>
  <blockquote type="cite">Sebastian Niezgoda wrote:
    <blockquote cite="mid:1181651922.5490.3.camel@B-0459" type="cite">
Hello,<br>
      <br>
I've read through archives and the wiki but I'm still not clear on how
exactly to handle enums using iBatis.<br>
      <br>
I use ant's xjc task to create objects from a database schema. The code
tables become Java Enum objects such as:<br>
      <br>
public enum MyEnum {
      <pre>   VAL1,
   VAL2;

   public String value() {
      return name();
   }

   public static MyEnum fromValue(String v) {
      return valueOf(v);
   }
}

I have a POJO, MyObject, with the following parameters:

private String ID;
private MyEnum enum;
  </pre>
      <br>
I do a simple query and in the DAO SQL I create a result map as follows:<br>
      <br>
&lt;resultMap id="pojoMap" class="MyObject"&gt;
      <pre>   &lt;result property="ID" column="ID" /&gt;
   &lt;result property="enum" resultMap="MyObject.enumMap" /&gt;
&lt;/resultMap&gt;

&lt;resultMap id="enumMap" class="MyEnum"&gt;
   &lt;result property="?????" value="enum" /&gt;
&lt;/resultMap&gt;
  </pre>
      <br>
My question is - how do I map the value I retrieve from db (called
enum) to the MyEnum class?<br>
      <br>
No matter what I replace the ????? with it doesn't work and it fails
with the following error:<br>
      <br>
Cause: com.ibatis.common.beans.ProbeException: There is no WRITEABLE
property named '?????'; in class 'MyEnum'<br>
      <br>
I could create a bunch of handlers but there are many of them and since
the objects are generated from the schema they can always change.&nbsp; Is
there an easy way to do this?<br>
      <br>
Thanks,<br>
      <br>
Sebastian<br>
      <table cellpadding="0" cellspacing="0" width="100%">
        <tbody>
          <tr>
            <td> <br>
            </td>
          </tr>
        </tbody>
      </table>
    </blockquote>
  </blockquote>
  </div>
  <br>
</blockquote>
</body>
</html>

Mime
View raw message