problem with custom typehandler

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

problem with custom typehandler

Matteo Moci
Hello,
I want to manage a table with a VARCHAR primary key, that in the
mapped java object should be a UUID.

i have my sql-map-config.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
    PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>
        <properties resource="database.properties"/>
    <typeHandlers>
        <typeHandler
handler="com.collective.persistence.typehandlers.UuidTypeHandler"
                     javaType="java.util.UUID"
                     jdbcType="VARCHAR"/>
    </typeHandlers>
        <environments default="development">
                <environment id="development">
                        <transactionManager type="JDBC" />
                        <dataSource type="POOLED">
                                <property name="driver" value="${driver}" />
                                <property name="url" value="${url}" />
                                <property name="username" value="${username}" />
                                <property name="password" value="${password}" />
                        </dataSource>
                </environment>
        </environments>
        <mappers>
                <mapper resource="user.xml" />
    </mappers>
</configuration>


and the user.xml is like that:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="[...].mappers.UserMapper">

    <select id="selectUserByUUID" parameterType="java.util.UUID"
resultMap="userResultMap">
        SELECT * FROM user WHERE uuid = #{uuid, jdbcType=VARCHAR,
typeHandler=[...].persistence.typehandlers.UuidTypeHandler}
    </select>
    <resultMap id="userResultMap" type="[...].model.User">
        <id property="uuid" column="uuid" jdbcType="OTHER"
typeHandler="[...].persistence.typehandlers.UuidTypeHandler"/>
        ...
    </resultMap>
</mapper>

anyway, i got this exception:
org.apache.ibatis.exceptions.PersistenceException:
### Error querying database.  Cause:
org.apache.ibatis.reflection.ReflectionException: There is no getter
for property named 'uuid' in 'class java.util.UUID'
### The error may involve
com.collective.persistence.mappers.UserMapper.selectUserByUUID-Inline
### The error occurred while setting parameters
### Cause: org.apache.ibatis.reflection.ReflectionException: There is
no getter for property named 'uuid' in 'class java.util.UUID'

it seems that my typehandler never gets called (i have it logging a
bit, but never prints anything).
Is there something wrong? Thanks.

Reply | Threaded
Open this post in threaded view
|

Re: problem with custom typehandler

Clinton Begin
Administrator
That error means that whatever you're passing into the statement doesn't have a uuid property.  It fails before getting to the type handler because it can't even get the value in the first place.

The UUID class doesn't have a uuid property.  The easiest way to deal with this is to just pass in the String value of the UUID.

Otherwise try referencing the whole parameter object, which I think is:  "_parameter" or "_param" (can't remember off the top of my head).

Also, unrelated recommendation... your mapped statement will be easier to read if you use an alias for the long class of the type handler: [...].persistence.typehandlers.UuidTypeHandler

<typeAlias name="uuidTH" type="[...].persistence.typehandlers.UuidTypeHandler"/>

Clinton

On Fri, Feb 18, 2011 at 5:46 AM, Matteo Moci <[hidden email]> wrote:
Hello,
I want to manage a table with a VARCHAR primary key, that in the
mapped java object should be a UUID.

i have my sql-map-config.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
   PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
   "http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>
       <properties resource="database.properties"/>
   <typeHandlers>
       <typeHandler
handler="com.collective.persistence.typehandlers.UuidTypeHandler"
                    javaType="java.util.UUID"
                    jdbcType="VARCHAR"/>
   </typeHandlers>
       <environments default="development">
               <environment id="development">
                       <transactionManager type="JDBC" />
                       <dataSource type="POOLED">
                               <property name="driver" value="${driver}" />
                               <property name="url" value="${url}" />
                               <property name="username" value="${username}" />
                               <property name="password" value="${password}" />
                       </dataSource>
               </environment>
       </environments>
       <mappers>
               <mapper resource="user.xml" />
   </mappers>
</configuration>


and the user.xml is like that:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
       PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
       "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="[...].mappers.UserMapper">

   <select id="selectUserByUUID" parameterType="java.util.UUID"
resultMap="userResultMap">
       SELECT * FROM user WHERE uuid = #{uuid, jdbcType=VARCHAR,
typeHandler=[...].persistence.typehandlers.UuidTypeHandler}
   </select>
   <resultMap id="userResultMap" type="[...].model.User">
       <id property="uuid" column="uuid" jdbcType="OTHER"
typeHandler="[...].persistence.typehandlers.UuidTypeHandler"/>
       ...
   </resultMap>
</mapper>

anyway, i got this exception:
org.apache.ibatis.exceptions.PersistenceException:
### Error querying database.  Cause:
org.apache.ibatis.reflection.ReflectionException: There is no getter
for property named 'uuid' in 'class java.util.UUID'
### The error may involve
com.collective.persistence.mappers.UserMapper.selectUserByUUID-Inline
### The error occurred while setting parameters
### Cause: org.apache.ibatis.reflection.ReflectionException: There is
no getter for property named 'uuid' in 'class java.util.UUID'

