I am working with Mybatis-3, and I have the following situation:
I have Class - User which looks as follows:
public class User extends GeneralDto {
private String userId;
private String email;
private String firstName;
private String lastName;
private long creationTimestamp;
private long updateTimestamp;
private List<String> tags;
private HashMap<String, String> attributes;
private HashMap<String, String> accounts;
get and set to all + equals + hashcode.
}
The two hashmaps contain unknown keys and values of the type String (and they are making me a lot of trouble).
I have already mapped this Class into tables using insert method. and it is mapped OK to the following tables scheme:
User (userIdentity, userId, email, firstName, lastName, creationTimestamp, updateTimestamp)
UserAttribute (userIdentity, attributeName, attributeValue, creationTimestamp, updateTimestamp)
UserTag (userIdentity, tagName, creationTimestamp, updateTimestamp)
UserAccount (userIdentity, accountIdentity, role, creationTimestamp, updateTimestamp)
I need to create a GET method. The method receives UserKey object which contains the userId which is the key of a user. and returns an instance of the User Class.
This is the SELECT statement which joins all tables and gets the relevant data from each:
<select id="getUser" parameterType="com.intel.aa.iot.mybatis.UserResultHandler" resultMap="userResultMap" resultOrdered="true">
SELECT
U.userId as userId,
U.email as email,
U.firstName as firstName,
U.lastName as lastName,
U.creationTimestamp as creationTimestamp,
U.updateTimestamp as updateTimestamp,
UT.tagName as tagName,
UAT.attributeName as attributeName,
UAT.attributeValue as attributeValue,
A.accountId as accountId,
UAC.role as role
FROM User U
LEFT OUTER JOIN UserTag UT ON U.userIdentity = UT.userIdentity
LEFT OUTER JOIN UserAttribute UAT ON U.userIdentity = UAT.userIdentity
LEFT OUTER JOIN UserAccount UAC ON U.userIdentity = UAC.userIdentity
LEFT OUTER JOIN ACCOUNTS A ON UAC.accountIdentity = A.accountIdentity
WHERE U.userId = #{userKey.userId}
This query might return more than one row, because of the joins, but all rows are of the given userId.
My question is how do i map this into one result which is an instance of the User Class. I tried using result map but encountered a problem with mapping the hashmap. Then I tried using ResultHandler - returning class called UserProperties which contains each hashmap as two lists (see code below), but unfortunately it didn't work as well - only one value for each list is saved eventually.
The UserProperties class:
public static class UserProperties {
private User user;
private List<String> attributeNames;
private List<String> attributeValues;
private List<String> accountIds;
private List<String> accountRoles;
get and set to all
}
ResultMap:
<resultMap id="userResultMap" type="com.intel.aa.iot.mybatis.UserMapper$UserProperties">
<association property="user" javaType="com.intel.aa.iot.dto.User">
<id property="userId" column="userId"/>
<result property="email" column="email"/>
<result property="firstName" column="firstName"/>
<result property="lastName" column="lastName"/>
<result property="creationTimestamp" column="creationTimestamp"/>
<result property="updateTimestamp" column="updateTimestamp"/>
<collection property="tags" javaType="java.util.ArrayList" column="tagName" ofType="java.lang.String"/>
</association>
<collection property="attributeNames" javaType="java.util.ArrayList" column="attributeName" ofType="java.lang.String"/>
<collection property="attributeValues" javaType="java.util.ArrayList" column="attributeValue" ofType="java.lang.String"/>
<collection property="accountIds" javaType="java.util.ArrayList" column="accountId" ofType="java.lang.String"/>
<collection property="accountRoles" javaType="java.util.ArrayList" column="role" ofType="java.lang.String"/>
</resultMap>
What are my ways for dealing with these Hasmaps?
Thanks!