Question

I'm having some trouble with a mapping in MyBatis for Java and would appreciate some help. My class structure is as below:

//Getters/setters omitted for clarity

class Foo 
{
  int id;
  Bar bar;
}

class Bar {
  String x;
  int y;
}

My table looks like this - i.e. it is de-normalized from the class structure.

create table foo_bar (
  id int,
  x varchar,
  y int
);

My working insert statement is able to de-normalize using (bar.x, bar.y) parameters.

<insert id="insert" parameterType="foo">
  <![CDATA[
    insert into foo_bar
    (id, x, y) values
    (#{x}, #{bar.x}, #{bar.y})
  ]]>
</insert>

So, the problem:

When I execute my select, I would like the resultant object to be an instance of Foo that has a reference to Bar.

I don't think I can use a type handler since this works over a single column, and and association doesn't seem to make sense since 'Bar' is not an entity explicitly represented in the database via a foreign key relationship.

Can anyone show me the recommended way of doing this please?

Thanks!

Was it helpful?

Solution

Have you tried using resultMap definition in your ibatis sqlmap?

<resultMap id="foobar-result" class="Foo">
    <result property="id" column="id"/>
    <result property="bar.x" column="x"/>
    <result property="bar.y" column="y"/>
</resultMap>

and then in you sql just reference the resultMap:

<select id="getFooBar" resultMap="foobar-result">
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top