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 Shepherdz <zh...@ndtech.com.cn> on 2006/05/22 07:48:15 UTC

Can I use Map or XML result in mapping complex properties

Our project needs to use generic result structure to recieve results of a
query, i.e. Map or XML. How can I use Map or XML result in mapping complex
properties? 
 
Following is a sample mapping file I write.
 
<sqlMap namespace="User">
  <resultMap id="interestGroupResult" class="java.util.HashMap"
groupBy="id">
    <result property="id" column="GroupID"/>
    <result property="name" column="GroupName"/>
    <result property="description" column="GroupDesp"/>
    <result property="interests" resultMap="User.interestsResult"/>
  </resultMap>
  <resultMap id="interestsResult" class="java.util.HashMap" groupBy="id">
    <result property="id" column="InterestID"/>
    <result property="name" column="InterestName"/>
    <result property="description" column="InterestDesp"/>
    <result property="users" resultMap="User.userResult"/>
  </resultMap>
  <resultMap id="userResult" class="java.util.HashMap">
    <result property="id" column="UserID"/>
    <result property="name" column="UserName"/>
    <result property="password" column="UserPassword"/>
    <result property="firstName" column="UserFirstName"/>
    <result property="middleName" column="UserMiddleName"/>
    <result property="lastName" column="UserLastName"/>
    <result property="age" column="UserAge"/>
    <result property="affiliation" column="UserAffiliation"/>
    <result property="email" column="UserEmail"/>
    <result property="telephone" column="UserTelephone"/>
    <result property="mobile" column="UserMobile"/>
  </resultMap>
  <select id="showGroupUser" parameterClass="java.lang.String"
resultMap="interestGroupResult">
    SELECT G.ID as GroupID, G.Name as GroupName, G.Description as GroupDesp,
    I.ID as InterestID, I.Name as InterestName, I.Description as
InterestDesp,
    U.ID as UserID, U.Name as UserName, U.Password as UserPassword,
U.FirstName as UserFirstName, U.MiddleName as UserMiddleName, 
    U.LastName as UserLastName, U.Age as UserAge, U.Affiliation as
UserAffiliation, U.Email as UserEmail, U.Telephone as UserTelephone, 
    U.Mobile as UserMobile
    FROM UserInterestsGroup G
    LEFT OUTER JOIN UserInterest I ON I.InterestGroup = G.ID
    LEFT OUTER JOIN Users U ON U.Interest = I.ID
    WHERE G.ID = #value#
 </select>
</sqlMap>
 
However, an exception is thrown: 
"com.ibatis.sqlmap.client.SqlMapException: Error instantiating collection
property for mapping 'interests'.  Cause: java.lang.ClassCastException:
java.lang.Object"
 
Is my mapping statement invalid? Or the iBatis does not allow mapping Map or
XML as collection propery? 

--
View this message in context: http://www.nabble.com/Can+I+use+Map+or+XML+result+in+mapping+complex+properties-t1661124.html#a4500363
Sent from the iBATIS - User - Java forum at Nabble.com.


Re: Can I use Map or XML result in mapping complex properties

Posted by roan <ro...@oberonassociates.com>.
Hi,

I have been trying to do the same with XML resultMaps, and I *think* I
figured it out. Have not tested this exhaustively, but got expected results
from last couple of junit's I ran.

Key is the "xmlCollection" type alias, which is registered in
com.ibatis.sqlmap.engine.builder.xml.SqlMapConfigParser#registerDefaultTypeAliases 

Armed with that (undocumented?) type alias, I declared the following in my
xml resultMap:
    <result property="locations" column="locid" javaType="xmlCollection"
select="MYINFO.findLocationById" />

This is how my findLocationById statement is declared:
   <resultMap id="locationXmlResult" class="xml" xmlName="location" >
       ... 
   </resultMap>   
   <select id="findLocationsById" resultMap="locationXmlResult"
parameterClass="java.lang.String">
       ....
   </select>

I'm curious if this approach to "xmlCollection" properties is documented
anywhere, or at least whether this is how it is intended to work. Haven't
been able to find anything on the lists ... any iBATIS experts out there who
can say one way or the other?

Obviously current setup doesn't solve the N+1 selects problem. That's the
next step. 

For the record ... I know a lot of changes are being considered for 3.0
(especially around XML), and just want to let you guys know that (on a 
current project) the iBATIS XML support has made it possible to put together
a data access layer that populates XML Beans from an RDBMS with a bare
minimum of plumbing. It's actually quite elegant. Without being able to
return XML, it would not be possible to populate XML Beans through the
iBATIS framework since there are no default constructors in XML Beans. My
two cents.

Roan



Shepherdz wrote:
> 
> Our project needs to use generic result structure to recieve results of a
> query, i.e. Map or XML. How can I use Map or XML result in mapping complex
> properties? 
>  ....  
> 

-- 
View this message in context: http://www.nabble.com/Can-I-use-Map-or-XML-result-in-mapping-complex-properties-tf1661124.html#a8700325
Sent from the iBATIS - User - Java mailing list archive at Nabble.com.


Re: Can I use Map or XML result in mapping complex properties

Posted by Brandon Goodin <br...@gmail.com>.
iB3 is looking to avoid being the monolith solution. This does not
mean that there will not be any XML support. It means there will not
be XML support in the core API. It should be possible for an extension
to be written to handle XML.

Brandon

On 5/22/06, Shepherdz <zh...@ndtech.com.cn> wrote:
>
> Thx for responsing. However, XML support is really important.
>
> In our project, we use AJAX in the web tier. XML is the very representation
> for business data. If the ORM can return XML result, we can get rid of a lot
> of meaningless reflections in our system. More over, XML is a good structure
> in representing information. For data processing, we can use JAXP and XSLT,
> partially at least. Also, we can use Prototype pattern to encapsulate the
> dom object as generic object class.
>
> Actually, in our project we decide to use XML everywhere and we look for
> some ORM approach to support our scenario.
>
> Hibernate has announced XML support in 3.0, although it is said to be "an
> experimental feature in Hibernate 3.0 and is under extremely active
> development". Sincerely to say, getting away XML support in ORM is a rather
> stupid decision...
>
> Shepherd
> --
> View this message in context: http://www.nabble.com/Can+I+use+Map+or+XML+result+in+mapping+complex+properties-t1661124.html#a4515908
> Sent from the iBATIS - User - Java forum at Nabble.com.
>
>

Re: Can I use Map or XML result in mapping complex properties

Posted by Shepherdz <zh...@ndtech.com.cn>.
Thx for responsing. However, XML support is really important.

In our project, we use AJAX in the web tier. XML is the very representation
for business data. If the ORM can return XML result, we can get rid of a lot
of meaningless reflections in our system. More over, XML is a good structure
in representing information. For data processing, we can use JAXP and XSLT,
partially at least. Also, we can use Prototype pattern to encapsulate the
dom object as generic object class.

Actually, in our project we decide to use XML everywhere and we look for
some ORM approach to support our scenario.

Hibernate has announced XML support in 3.0, although it is said to be "an
experimental feature in Hibernate 3.0 and is under extremely active
development". Sincerely to say, getting away XML support in ORM is a rather
stupid decision...

Shepherd
--
View this message in context: http://www.nabble.com/Can+I+use+Map+or+XML+result+in+mapping+complex+properties-t1661124.html#a4515908
Sent from the iBATIS - User - Java forum at Nabble.com.


Re: Can I use Map or XML result in mapping complex properties

Posted by Guido García Bernardo <gg...@tid.es>.
Larry Meadors escribió:
> Don't use the XML support in iBATIS - it's pretty crappy and very
> likely going to go away in iB3.
+1 :)
>
> Larry
>
>
> On 5/21/06, Shepherdz <zh...@ndtech.com.cn> wrote:
>>
>> Our project needs to use generic result structure to recieve results 
>> of a
>> query, i.e. Map or XML. How can I use Map or XML result in mapping 
>> complex
>> properties?
>>
>> Following is a sample mapping file I write.
>>
>> <sqlMap namespace="User">
>>   <resultMap id="interestGroupResult" class="java.util.HashMap"
>> groupBy="id">
>>     <result property="id" column="GroupID"/>
>>     <result property="name" column="GroupName"/>
>>     <result property="description" column="GroupDesp"/>
>>     <result property="interests" resultMap="User.interestsResult"/>
>>   </resultMap>
>>   <resultMap id="interestsResult" class="java.util.HashMap" 
>> groupBy="id">
>>     <result property="id" column="InterestID"/>
>>     <result property="name" column="InterestName"/>
>>     <result property="description" column="InterestDesp"/>
>>     <result property="users" resultMap="User.userResult"/>
>>   </resultMap>
>>   <resultMap id="userResult" class="java.util.HashMap">
>>     <result property="id" column="UserID"/>
>>     <result property="name" column="UserName"/>
>>     <result property="password" column="UserPassword"/>
>>     <result property="firstName" column="UserFirstName"/>
>>     <result property="middleName" column="UserMiddleName"/>
>>     <result property="lastName" column="UserLastName"/>
>>     <result property="age" column="UserAge"/>
>>     <result property="affiliation" column="UserAffiliation"/>
>>     <result property="email" column="UserEmail"/>
>>     <result property="telephone" column="UserTelephone"/>
>>     <result property="mobile" column="UserMobile"/>
>>   </resultMap>
>>   <select id="showGroupUser" parameterClass="java.lang.String"
>> resultMap="interestGroupResult">
>>     SELECT G.ID as GroupID, G.Name as GroupName, G.Description as 
>> GroupDesp,
>>     I.ID as InterestID, I.Name as InterestName, I.Description as
>> InterestDesp,
>>     U.ID as UserID, U.Name as UserName, U.Password as UserPassword,
>> U.FirstName as UserFirstName, U.MiddleName as UserMiddleName,
>>     U.LastName as UserLastName, U.Age as UserAge, U.Affiliation as
>> UserAffiliation, U.Email as UserEmail, U.Telephone as UserTelephone,
>>     U.Mobile as UserMobile
>>     FROM UserInterestsGroup G
>>     LEFT OUTER JOIN UserInterest I ON I.InterestGroup = G.ID
>>     LEFT OUTER JOIN Users U ON U.Interest = I.ID
>>     WHERE G.ID = #value#
>>  </select>
>> </sqlMap>
>>
>> However, an exception is thrown:
>> "com.ibatis.sqlmap.client.SqlMapException: Error instantiating 
>> collection
>> property for mapping 'interests'.  Cause: java.lang.ClassCastException:
>> java.lang.Object"
>>
>> Is my mapping statement invalid? Or the iBatis does not allow mapping 
>> Map or
>> XML as collection propery?
>>
>> -- 
>> View this message in context: 
>> http://www.nabble.com/Can+I+use+Map+or+XML+result+in+mapping+complex+properties-t1661124.html#a4500363 
>>
>> Sent from the iBATIS - User - Java forum at Nabble.com.
>>
>>
>


-- 
Guido García Bernardo

ITDEUSTO - Valladolid


Re: Can I use Map or XML result in mapping complex properties

Posted by Larry Meadors <lm...@apache.org>.
Don't use the XML support in iBATIS - it's pretty crappy and very
likely going to go away in iB3.

Larry


On 5/21/06, Shepherdz <zh...@ndtech.com.cn> wrote:
>
> Our project needs to use generic result structure to recieve results of a
> query, i.e. Map or XML. How can I use Map or XML result in mapping complex
> properties?
>
> Following is a sample mapping file I write.
>
> <sqlMap namespace="User">
>   <resultMap id="interestGroupResult" class="java.util.HashMap"
> groupBy="id">
>     <result property="id" column="GroupID"/>
>     <result property="name" column="GroupName"/>
>     <result property="description" column="GroupDesp"/>
>     <result property="interests" resultMap="User.interestsResult"/>
>   </resultMap>
>   <resultMap id="interestsResult" class="java.util.HashMap" groupBy="id">
>     <result property="id" column="InterestID"/>
>     <result property="name" column="InterestName"/>
>     <result property="description" column="InterestDesp"/>
>     <result property="users" resultMap="User.userResult"/>
>   </resultMap>
>   <resultMap id="userResult" class="java.util.HashMap">
>     <result property="id" column="UserID"/>
>     <result property="name" column="UserName"/>
>     <result property="password" column="UserPassword"/>
>     <result property="firstName" column="UserFirstName"/>
>     <result property="middleName" column="UserMiddleName"/>
>     <result property="lastName" column="UserLastName"/>
>     <result property="age" column="UserAge"/>
>     <result property="affiliation" column="UserAffiliation"/>
>     <result property="email" column="UserEmail"/>
>     <result property="telephone" column="UserTelephone"/>
>     <result property="mobile" column="UserMobile"/>
>   </resultMap>
>   <select id="showGroupUser" parameterClass="java.lang.String"
> resultMap="interestGroupResult">
>     SELECT G.ID as GroupID, G.Name as GroupName, G.Description as GroupDesp,
>     I.ID as InterestID, I.Name as InterestName, I.Description as
> InterestDesp,
>     U.ID as UserID, U.Name as UserName, U.Password as UserPassword,
> U.FirstName as UserFirstName, U.MiddleName as UserMiddleName,
>     U.LastName as UserLastName, U.Age as UserAge, U.Affiliation as
> UserAffiliation, U.Email as UserEmail, U.Telephone as UserTelephone,
>     U.Mobile as UserMobile
>     FROM UserInterestsGroup G
>     LEFT OUTER JOIN UserInterest I ON I.InterestGroup = G.ID
>     LEFT OUTER JOIN Users U ON U.Interest = I.ID
>     WHERE G.ID = #value#
>  </select>
> </sqlMap>
>
> However, an exception is thrown:
> "com.ibatis.sqlmap.client.SqlMapException: Error instantiating collection
> property for mapping 'interests'.  Cause: java.lang.ClassCastException:
> java.lang.Object"
>
> Is my mapping statement invalid? Or the iBatis does not allow mapping Map or
> XML as collection propery?
>
> --
> View this message in context: http://www.nabble.com/Can+I+use+Map+or+XML+result+in+mapping+complex+properties-t1661124.html#a4500363
> Sent from the iBATIS - User - Java forum at Nabble.com.
>
>