Meaningless Drivel is fun!
The moose likes Object Relational Mapping and the fly likes iBatis and Bidirectional Association Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "iBatis and Bidirectional Association" Watch "iBatis and Bidirectional Association" New topic

iBatis and Bidirectional Association

Ro Ah

Joined: Aug 14, 2007
Posts: 15
Hi All,
My problem is that I am trying to represent a bidirectional association relationship using “iBatis”. Suppose this problem:
Java Classes:
class A {String ida; List<B> bs;}
class B {String idb; A parentA;}

Database Structure
a( ida [PK])
b( idb [PK], ida [FK] )

And the relation between a-> b is one-to-many (A has 0..* B)

And iBatis Maps
<resultMap class="A" id="AResult" groupBy="ida">
<result property="ida" column="ida" />
<result property="bs" resultMap="BResult" />

<resultMap class="B" id="BResult">
<result property="idb" column="idb" />
<result property="parentA" resultMap="AResult" />

And a select

<select id="getA" resultMap="AResult">
A.ida as ida,
B.idb as idb,
A.ida = B.ida
and A.ida = #value#

Now, When I try to run “getA” select statement I get an exception:
at sun.nio.cs.UTF_8$Decoder.decodeArrayLoop(
at sun.nio.cs.UTF_8$Decoder.decodeLoop(
at java.nio.charset.CharsetDecoder.decode(
at java.lang.StringCoding$CharsetSD.decode(
at java.lang.StringCoding.decode(
at java.lang.String.<init>(

This will work if I removed “<result property="parentA" resultMap="AResult" /> “ this line from B map. But this will make the generated B.paretnA property with null value

So, what is the best way I can use to overcome this problem? And Sorry for the long mail 
Any help is appreciated

NOTE: I can’t use another tecnology
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968

I think the java.lang.StackOverflowError is from creating a circular relationship that becomes infinitely recursive. Flattening it out should solve the problem.

-Cameron McKenzie
Ro Ah

Joined: Aug 14, 2007
Posts: 15
But in that case more calls to the backend will be made, What is the most efficent way to break this?
I agree. Here's the link:
subject: iBatis and Bidirectional Association
It's not a secret anymore!