{"id":241,"date":"2010-06-28T22:40:34","date_gmt":"2010-06-28T20:40:34","guid":{"rendered":"http:\/\/mcl.jogger.pl\/2010\/06\/28\/generic-enum-converter-for-ibatis\/"},"modified":"2023-03-20T12:49:04","modified_gmt":"2023-03-20T11:49:04","slug":"generic-enum-converter-for-ibatis","status":"publish","type":"post","link":"https:\/\/touk.pl\/blog\/2010\/06\/28\/generic-enum-converter-for-ibatis\/","title":{"rendered":"Generic Enum converter for iBatis"},"content":{"rendered":"<p>My goal was to create a simple, extensible Enum converter that would work with<\/p>\n<p><a href=\"http:\/\/www.mybatis.org\/\">iBatis<\/a>. This seems like a trivial problem, but took me a while to find a proper solution. There were some additional preconditions:<br \/>\n* all given Enums are jaxb generated objects &#8211; but any standard Java Enum should work<br \/>\n* conversion was 1-to-1, no special conditions and processing The example Enum for this problem looks like this one (copy&amp;paste from jaxb generated source):<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">@XmlType(name =\"ServiceType\") \r\n@XmlEnum\r\npublic enum ServiceType {\r\n\r\n    @XmlEnumValue(\"stationary\")\r\n    STATIONARY(\"stationary\"),\r\n    @XmlEnumValue(\"mobile\")\r\n    MOBILE(\"mobile\");\r\n    private final String value;\r\n\r\n    ServiceType(String v) {\r\n        value = v;\r\n    }\r\n\r\n    public String value() {\r\n        return value;\r\n    }\r\n\r\n    public static ServiceType fromValue(String v) {\r\n        for (ServiceType c: ServiceType.values()) {\r\n            if (c.value.equals(v)) {\r\n                return c;\r\n            }\r\n        }\r\n        throw new IllegalArgumentException(v);\r\n    }\r\n\r\n}<\/pre>\n<p>&#8220;No big deal&#8221;, you say. I beg to differ. What I wanted to achieve was a simple construction which would look like this when used for another Enum (CommonEnumTypeHandler is the name of my generic converter):<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">public class ServiceTypeHandler extends CommonEnumTypeHandler { }<\/pre>\n<p>Unfortunately due to the fact, that Java does not have reified generics, which is described in<\/p>\n<p><a href=\"http:\/\/stackoverflow.com\/questions\/1927789\/why-should-i-care-that-java-doesnt-have-reified-generics\">multiple<\/a> <a href=\"http:\/\/gafter.blogspot.com\/2006\/12\/super-type-tokens.html\">places<\/a>, I had to stick with passing through a Class type of my enum. So it looks like this:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">public class ServiceTypeHandler extends CommonEnumTypeHandler {\r\n\r\n    public ServiceTypeHandler() {\r\n        super(ServiceType.class);\r\n    }\r\n}<\/pre>\n<p>My final class has to look like this one below:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">import java.sql.SQLException;\r\n\r\nimport com.ibatis.sqlmap.client.extensions.ParameterSetter;\r\nimport com.ibatis.sqlmap.client.extensions.ResultGetter;\r\nimport com.ibatis.sqlmap.client.extensions.TypeHandlerCallback;\r\n\r\npublic abstract class CommonEnumTypeHandler implements TypeHandlerCallback {\r\n\r\n    Class enumClass;\r\n\r\n    public CommonEnumTypeHandler(Class clazz) {\r\n        this.enumClass = clazz;\r\n    }\r\n\r\n    public void setParameter(ParameterSetter ps, Object o) throws SQLException {\r\n        if (o.getClass().isAssignableFrom(enumClass)) {\r\n            ps.setString(((T) o).name().toUpperCase());\r\n        } else\r\n            throw new SQLException(\"Excpected \" + enumClass + \" object than: \" + o);\r\n    }\r\n\r\n    public Object getResult(ResultGetter rs) throws SQLException {\r\n        Object o = valueOf(rs.getString());\r\n        if (o == null)\r\n            throw new SQLException(\"Unknown parameter type: \" + rs.getString());\r\n        return o;\r\n    }\r\n\r\n    public Object valueOf(String s) {\r\n        return Enum.valueOf(enumClass, s.toUpperCase());\r\n    }\r\n}<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"My goal was to create a simple, extensible Enum converter that would work with iBatis. This seems like&hellip;\n","protected":false},"author":11,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[687,68],"class_list":{"0":"post-241","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-development-design","7":"tag-db","8":"tag-java"},"_links":{"self":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/241","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/users\/11"}],"replies":[{"embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/comments?post=241"}],"version-history":[{"count":7,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/241\/revisions"}],"predecessor-version":[{"id":15349,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/241\/revisions\/15349"}],"wp:attachment":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/media?parent=241"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/categories?post=241"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/tags?post=241"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}