You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user-java@ibatis.apache.org by Metsovuori Juho <Ju...@iltalehti.fi> on 2010/05/06 12:27:40 UTC

Strange problem with resultMaps

Hey all,

I have a this strange problem with resultMaps. When I use the query below to retrieve a users information, everything works great.

    public class User

        private int id = 0;
        private String username = "";
        private String password = "";
        private Timestamp registeredAt;
        private Timestamp lastLogin;
        private List<User> friends = new ArrayList<User>();

    <sql id="table">
        users
        </sql>

    <select id="getUser" parameterType="java.lang.String" resultType="User">
            SELECT * FROM <include refid="table"/> WHERE username = #{username}
      </select>

But I need to add a resultMap, see below, to retrieve all the users friends. When I use this resultMap, I don't get any of the users friends or even the users own username, but I DO get the users password and other info. So do you have any ideas why this doesn't work, and why I don't get the users username. The actualy sql queries do work, I tested them manually.

    <resultMap id="friendByUsernameMap" type="User">
            <association property="friends"
                    column="username"
                    javaType="java.util.ArrayList"          
                     select="eventstream.model.mapper.UserMapper.getFriendsByUsername" />
    </resultMap>

    <select id="getFriendsByUsername"
        parameterType="java.lang.String"
        resultType="User">
            SELECT * FROM <include refid="table"/>
            WHERE id IN
                (SELECT userFriendId
                FROM users_to_users, users
                WHERE users.username = #{username}
                AND users_to_users.userId = users.id)
        </select>

Thanks
Juho

---------------------------------------------------------------------
To unsubscribe, e-mail: user-java-unsubscribe@ibatis.apache.org
For additional commands, e-mail: user-java-help@ibatis.apache.org


RE: Strange problem with resultMaps

Posted by Metsovuori Juho <Ju...@iltalehti.fi>.
Hey,

Thanks a lot! That solved it.

- Juho
________________________________________
From: Jeff Butler [jeffgbutler@gmail.com]
Sent: Thursday, May 06, 2010 5:26 PM
To: user-java@ibatis.apache.org
Subject: Re: Strange problem with resultMaps

If you specify a resultMap, you need to specify a complete result map
- including all the fields from User.

Previously when you were not specifying a resultMap, iBATIS was
building one for you on the fly by introspecting the User class.  But
since you are now specifying the map yourself, iBATIS assumes it is
complete.

Jeff Butler


On Thu, May 6, 2010 at 5:27 AM, Metsovuori Juho
<Ju...@iltalehti.fi> wrote:
> Hey all,
>
> I have a this strange problem with resultMaps. When I use the query below to retrieve a users information, everything works great.
>
>    public class User
>
>        private int id = 0;
>        private String username = "";
>        private String password = "";
>        private Timestamp registeredAt;
>        private Timestamp lastLogin;
>        private List<User> friends = new ArrayList<User>();
>
>    <sql id="table">
>        users
>        </sql>
>
>    <select id="getUser" parameterType="java.lang.String" resultType="User">
>            SELECT * FROM <include refid="table"/> WHERE username = #{username}
>      </select>
>
> But I need to add a resultMap, see below, to retrieve all the users friends. When I use this resultMap, I don't get any of the users friends or even the users own username, but I DO get the users password and other info. So do you have any ideas why this doesn't work, and why I don't get the users username. The actualy sql queries do work, I tested them manually.
>
>    <resultMap id="friendByUsernameMap" type="User">
>            <association property="friends"
>                    column="username"
>                    javaType="java.util.ArrayList"
>                     select="eventstream.model.mapper.UserMapper.getFriendsByUsername" />
>    </resultMap>
>
>    <select id="getFriendsByUsername"
>        parameterType="java.lang.String"
>        resultType="User">
>            SELECT * FROM <include refid="table"/>
>            WHERE id IN
>                (SELECT userFriendId
>                FROM users_to_users, users
>                WHERE users.username = #{username}
>                AND users_to_users.userId = users.id)
>        </select>
>
> Thanks
> Juho
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-java-unsubscribe@ibatis.apache.org
> For additional commands, e-mail: user-java-help@ibatis.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-java-unsubscribe@ibatis.apache.org
For additional commands, e-mail: user-java-help@ibatis.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: user-java-unsubscribe@ibatis.apache.org
For additional commands, e-mail: user-java-help@ibatis.apache.org


Re: Strange problem with resultMaps

Posted by Jeff Butler <je...@gmail.com>.
If you specify a resultMap, you need to specify a complete result map
- including all the fields from User.

Previously when you were not specifying a resultMap, iBATIS was
building one for you on the fly by introspecting the User class.  But
since you are now specifying the map yourself, iBATIS assumes it is
complete.

Jeff Butler


On Thu, May 6, 2010 at 5:27 AM, Metsovuori Juho
<Ju...@iltalehti.fi> wrote:
> Hey all,
>
> I have a this strange problem with resultMaps. When I use the query below to retrieve a users information, everything works great.
>
>    public class User
>
>        private int id = 0;
>        private String username = "";
>        private String password = "";
>        private Timestamp registeredAt;
>        private Timestamp lastLogin;
>        private List<User> friends = new ArrayList<User>();
>
>    <sql id="table">
>        users
>        </sql>
>
>    <select id="getUser" parameterType="java.lang.String" resultType="User">
>            SELECT * FROM <include refid="table"/> WHERE username = #{username}
>      </select>
>
> But I need to add a resultMap, see below, to retrieve all the users friends. When I use this resultMap, I don't get any of the users friends or even the users own username, but I DO get the users password and other info. So do you have any ideas why this doesn't work, and why I don't get the users username. The actualy sql queries do work, I tested them manually.
>
>    <resultMap id="friendByUsernameMap" type="User">
>            <association property="friends"
>                    column="username"
>                    javaType="java.util.ArrayList"
>                     select="eventstream.model.mapper.UserMapper.getFriendsByUsername" />
>    </resultMap>
>
>    <select id="getFriendsByUsername"
>        parameterType="java.lang.String"
>        resultType="User">
>            SELECT * FROM <include refid="table"/>
>            WHERE id IN
>                (SELECT userFriendId
>                FROM users_to_users, users
>                WHERE users.username = #{username}
>                AND users_to_users.userId = users.id)
>        </select>
>
> Thanks
> Juho
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-java-unsubscribe@ibatis.apache.org
> For additional commands, e-mail: user-java-help@ibatis.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-java-unsubscribe@ibatis.apache.org
For additional commands, e-mail: user-java-help@ibatis.apache.org