it seems that my typehandler never gets called (i have it logging a
bit, but never prints anything).
Is there something wrong? Thanks.


Reply | Threaded
Open this post in threaded view
|

Re: problem with custom typehandler

Matteo Moci
Thanks Clinton,
Indeed, mybatis was trying to "get" a non-existing field:
Simone helped me a while ago, suggesting that the easiest way to get
it to work is to add an annotation in the UserMapper to do the thing
you mentioned:

public User selectUserByUUID(@Param("uuid") UUID uuid);

<select id="selectUserByUUID" parameterType="uuid" resultMap="userResultMap">
        SELECT * FROM user WHERE uuid = #{uuid,
typeHandler=com.collective.persistence.typehandlers.UuidTypeHandler,
javaType=uuid, jdbcType=VARCHAR}
    </select>

I was stuck in watching the typehandler configuration, but,
If I understood it right, this has not much to do with typehandlers,
since they are used to "transport and translate" data between the sql
table and the java objects (through setter and getters), while my
issue was in the handling of methods' parameters.





On Fri, Mar 11, 2011 at 2:52 PM, Clinton Begin <[hidden email]> wrote:

> That error means that whatever you're passing into the statement doesn't
> have a uuid property.  It fails before getting to the type handler because
> it can't even get the value in the first place.
> The UUID class doesn't have a uuid property.  The easiest way to deal with
> this is to just pass in the String value of the UUID.
> Otherwise try referencing the whole parameter object, which I think is:
>  "_parameter" or "_param" (can't remember off the top of my head).
> Also, unrelated recommendation... your mapped statement will be easier to
> read if you use an alias for the long class of the type
> handler: [...].persistence.typehandlers.UuidTypeHandler
> <typeAlias name="uuidTH"
> type="[...].persistence.typehandlers.UuidTypeHandler"/>
> Clinton
>
> On Fri, Feb 18, 2011 at 5:46 AM, Matteo Moci <[hidden email]> wrote:
>>
>> Hello,
>> I want to manage a table with a VARCHAR primary key, that in the
>> mapped java object should be a UUID.
>>
>> i have my sql-map-config.xml:
>>
>> <?xml version="1.0" encoding="UTF-8" ?>
>> <!DOCTYPE configuration
>>    PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
>>    "http://mybatis.org/dtd/mybatis-3-config.dtd">
>>
>> <configuration>
>>        <properties resource="database.properties"/>
>>    <typeHandlers>
>>        <typeHandler
>> handler="com.collective.persistence.typehandlers.UuidTypeHandler"
>>                     javaType="java.util.UUID"
>>                     jdbcType="VARCHAR"/>
>>    </typeHandlers>
>>        <environments default="development">
>>                <environment id="development">
>>                        <transactionManager type="JDBC" />
>>                        <dataSource type="POOLED">
>>                                <property name="driver" value="${driver}"
>> />
>>                                <property name="url" value="${url}" />
>>                                <property name="username"
>> value="${username}" />
>>                                <property name="password"
>> value="${password}" />
>>                        </dataSource>
>>                </environment>
>>        </environments>
>>        <mappers>
>>                <mapper resource="user.xml" />
>>    </mappers>
>> </configuration>
>>
>>
>> and the user.xml is like that:
>> <?xml version="1.0" encoding="UTF-8" ?>
>> <!DOCTYPE mapper
>>        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
>>        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
>>
>> <mapper namespace="[...].mappers.UserMapper">
>>
>>    <select id="selectUserByUUID" parameterType="java.util.UUID"
>> resultMap="userResultMap">
>>        SELECT * FROM user WHERE uuid = #{uuid, jdbcType=VARCHAR,
>> typeHandler=[...].persistence.typehandlers.UuidTypeHandler}
>>    </select>
>>    <resultMap id="userResultMap" type="[...].model.User">
>>        <id property="uuid" column="uuid" jdbcType="OTHER"
>> typeHandler="[...].persistence.typehandlers.UuidTypeHandler"/>
>>        ...
>>    </resultMap>
>> </mapper>
>>
>> anyway, i got this exception:
>> org.apache.ibatis.exceptions.PersistenceException:
>> ### Error querying database.  Cause:
>> org.apache.ibatis.reflection.ReflectionException: There is no getter
>> for property named 'uuid' in 'class java.util.UUID'
>> ### The error may involve
>> com.collective.persistence.mappers.UserMapper.selectUserByUUID-Inline
>> ### The error occurred while setting parameters
>> ### Cause: org.apache.ibatis.reflection.ReflectionException: There is
>> no getter for property named 'uuid' in 'class java.util.UUID'
>>
>> it seems that my typehandler never gets called (i have it logging a
>> bit, but never prints anything).
>> Is there something wrong? Thanks.
>>
>
>



--
Matteo Moci
http://it.linkedin.com/in/matteomoci
http://about.me/matteomoci/bio