emacarron / mybatis

Automatically exported from code.google.com/p/mybatis
0 stars 0 forks source link

Mapping not working with generics #112

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What version of the MyBatis are you using?
3.0.2

Please describe the problem.  Unit tests are best!
I reported the full description of the problem in this thread on the user group:
http://groups.google.com/group/mybatis-user/browse_thread/thread/106b1e0c7a8b4aa
9

I copy the problem here:
I'm trying to write a "generic" dao for different tables which may 
share a few columns. I defined a hierarchy of mappers which reflects 
this table structure (that is I have a BaseMapper which defines a 
"selectByExample" query, which is overridden by specific mappers). 
All mappers have their own different namespace and this is confermed 
because debugging the session configuration I see that 
"selectByExample" is bound in different namespaces. 
I'm trying to execute this statement: 
U mapper = session.getMapper(namespace); 
List<T> list = mapper.selectByExample(example); 
where <T> is declared as <T extends BaseBean>, U as <U extends 
BaseBeanMapper<T>>,  namespace is a Class<U> and example is an 
instance of <T>. 
I would thik that session.getMapper would return me a Mapper where 
selectByExample is bound to the specific namespace, but this isn't the 
case, I get this exception: 
java.lang.IllegalArgumentException: selectByExample is ambiguous in 
Mapped Statements collection (try using the full name including the 
namespace, or rename one of the entries) 
at org.apache.ibatis.session.Configuration 
$StrictMap.get(Configuration.java:466) 
        at 
org.apache.ibatis.session.Configuration.getMappedStatement(Configuration.ja va: 
349) 
        at 
org.apache.ibatis.binding.MapperMethod.setupCommandType(MapperMethod.java: 
137) 
        at org.apache.ibatis.binding.MapperMethod.<init>(MapperMethod.java: 
46) 
        at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:34) 
        at $Proxy6.selectByExample(Unknown Source) 
I'm using mybatis 3.0.2 on java 1.5, with java configuration (not 
XML), using the "companion XML mapping" pattern described in the user 
guide; the mapping files are like this, where like I said the 
namespace part is different and unique for every mapper: 
<?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="avvocati24.StatoMapper"> 
        <resultMap id="detailStato" type="Stato"> 
                <result property="id" column="idStato" /> 
        </resultMap> 
        <select id="selectByExample" parameterType="Stato" 
                resultMap="detailStato"> 
                select * from Stato 
                <where> 
                        <if test="id != null"> 
                                idStato = #{id} 
                        </if> 
                </where> 
        </select> 
        <select id="selectById" parameterType="int" resultMap="detailStato"> 
                select * 
                from Stato 
                WHERE idStato = #{id} 
        </select> 
</mapper> 

Please provide any additional information below.
The problem can be fully reproduced using the attached maven project.

Original issue reported on code.google.com by riccardo...@gmail.com on 20 Sep 2010 at 10:23

Attachments:

GoogleCodeExporter commented 9 years ago
I forgot to say that the database used by the project is empty, so fixing the 
mapping should produce JDBC errors anyway, but this is outside the scope of 
this issue.

Original comment by riccardo...@gmail.com on 20 Sep 2010 at 10:27

GoogleCodeExporter commented 9 years ago
how to resolve this issue? anyone knows? could you please help me?

Original comment by iamba...@gmail.com on 21 Feb 2011 at 2:42

GoogleCodeExporter commented 9 years ago
I'm having the same issue.  Essentially i have a generic ID property that can 
be a Long and in the near future some other primitive type.  What is happening 
is that i have a base class that has a generic Id type.  The mapper isn't 
setting the id property in the base class because its not of a primitive type.  
my work around is to have the subclass override the id getter and setter to 
return and accept a primitive value.

see below for reference:

public class Base<T extends Serializable> {
     T id;

     T getId();
     void setId( T id );
}
---------------------------

public class Sub extends Base<Long> {
     Long getId();
     void setId( Long id );
}

---------------------------

<resultMap id="test" type="Sub">
     <result property="id" column="id"/>
</resultMap>

---------------------------

Original comment by steven.w...@gmail.com on 25 May 2011 at 8:36

GoogleCodeExporter commented 9 years ago
any updates available for this issue yet?

Original comment by steven.w...@gmail.com on 5 Jul 2011 at 3:59

GoogleCodeExporter commented 9 years ago
Riccardo. I have tried with 3.1.0 and MyBatis fails telling that namespaces are 
wrong. After fixing that there is a JDBC error telling that a table was not 
found.

Anyaway it seems that generis are working (don't know if that has always worked 
or if it has been fixed by chance). You can try with the patch attached

Original comment by eduardo.macarron on 14 Jan 2012 at 10:59

Attachments: