Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ordered Set with MyBatis

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();

}
like image 431
michal.kreuzman Avatar asked Oct 29 '25 13:10

michal.kreuzman


1 Answers

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.

like image 99
Diego Lopez Avatar answered Oct 31 '25 12:10

Diego Lopez



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!