Mybatis - Postgresql - How to work with a Collection of enum

2,106 views
Skip to first unread message

xanadu

unread,
Jun 20, 2014, 5:57:16 AM6/20/14
to mybati...@googlegroups.com

Hi !

I have an enum :

public enum Sport{
  SOCCER,
  BASKET;
}

I store my Enum as an array of String in my postgresql database

CREATE TABLE teams(
  id SERIAL UNIQUE, 
  sports text[]
);

The corresponding class looks like this

public class Team(){
  Long id;
  Set<Sport> sports;
...
}

In fact the reality is that I have several collections of enum like Sport in my team class and I would like to know how to deal with that easily. I would prefer avoid creating one CustomTypeHandler for each of the enums.

Is there a way to solve that with maybe a simple EnumTypeHandler ?

Is there another combination jdbcType / javaType to deal with that ? By the way what is the natural combination with jdbcType Array ?

Thanks in advance for your help

Poitras Christian

unread,
Jun 20, 2014, 11:37:52 AM6/20/14
to mybati...@googlegroups.com

MyBatis handles enum out of the box.

 

What you need to do is select the right type handler for your enums.

By default, MyBatis converts enums using enum.name() so it works if the database column is a char, varchar or enum.

Alternatively, MyBatis offers a EnumOrdinalTypeHandler that converts enums using enum.ordinal(). This works if the database column is int or any other integer like types.

 

If you go with ordinals, you can replace de default type handler using:

Configuration.getTypeHandlerRegistry().register(Enum.class, new EnumOrdinalTypeHandler());

For XML, see http://mybatis.github.io/mybatis-3/configuration.html#typeHandlers

 

 

De : mybati...@googlegroups.com [mailto:mybati...@googlegroups.com] De la part de xanadu
Envoyé : June-20-14 5:57 AM
À : mybati...@googlegroups.com
Objet : Mybatis - Postgresql - How to work with a Collection of enum

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mybatis-user...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

xanadu

unread,
Jun 20, 2014, 11:53:02 AM6/20/14
to mybati...@googlegroups.com
Thank you for your answer.

My problem is that it's not an enum but a "Set of enums".
Moreover it's stored as an array of text in the database : text[]

Unfortunately MyBatis doesn't handle that out of the box, I have the following error message :
java.lang.IllegalStateException: No typehandler found for property sports

So I tried to create my own TypeHandler :

@MappedJdbcTypes(JdbcType.ARRAY)
public class SportSetTypeHandler extends BaseTypeHandler<Collection<Sport>>{
 ....
}

but my SportSetTypeHandler is not executed with the following result line :
<result column="sports" property="sports" typeHandler="com.example.SportSetTypeHandler" />

I've tried to add jdbcType="ARRAY" and javaType="collection" but it's still not executed...

FYI I use guice version of MyBatis and in my module I registered the TypeHandler class thanks to
addTypeHandlerClass(SportSetTypeHandler.class);

Maybe I'm doing something wrong, any idea ?

Poitras Christian

unread,
Jun 20, 2014, 11:56:11 AM6/20/14
to mybati...@googlegroups.com

I’m sorry, I didn’t read your question correctly.

 

I am not an expert in postgresql and I am not sure MyBatis handles arrays of text.

I guess you can define your own type handler to convert these to an enum set, but I don’t recall seeing such a thing in the mailing list...

 

In such case, you would probably need to define a generic EnumTypeHandler and register it for each type and/or you would need to specify the type handler in each resultMap.

 

 

De : mybati...@googlegroups.com [mailto:mybati...@googlegroups.com] De la part de Poitras Christian
Envoyé : June-20-14 11:37 AM
À : 'mybati...@googlegroups.com'
Objet : RE: Mybatis - Postgresql - How to work with a Collection of enum

Poitras Christian

unread,
Jun 20, 2014, 11:59:39 AM6/20/14
to mybati...@googlegroups.com

Unfortunately, as I said in my other email, I have no idea how MyBatis handles arrays.

Maybe someone who knows postgresql could help you...

 

It’s bizarre that the type handler is not invoked. Try adding ARRAY as jdbcType in your result map.

 

 

De : mybati...@googlegroups.com [mailto:mybati...@googlegroups.com] De la part de xanadu
Envoyé : June-20-14 11:53 AM
À : mybati...@googlegroups.com
Objet : Re: Mybatis - Postgresql - How to work with a Collection of enum

manitas manitas

unread,
Jun 20, 2014, 12:52:50 PM6/20/14
to mybati...@googlegroups.com
I don't know if my latest answer was sent through Google groups so here it is again :

It was just a spelling error in my column name : no column found = no type handler evoked

So now everything works as expected !

IMHO there is a lack of log in MyBatis, it would be definitely easier to debug if we can have more trace level data about result mapping, typeHandler etc..

Anyway thanks a lot for your help and your reactivity !


--
You received this message because you are subscribed to a topic in the Google Groups "mybatis-user" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mybatis-user/UNXaRG3GMb4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mybatis-user...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages