File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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 Head First Android this week in the Android forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "iBatis and Bidirectional Association" Watch "iBatis and Bidirectional Association" New topic
Author

iBatis and Bidirectional Association

Ro Ah
Greenhorn

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>

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

And a select

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

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

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
    
    1

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
Greenhorn

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?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: iBatis and Bidirectional Association
 
It's not a secret anymore!