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