You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user-java@ibatis.apache.org by Sebastian Niezgoda <sn...@gestalt-llc.com> on 2007/06/12 14:38:42 UTC

Enums and iBatis - still not clear

Hello,

I've read through archives and the wiki but I'm still not clear on how
exactly to handle enums using iBatis.

I use ant's xjc task to create objects from a database schema. The code
tables become Java Enum objects such as:

public enum MyEnum { 

   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;


I do a simple query and in the DAO SQL I create a result map as follows:

<resultMap id="pojoMap" class="MyObject"> 

   <result property="ID" column="ID" />
   <result property="enum" resultMap="MyObject.enumMap" />
</resultMap>

<resultMap id="enumMap" class="MyEnum">
   <result property="?????" value="enum" />
</resultMap>


My question is - how do I map the value I retrieve from db (called enum)
to the MyEnum class?

No matter what I replace the ????? with it doesn't work and it fails
with the following error:

Cause: com.ibatis.common.beans.ProbeException: There is no WRITEABLE
property named '?????'; in class 'MyEnum'

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.  Is
there an easy way to do this?

Thanks,

Sebastian


Re: Enums and iBatis - still not clear

Posted by Larry Meadors <lm...@apache.org>.
On 6/13/07, Sebastian Niezgoda <sn...@gestalt-llc.com> wrote:
> I agree.  I actually was able to configure the handlers correctly with some trial and error
> methodology but I would hope that iBatis would handle these directly.

It can, you just need to contribute that code you wrote to do it. :-)

Uh-oh, I feel a rant coming on...

// BEGIN: Rant about Open Source Software

Open source doesn't mean "other people write it for me", it means the
community works on it together.

If it's something you feel strongly about, put your time (== money)
where your mouth is and write the code to do it or pay for someone to
do it for you. If it's a real show stopper, feel free to contact me
off-list, I'll happily send you an estimate to write the code for you
and contribute it back to the project for inclusion in the next
release...for my regular consulting fee.

It's all about time and money, which are you willing to contribute?

If you are unwilling to contribute either, then your complaints have
no weight - be happy with it as it is for free.

// END: Rant about Open Source Software

Larry

RE: Enums and iBatis - still not clear

Posted by Sebastian Niezgoda <sn...@gestalt-llc.com>.
I agree.  I actually was able to configure the handlers correctly with some trial and error methodology but I would hope that iBatis would handle these directly.


-----Original Message-----
From: Tom Duffey [mailto:tduffey@utilivisor.com]
Sent: Wed 6/13/2007 12:58 AM
To: user-java@ibatis.apache.org
Subject: Re: Enums and iBatis - still not clear
 

On Jun 12, 2007, at 9:32 PM, Paul Benedict wrote:

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

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?  It's a major pain to write  
all these type handlers.

Tom

> Sebastian Niezgoda wrote:
>> Hello,
>>
>> I've read through archives and the wiki but I'm still not clear on  
>> how exactly to handle enums using iBatis.
>>
>> I use ant's xjc task to create objects from a database schema. The  
>> code tables become Java Enum objects such as:
>>
>> public enum MyEnum {
>>    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;
>>
>>
>> I do a simple query and in the DAO SQL I create a result map as  
>> follows:
>>
>> <resultMap id="pojoMap" class="MyObject">
>>    <result property="ID" column="ID" />
>>    <result property="enum" resultMap="MyObject.enumMap" />
>> </resultMap>
>>
>> <resultMap id="enumMap" class="MyEnum">
>>    <result property="?????" value="enum" />
>> </resultMap>
>>
>>
>> My question is - how do I map the value I retrieve from db (called  
>> enum) to the MyEnum class?
>>
>> No matter what I replace the ????? with it doesn't work and it  
>> fails with the following error:
>>
>> Cause: com.ibatis.common.beans.ProbeException: There is no  
>> WRITEABLE property named '?????'; in class 'MyEnum'
>>
>> 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.  Is there an easy way to do this?
>>
>> Thanks,
>>
>> Sebastian
>>



Re: Enums and iBatis - still not clear

Posted by Larry Meadors <lm...@apache.org>.
They aren't too bad to write - more of a nuisance than anything.

There is one example on the WIKI, and I just added another one that I
like better:

http://opensource.atlassian.com/confluence/oss/display/IBATIS/How+do+I+use+Enums+with+annotations

(in case that wraps --> http://tinyurl.com/2mgqd3 <-- this is the same page)

My guess as to why they aren't handled easier is two-fold: time and
compatibility.

I know I don't have the time to implement this in iBATIS in a way that
retains jdk1.4 compatibility - if you do, please feel free. It's open
source - you get to scratch your own itch. :-)

Larry


On 6/12/07, Tom Duffey <td...@utilivisor.com> wrote:
>
>
>
>
> On Jun 12, 2007, at 9:32 PM, Paul Benedict wrote:
>
> 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.
>
> 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?  It's a major pain to write all these type handlers.
>
>
> Tom
>
>
>
> Sebastian Niezgoda wrote:
>      Hello,
>
>  I've read through archives and the wiki but I'm still not clear on how exactly to handle enums using iBatis.
>
>  I use ant's xjc task to create objects from a database schema. The code tables become Java Enum objects such as:
>
>  public enum MyEnum {     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;
>
>
>  I do a simple query and in the DAO SQL I create a result map as follows:
>
>  <resultMap id="pojoMap" class="MyObject">     <result property="ID" column="ID" />
>    <result property="enum" resultMap="MyObject.enumMap" />
> </resultMap>
>
> <resultMap id="enumMap" class="MyEnum">
>    <result property="?????" value="enum" />
> </resultMap>
>
>
>  My question is - how do I map the value I retrieve from db (called enum) to the MyEnum class?
>
>  No matter what I replace the ????? with it doesn't work and it fails with the following error:
>
>  Cause: com.ibatis.common.beans.ProbeException: There is no WRITEABLE property named '?????'; in class 'MyEnum'
>
>  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.  Is there an easy way to do this?
>
>  Thanks,
>
>  Sebastian
>
>
>
>

Re: Enums and iBatis - still not clear

Posted by Tom Duffey <td...@utilivisor.com>.
On Jun 12, 2007, at 9:32 PM, Paul Benedict wrote:

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

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?  It's a major pain to write  
all these type handlers.

Tom

> Sebastian Niezgoda wrote:
>> Hello,
>>
>> I've read through archives and the wiki but I'm still not clear on  
>> how exactly to handle enums using iBatis.
>>
>> I use ant's xjc task to create objects from a database schema. The  
>> code tables become Java Enum objects such as:
>>
>> public enum MyEnum {
>>    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;
>>
>>
>> I do a simple query and in the DAO SQL I create a result map as  
>> follows:
>>
>> <resultMap id="pojoMap" class="MyObject">
>>    <result property="ID" column="ID" />
>>    <result property="enum" resultMap="MyObject.enumMap" />
>> </resultMap>
>>
>> <resultMap id="enumMap" class="MyEnum">
>>    <result property="?????" value="enum" />
>> </resultMap>
>>
>>
>> My question is - how do I map the value I retrieve from db (called  
>> enum) to the MyEnum class?
>>
>> No matter what I replace the ????? with it doesn't work and it  
>> fails with the following error:
>>
>> Cause: com.ibatis.common.beans.ProbeException: There is no  
>> WRITEABLE property named '?????'; in class 'MyEnum'
>>
>> 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.  Is there an easy way to do this?
>>
>> Thanks,
>>
>> Sebastian
>>