Ordered Set with MyBatis

java, mybatis, set, sql-order-by

Solution

MyBatis converts the result to the return type of your method.

If you use a LinkedHashSet, it will create a LinkedHashSet. But if you specify a generic interface it will use a default implementation. In the case of Set I think it is a HashSet.

You can control that behaviour replacing the default ObjectFactory by your own one. Have a look at MyBatis DefaultObjectFactory.

Problem

What is the correct way to define collection implementation using MyBatis. Consider example below. I want `LinkedHashSet` to be returned from mapping. Where should I specify `Set` implementation if I don't want to have `LinkedHashSet` hardcoded in mapping interface. Mapping fragment : ``` <select id="selectAll" resultType="Language"> SELECT <include refid="languageColumns"/> FROM language ORDER BY ord </select> ``` Mapping interface : ``` public interface LanguageDAO { public Set<Language> selectAll(); } ```

Original source