Results 1 to 5 of 5

Thread: Map SQL query results to Java Enum?

  1. #1
    Join Date
    Jun 2007
    Posts
    4

    Default Map SQL query results to Java Enum?

    Hello,

    Thus far I've been unsuccessful in finding a resolution to my problem. I'm hoping that somebody here will be able to help.

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

    Code:
    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:

    Code:
    private String ID;
    private MyEnum enum;
    I do a simple query and in the DAO SQL I create a result map as follows:

    Code:
    <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:

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

  2. #2
    Join Date
    Jul 2007
    Posts
    2

    Default

    I had similar problem when I was using hibernate.
    Now I am using iBatis and I am having the same problem

    Anyways I'll be using the same soln I was using for Hibernate

    class EnumPropertyContainer {
    private String enumPorperty;

    private MyEnum enum;

    protected String setEnumProperty( String pEnumProperty ) {
    MyEnum temp = MyEnum.valueOf( pEnumProperty );
    this.enum = temp;
    }

    public setEnum( MyEnum pEnum ) {
    this.enum = pEnum;
    this.enumProperty = this.enum == null ? null : this.pEnum.getName();
    }

    }

    You can then use enumProperty in your maps

    Hope this helps.

    Shams

  3. #3
    Join Date
    Jun 2007
    Posts
    4

    Default

    I actually followed the tutorial here http://opensource.atlassian.com/conf...fe+Enumeration and ended up implementing the bottom generic code.

  4. #4
    Join Date
    Jul 2007
    Posts
    2

    Default

    Quote Originally Posted by sniezgoda View Post
    I actually followed the tutorial here http://opensource.atlassian.com/conf...fe+Enumeration and ended up implementing the bottom generic code.
    Thanx.

    Got my problem solved

    Do you know if there's something similar for Hibernate.

    Shams

  5. #5
    Join Date
    Jun 2007
    Posts
    4

    Default

    Don't know, sorry.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •