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 Kenny Pearce <ke...@hxti.com> on 2007/10/24 15:36:49 UTC
complicated object model in iBATIS 2.3
Hi,
I'm wondering if it is possible in iBATIS to get adatabase structure
and object model like the following to work together:
DATABASE:
===========
table foos:
foo_id NUMBER PRIMARY KEY,
foo VARCHAR,
bar NUMBER FOREIGN KEY
table bars:
bar_id NUMBER PRIMARY KEY,
bar VARCHAR
table bazs:
baz_id NUMBER PRIMARY KEY,
baz VARCHAR
table bar_baz_map:
map_id NUMBER PRIMARY KEY,
bar NUMBER FOREIGN KEY,
baz NUMBER FOREIGN KEY
===========
JAVA:
===========
public class FooContainer{
private Foo foo;
private Content content;
//getters and setters omitted
}
public class Foo{
private String foo;
//getters and setters omitted
}
public abstract class Content{}
public class Bar extends Content{
private String bar;
private List<Baz> bazList;
//getters and setters omitted
}
public class Baz{
private String baz;
//getters and setters omitted
}
===========
I want iBATIS to return a List<FooContainer> with the content of each
FooContainer being a Bar, and each Bar having a list of the Bazs mapped
to it. Doing it in one query would be nice.
This is what I've got so far: In order to get iBATIS to deal properly
with Bar, I have to link it to a ResultMap that specifies the class (I
can't use, for instance, content.bar to set the value of the string bar
in the class Bar). I tried adding the primary keys into Java properties
(for the purposes of the example, suppose each object has an int
property ID), and using groupBy="content.ID" in the outer ResultMap (the
one of type FooContainer), but that did nothing (I still got a Cartesian
join - each Bar contained a list of one Baz, and there was a different
FooContainer, with the same Foo, for each combination). I tried using
groupBy="id" in the inner ResultMap (the one of type Bar), and that gave
me the correct objects but, strangely enough, for every additional
Bar-Baz mapping, there was an extra FooContainer that had the same Foo
in it, but null content.
Is it possible to do what I'm trying to do? Right now I'm getting
correct results by doing the last thing and clearing out the nulls in my
Java code, but this is clearly not optimal.
Thanks,
--
Kenny Pearce
Hx Technologies