SQLSession.selectOne() for resultType="map" and resultType="hashmap" returns null

2,602 views
Skip to first unread message

zladidad

unread,
Jan 11, 2011, 2:12:45 PM1/11/11
to mybatis-user
Hi,

I was trying out a basic MyBatis (v3.0.4) feature, namely getting a
(Hash)Map from a selected DB table row. But I can't get it working.

I have the following table and data in a MySQL database:

CREATE TABLE `NotificationService` (
`player` bigint(20) unsigned NOT NULL COMMENT 'Player ID',
`appleDeviceToken` varchar(64) collate latin1_bin default NULL
COMMENT 'Device token',
`enabledAppleNotificationTypes` int(11) default '0' COMMENT 'Enabled
notification types',
`appleBadge` int(10) unsigned default '0' COMMENT 'The badge number
(number of pending events)',
`facebookId` bigint(20) unsigned default '0',
`facebookSessionKey` varchar(150) collate latin1_bin default NULL,
`facebookSettings` int(11) default '0',
`twitterFeed` varchar(15) collate latin1_bin default NULL,
`twitterPassword` varchar(30) collate latin1_bin default NULL,
PRIMARY KEY (`player`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_bin;

INSERT INTO `NotificationService` (`player`, `appleDeviceToken`,
`enabledAppleNotificationTypes`, `appleBadge`, `facebookId`,
`facebookSessionKey`, `facebookSettings`, `twitterFeed`,
`twitterPassword`) VALUES (76356, NULL, 0, 0, 2345, 0x6662534b, 1,
NULL, NULL);

I want to use MyBatis to retrieve a (Hash)Map of the values for the
single row, given the "player" primary key. So, the mapper file entry
looks like this (i hard-coded the "player" parameter in order to make
sure wrong parameter-setting usage can be ruled out as the source of
the problem):

<select id="playerSelectNotificationPlayer2" resultType="map" >
SELECT * FROM db.NotificationService
WHERE player = 76356;
</select>

In the code, i do the following:

@Test
public final void testPlayerSelectNotificationPlayer() {
SqlSession s = null;
try {
s = sessionFactory.openSession();
Object res = s.selectOne("playerSelectNotificationPlayer2");
System.out.println("RESULT IS " + res);
} finally {
if (s != null) {
s.close();
}
}
}

Which gives me the following output:

9:38:12,546 DEBUG PooledDataSource:27 - Created connection 28652556.
19:38:12,562 DEBUG Connection:27 - ooo Connection Opened
19:38:12,640 DEBUG PreparedStatement:27 - ==> Executing: SELECT *
FROM db.NotificationService WHERE player = 76356;
19:38:12,640 DEBUG PreparedStatement:27 - ==> Parameters:
19:38:12,656 DEBUG ResultSet:27 - <== Columns: player,
appleDeviceToken, enabledAppleNotificationTypes, appleBadge,
facebookId, facebookSessionKey, facebookSettings, twitterFeed,
twitterPassword
19:38:12,656 DEBUG ResultSet:27 - <== Row: 76356, null, 0, 0,
2345, fbSK, 1, null, null
RESULT IS null
19:38:12,671 DEBUG Connection:27 - xxx Connection Closed
19:38:12,671 DEBUG PooledDataSource:27 - Returned connection 28652556
to pool.

The System.out.prinltn() indicates that the returned value was "null",
even though the row was retrieved from the DB. The same happens if I
replace resultType="map" with resultType="hashmap" in the mapper file.
The problem also appears when I replace s.selectOne() with
s.selectList(), the single entry in the returned list being a "null"
value.

What am I doing wrong?

--z

zladidad

unread,
Mar 2, 2011, 4:26:26 AM3/2/11
to mybatis-user
It's been almost 2 months since I posted this question. I would really
appreciate any help. I can give some more information I forgot to post
originally.

JDBC driver JAR: mysql-connector-java-5.1.0-bin.jar (also tried
v5.1.12, to no avail)
MySQL server version: 5.0.22-Debian_0ubuntu6.06.11
DB is on a remote host

Anyone?

Thanks in advance.

--z

Poitras Christian

unread,
Mar 2, 2011, 8:22:16 AM3/2/11
to mybati...@googlegroups.com
Try using resultType="java.util.HashMap".

Be aware that the map will contain column values mapped by column name.

Christian

-----Message d'origine-----
De : mybati...@googlegroups.com [mailto:mybati...@googlegroups.com] De la part de zladidad
Envoyé : March-02-11 4:26 AM
À : mybatis-user
Objet : Re: SQLSession.selectOne() for resultType="map" and resultType="hashmap" returns null

zladidad

unread,
Mar 2, 2011, 9:47:09 AM3/2/11
to mybatis-user
Hi Christian,

Thank you very much for your response.

I tried your suggestion, but setting resultType="java.util.HashMap"
still results in SqlSession.selectOne() returning null for the
otherwise same setup as above.

Maybe some more information about my platform might help(?):
Windows XP SP3 (32 bit)
Eclipse v3.5.2 (where I run the JUnit test from)
Used Sun JDK: 1.6.0_14 (for both compilation and as execution
environment)

The JDK I use is pretty old (released May 2009), so I could actually
try the latest Java 6 JDK.

--z

Poitras Christian

unread,
Mar 2, 2011, 10:13:33 AM3/2/11
to mybati...@googlegroups.com
Hi,

Usually, when null is returned, that is because the query returned no row. If the resultType was incorrect, an exception would be raised.
Try running your query on the database and see if it returns exactly one row.

Christian

-----Message d'origine-----
De : mybati...@googlegroups.com [mailto:mybati...@googlegroups.com] De la part de zladidad

Envoyé : March-02-11 9:47 AM


À : mybatis-user
Objet : Re: SQLSession.selectOne() for resultType="map" and resultType="hashmap" returns null

Hi Christian,

zladidad

unread,
Mar 2, 2011, 11:26:38 AM3/2/11
to mybatis-user
Ok, I figured it out. In essence: I am stupid.

Of all details I posted, I forgot to post the MyBatis config file -
and of course, the issue was there, d'oh.

I set "autoMappingBehavior" to "NONE", because MyBatis tried to map DB
columns to Java fields (in other select statements using custom
mappers) that I did not want to have mapped. And with
"autoMappingBehavior" set to "PARTIAL", it works for the above case.

But I do have a follow-up question: Is there a way of excluding
certain columns to be mapped automatically to Java fields when using
"PARTIAL" mapping, and specifying a custom mapper? Deriving from an
example from the user guide:

<select id=”selectUsers” parameterType=”int”
resultMap=”userResultMap”>
select *
from some_table
where id = #{id}
</select>

Assume the DB columns are user_id, user_name, hashed_password. If I
only needed user_id and user_name in my userResultMap, can I instruct
MyBatis not to map any other column, i.e. hashed_password? Or is
specifying the columns in the select statement explicitly the only way
to solve that? Or is there a way of setting "autoMappingBehavior" to
"NONE" for specific selects/mappers? Ideally, my custom mappers would
do all the mapping without MyBatis interfering, and only when
resultType instead of resultMap is used would MyBatis try to figure
out how to do the mapping. So, then I could still use the
resultType="map" feature in some cases, while using custom mappers in
others, and not having to bother to only specify the columns the
mapper needs in the select statement.

Thanks again for your help!

--z

Poitras Christian

unread,
Mar 2, 2011, 11:55:34 AM3/2/11
to mybati...@googlegroups.com
I would say no.
autoMappingBehavior is a global configuration parameter.

To achieve this, I would either select a limited number of columns or use resultMap for all my queries with autoMappingBehavior set to NONE.

Christian

-----Message d'origine-----
De : mybati...@googlegroups.com [mailto:mybati...@googlegroups.com] De la part de zladidad

Envoyé : March-02-11 11:27 AM


À : mybatis-user
Objet : Re: SQLSession.selectOne() for resultType="map" and resultType="hashmap" returns null

Ok, I figured it out. In essence: I am stupid.

zladidad

unread,
Mar 4, 2011, 5:37:12 AM3/4/11
to mybatis-user
Hi Christian,

Thanks again for taking some time to answer my questions.

I guess I'll have a look at which of the 2 options is better suited
for my project.

Keep up the good work with MyBatis.

Cheers
--z

On Mar 2, 5:55 pm, Poitras Christian <Christian.Poit...@ircm.qc.ca>
wrote:
Reply all
Reply to author
Forward
0 new messages