Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Obtaining custom Enum from database ID in MyBatis 3
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  5 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Stronkoes  
View profile  
 More options Oct 8 2012, 6:26 am
From: Stronkoes <pie...@gmail.com>
Date: Mon, 8 Oct 2012 03:26:05 -0700 (PDT)
Local: Mon, Oct 8 2012 6:26 am
Subject: Obtaining custom Enum from database ID in MyBatis 3

I have the following MyBatis 3 select statement that selects an ID from the
database and should return the enum belonging to that ID:

  <select id="getMyEnum" resultType="MyEnum">
    SELECT DISTINCT enums.id
    FROM enums    
    WHERE value = #{value}        
  </select>

If i use "enums.name" it works. But if i try it with the id of the enum it
gives me the following exception:

No enum const class domain.MyEnum.1

I have the following enum:

public enum MyEnum implements ValueEnum<Integer>
{
  RED(1), BLACK(2), BLUE(3);

  private Integer id;

  public Integer getValue()
  {
    return id;
  }

  private MyEnum (Integer id)
  {
    this.id = id;
  }

}

With the following type handler:

@MappedJdbcTypes(JdbcType.INTEGER)
@MappedTypes(MyEnum.class)
public class MyEnumTypeHandler extends MyEnumTypeHandlerBase<MyEnum,
Integer>
{}

BaseTypeHandler:

public abstract class MyEnumTypeHandlerBase<E extends Enum<E> &
ValueEnum<T>, T> extends BaseTypeHandler<E>
{
  private Map<Object, ValueEnum<T>> map = new HashMap<Object,
ValueEnum<T>>();
  private JdbcType mappedJdbcType ;

  public MyEnumTypeHandlerBase()
  {
    MappedTypes mappedTypes =
this.getClass().getAnnotation(MappedTypes.class);
    MappedJdbcTypes mappedJdbcTypes =
this.getClass().getAnnotation(MappedJdbcTypes.class);

    if(mappedJdbcTypes.value().length > 0)
    {
      mappedJdbcType = mappedJdbcTypes.value()[0];
    }

    for(Class<?> clazz : mappedTypes.value())
    {
      ValueEnum<T>[] enumConstants = (ValueEnum[]) clazz.getEnumConstants();
      for(ValueEnum<T> enumConstant : enumConstants)
      {
        T value = enumConstant.getValue();
        String key = value.toString();
        map.put(key, enumConstant);
      }
    }
  }

  /******************* Java to JDBC ************************/
  @Override
  public void setNonNullParameter(PreparedStatement ps, int i, E parameter,
JdbcType jdbcType) throws SQLException
  {
    if (jdbcType == null && mappedJdbcType == null)
    {

      ps.setString(i, parameter.getValue().toString());
    } else
    {
      if(mappedJdbcType != null)
        ps.setObject(i, parameter.getValue(), mappedJdbcType.TYPE_CODE);
      else ps.setObject(i, parameter.getValue(), jdbcType.TYPE_CODE);
    }
  }

  /******************* JDBC to Java ************************/
  @Override
  public E getNullableResult(ResultSet rs, String columnName) throws
SQLException
  {    
    Object s = rs.getObject(columnName);
    E value = s == null ? null : (E) map.get(s.toString());
    return value;
  }

  @Override
  public E getNullableResult(ResultSet rs, int columnIndex) throws
SQLException
  {
    Object s = rs.getObject(columnIndex);
    return s == null ? null : (E) map.get(s.toString());
  }

  @Override
  public E getNullableResult(CallableStatement cs, int columnIndex) throws
SQLException
  {
    Object s = cs.getObject(columnIndex);

    return s == null ? null : (E) map.get(s.toString());
  }

}

This used to work in MyBatis 2.3.5 but since my migration to MyBatis 3 not
anymore.

Could anyone tell me if i am doing something wrong, or how i can make it
work?

Thanks in advance!


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Paul Krause  
View profile  
 More options Oct 9 2012, 10:35 am
From: Paul Krause <paulkraus...@alum.mit.edu>
Date: Tue, 9 Oct 2012 07:35:46 -0700 (PDT)
Local: Tues, Oct 9 2012 10:35 am
Subject: Re: Obtaining custom Enum from database ID in MyBatis 3

MB now has built-in type-handlers for enums, both for names and for values;
however, their use is not documented very well.  See
http://code.google.com/p/mybatis/issues/detail?id=624

I was able to get this working after reading
http://profiprog.blogspot.com/2012/03/mapping-enum-java-types-as-numb...


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Stronkoes  
View profile  
 More options Oct 11 2012, 10:19 am
From: Stronkoes <pie...@gmail.com>
Date: Thu, 11 Oct 2012 07:19:04 -0700 (PDT)
Local: Thurs, Oct 11 2012 10:19 am
Subject: Re: Obtaining custom Enum from database ID in MyBatis 3

Thanks for your reply.

If i am correct the type handler that is discussed there select's the enum
in the order that they are written.
So if i have

 BLACK(2), BLUE(3),RED(1);

it will return BLACK if the selected Id is 1

I need to obtain the enum from the integer value that it has, not from the
order it is in.

Op dinsdag 9 oktober 2012 16:35:46 UTC+2 schreef Paul Krause het volgende:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Rob Sonke  
View profile  
 More options Oct 11 2012, 2:03 pm
From: Rob Sonke <r...@tigrou.nl>
Date: Thu, 11 Oct 2012 20:02:59 +0200
Local: Thurs, Oct 11 2012 2:02 pm
Subject: Re: Obtaining custom Enum from database ID in MyBatis 3

As an addition to this post (I'm a colleague), the problem in this case is
not the typehandler itself but the fact that mybatis doesnt pickup the
typehandler. In this case the typehandler isnt used for a specific column
or a parameter but as the result type itself.

Rob


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Paul Krause  
View profile  
 More options Oct 15 2012, 1:16 pm
From: Paul Krause <paulkraus...@alum.mit.edu>
Date: Mon, 15 Oct 2012 10:16:06 -0700 (PDT)
Local: Mon, Oct 15 2012 1:16 pm
Subject: Re: Obtaining custom Enum from database ID in MyBatis 3

You can still do this.  You just need to extend EnumOrdinalTypeHandler
instead of BaseTypeHandler (you'll be overriding all or almost all of its
methods, though).


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »