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 mikkapl <fo...@wp.pl> on 2009/04/02 12:58:48 UTC

[resultMap] related objects

hello,
I want to get Object related to another object like this (I bolded important
things):

Company {
Integer id;
String name;
Address address;
}

Address {
Integer addressId;
String street;
}

SQL:
SELECT c.*, a.*
FROM companies c, addresses a
WHERE a.address_id = c.address_id;


...and there is a problem with resultMap:


something like that doesn't work:

<resultMap id="addr-result" class="Address">
<result property="addressId" column=".." />
<result property="street" column=".." />
</resultMap>

<resultMap id="comp-result" class="Company">
<result property="id" column=".." />
<result property="name" column=".." />
<result property="address" resultMap="address-result" />
</resultMap>


-- 
View this message in context: http://www.nabble.com/-resultMap--related-objects-tp22845356p22845356.html
Sent from the iBATIS - User - Java mailing list archive at Nabble.com.


Re: [resultMap] related objects

Posted by Ingmar Lötzsch <il...@asci-systemhaus.de>.
> I get exception:
> --- Check the company.company-result.  
> --- The error happened while setting a property on the result object.  
> --- Cause: com.ibatis.sqlmap.client.SqlMapException: Error instantiating
> collection property for mapping 'address'.  Cause:
> java.lang.ClassCastException: 

The exception says: java.lang.ClassCastException.

Can you provide the source of class Company?

> I have Company.xml where is all typeAliases, resultMap, sql statement
> I use useStatementNamespaces="true", so I must have <sqlMap
> namespace="company">

I think, this is the best approach.

> DAO:
> return getSqlMapClientTemplate().queryForList("company.getAll");

I'am using Spring too.

> XML:
> <select id="getAll" resultMap="company-result">
> SELECT 
> 	c.company_id, c.companyname,
> 	a.address_id, a.street
> FROM hh_companies c
> LEFT JOIN hh_addresses a ON c.address_id = a.address_id
> </select>
> ** all companies have its addresses; no null values

Than you should INNER JOIN instead of OUTER JOIN.

> <resultMap id="address-result" class="Address">
> 	<result property="addressId" column="address_id" />
> 	<result property="street" column="street" />
> </resultMap>
> 	
> <resultMap id="company-result" class="Company">
> 	<result property="companyId" column="company_id" />
> 	<result property="name" column="companyname" />
> 	<result property="address" javaType="Address"
> resultMap="company.address-result" />
> </resultMap>

You can omit the attribute javaType. I assume it is ignored due to the
presence of the resultMap attribute. Apart from that the type is
declared in the resultMap element.

Ingmar

Re: [resultMap] related objects

Posted by mikkapl <fo...@wp.pl>.


Richard Yee wrote:
> 
> Should your resultmap be address-result instead of company.address- 
> result?
> 

Then I get:
--- Check the company.company-result.  
--- The error happened while setting a property on the result object.  
--- Cause: com.ibatis.sqlmap.client.SqlMapException: There is no result map
named address-result in this SqlMap.;


-- 
View this message in context: http://www.nabble.com/-resultMap--related-objects-tp22845356p22848287.html
Sent from the iBATIS - User - Java mailing list archive at Nabble.com.


Re: [resultMap] related objects

Posted by Richard Yee <ry...@cruzio.com>.
Should your resultmap be address-result instead of company.address- 
result?

Richard

Sent from my iPhone

On Apr 2, 2009, at 6:25 AM, mikkapl <fo...@wp.pl> wrote:

>
>
>
> Ingmar Lötzsch wrote:
>>
>>> You don't need a custom TypeHandler. But perhaps you have to provide
>> more information.
>>
>
> I get exception:
> --- Check the company.company-result.
> --- The error happened while setting a property on the result object.
> --- Cause: com.ibatis.sqlmap.client.SqlMapException: Error  
> instantiating
> collection property for mapping 'address'.  Cause:
> java.lang.ClassCastException:
>
>
> I have Company.xml where is all typeAliases, resultMap, sql statement
> I use useStatementNamespaces="true", so I must have <sqlMap
> namespace="company">
>
> DAO:
> return getSqlMapClientTemplate().queryForList("company.getAll");
>
> XML:
> <select id="getAll" resultMap="company-result">
> SELECT
>    c.company_id, c.companyname,
>    a.address_id, a.street
> FROM hh_companies c
> LEFT JOIN hh_addresses a ON c.address_id = a.address_id
> </select>
> ** all companies have its addresses; no null values
>
> <resultMap id="address-result" class="Address">
>    <result property="addressId" column="address_id" />
>    <result property="street" column="street" />
> </resultMap>
>
> <resultMap id="company-result" class="Company">
>    <result property="companyId" column="company_id" />
>    <result property="name" column="companyname" />
>    <result property="address" javaType="Address"
> resultMap="company.address-result" />
> </resultMap>
>
>
>
> -- 
> View this message in context: http://www.nabble.com/-resultMap--related-objects-tp22845356p22847806.html
> Sent from the iBATIS - User - Java mailing list archive at Nabble.com.
>

Re: [resultMap] related objects

Posted by mikkapl <fo...@wp.pl>.


Ingmar Lötzsch wrote:
> 
>> You don't need a custom TypeHandler. But perhaps you have to provide
> more information.
> 

I get exception:
--- Check the company.company-result.  
--- The error happened while setting a property on the result object.  
--- Cause: com.ibatis.sqlmap.client.SqlMapException: Error instantiating
collection property for mapping 'address'.  Cause:
java.lang.ClassCastException: 


I have Company.xml where is all typeAliases, resultMap, sql statement
I use useStatementNamespaces="true", so I must have <sqlMap
namespace="company">

DAO:
return getSqlMapClientTemplate().queryForList("company.getAll");

XML:
<select id="getAll" resultMap="company-result">
SELECT 
	c.company_id, c.companyname,
	a.address_id, a.street
FROM hh_companies c
LEFT JOIN hh_addresses a ON c.address_id = a.address_id
</select>
** all companies have its addresses; no null values

<resultMap id="address-result" class="Address">
	<result property="addressId" column="address_id" />
	<result property="street" column="street" />
</resultMap>
	
<resultMap id="company-result" class="Company">
	<result property="companyId" column="company_id" />
	<result property="name" column="companyname" />
	<result property="address" javaType="Address"
resultMap="company.address-result" />
</resultMap>



-- 
View this message in context: http://www.nabble.com/-resultMap--related-objects-tp22845356p22847806.html
Sent from the iBATIS - User - Java mailing list archive at Nabble.com.


Re: [resultMap] related objects

Posted by Ingmar Lötzsch <il...@asci-systemhaus.de>.
> Do I have to use TypeHandler? But how?

You don't need a custom TypeHandler. But perhaps you have to provide
more information.

What are the column names of the tables "companies" and "adresses"?

Your first message shows that they are different from the class properties.

Which packages contain the classes Company and Address?

Did you declare aliases for these classes?

Which files contain the resultMap elements?

Which packages contain these files?

How looks your <select> element?

How do you invoke the SQL-Statement?

What exceptions do you get?

What DBMS do you use?

Assuming that you have tables

companies
(
	id int,
	name text,
	adress_id int
)

adresses
(
	address_id int,
	street text
)

Than you can write basically

<resultMap id="address-result" class="Address">
	<result property="addressId" column="adress_id" />
	<result property="street" column="street" />
</resultMap>

<resultMap id="comp-result" class="Company">
	<result property="id" column="id" />
	<result property="name" column="name" />
	<result property="address" resultMap="address-result" />
</resultMap>

<select id="selectAllCompanies" resultMap="comp-result">
	SELECT
		c.id,
		c.name,
		a.adress_id,
		a.street
	FROM companies c, addresses a
	WHERE a.address_id = c.address_id
</select>

In your DAO

List<Company> list = sqlMapClient.queryForList("selectAllCompanies");

Ingmar

Re: [resultMap] related objects

Posted by Brandon Goodin <br...@gmail.com>.
What is the exception?

Brandon

On Thu, Apr 2, 2009 at 6:38 AM, mikkapl <fo...@wp.pl> wrote:

>
>
> yeah, I made mistake... I didn't paste it just type. But It doesn't work
> even if it will be matched.
> It doesn't work at all.
> Do I have to use TypeHandler? But how?
>
>
> > This example can't work, because
> > resultMap="address-result"
> >
> > does not match
> >
> > <resultMap id="addr-result"
> --
> View this message in context:
> http://www.nabble.com/-resultMap--related-objects-tp22845356p22845909.html
> Sent from the iBATIS - User - Java mailing list archive at Nabble.com.
>
>

Re: [resultMap] related objects

Posted by mikkapl <fo...@wp.pl>.

yeah, I made mistake... I didn't paste it just type. But It doesn't work
even if it will be matched.
It doesn't work at all.
Do I have to use TypeHandler? But how?


> This example can't work, because
> resultMap="address-result"
> 
> does not match
> 
> <resultMap id="addr-result"
-- 
View this message in context: http://www.nabble.com/-resultMap--related-objects-tp22845356p22845909.html
Sent from the iBATIS - User - Java mailing list archive at Nabble.com.


Re: [resultMap] related objects

Posted by Ingmar Lötzsch <il...@asci-systemhaus.de>.
> ...and there is a problem with resultMap:

You should explane the problem. What exceptions? What table structures?

> something like that doesn't work:
> 
> <resultMap id="addr-result" class="Address">
> <result property="addressId" column=".." />
> <result property="street" column=".." />
> </resultMap>
> 
> <resultMap id="comp-result" class="Company">
> <result property="id" column=".." />
> <result property="name" column=".." />
> <result property="address" resultMap="address-result" />
> </resultMap>

This example can't work, because

resultMap="address-result"

does not match

<resultMap id="addr-result"

Ingmar