0

How to insert data of type java.util.Set into mysql db column of type set(mysql set type) ?

My POJO :

public class UserTEO 
{
    private Integer id;
    private Set changedfieldset;
    //getters and setters
}

xml file :

<sqlMap namespace="user"> 

    <typeAlias alias="USER" type="com.howtodoinjava.ibatis.demo.dto.UserTEO" />
    <insert id="addUser" parameterClass="USER">
        INSERT INTO USERINFO (ID,CHANGEDFIELDSET)
         VALUES(#id#,#changedfieldset#);
    </insert>
</sqlMap>

database :

CREATE TABLE USERINFO
(
    ID INT,
    CHANGEDFIELDSET SET('')
);

Exception :

com.ibatis.common.jdbc.exception.NestedSQLException:   
--- The error occurred in user.xml.  
--- The error occurred while applying a parameter map.  
--- Check the user.addUser-InlineParameterMap.  
--- Check the parameter mapping for the 'changedfieldset' property. 

Please help. Thanks !

1 Answer 1

1

I guess you explicitly want to work with (old) ibatis and not Mybatis. So here is the documentation I referenced to.

The Mysql SET expects a string of set values separated by commas and without white spaces: StringUtils.join(set, ","). So you have to use a type handler to transform the java Set into this string: Extend BaseTypeHandler, speceially overriding setParameter method.

Then call as follows:

INSERT INTO USERINFO (ID,CHANGEDFIELDSET)
         VALUES(#id#,#changedfieldset,handler=YourCustomTypeHandlerTypeAlias#)
Sign up to request clarification or add additional context in comments.

1 Comment

superb ! Thanks ! Meanwhile I changed the getter of my Set to return String which was working fine. But this is the genuine solution. Thanks Buddy .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.