Announcement Announcement Module
Collapse
No announcement yet.
Map SQL query results to Java Enum? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

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

    Comment


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

      Comment


      • #4
        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

        Comment


        • #5
          Don't know, sorry.

          Comment

          Working...
          X