Ibatis继承和一对多

时间:2022-08-15 21:40:41

Good day

I have a complex model (ddd) which i want to map using ibatis.

我有一个复杂的模型(ddd),我想用ibatis映射。

My model is as follows:

我的模型如下:

class A {
 int id;
 String title;
 List <B> b;
}

abstract class B {
 int id;
 String title;
 List <C> f;
 int type;
}

class BA extends B {

 BA() {
  setType(1);
 }
}

class BB extends B {

  BB {
   setType(2);
  }
}

My current XML Mapping:

<sqlMap namespace="ABC">

  <resultMap id="aResult" class="A" groupBy="a_id">
    <result property="id" column=""a_id" />
    <result property="title" column="a_title" />
    <result property="b" resultMap="ABC.bResult" />
</resultMap> 

<resultMap id="bResult" class="java.util.HashMap">       
   <discriminator javaType="java.lang.Integer" column="b_type">
      <subMap value="1" resultMap="baResult" />
      <subMap value="2" resultMap="bbResult" />
   </discriminator>
</resultMap>

<resultMap id="baResult" class="BA">
  <result property="id" column="b_id" />
  <result property="title" column="b_title" />
</resultMap>

<resultMap id="bbResult" class="BB">
  <result property="id" column="b_id" />
  <result property="title" column="b_title" />
</resultMap>

<select id="aselect" resultMap="aResult">
select a.id as 'a_id', a.title as 'a_title', b.id as 'b_id', b.title as 'b_title', b.type as 'b_type'
from aa a left join bb b on a.id = b.aid
</select>

Tables

aa (
id int not null primary key,
title varchar(50) not null
)

bb (
id int not null primary key,
aid int not null,
title varchar(50) not null
type int not null
)

The inheritance is working but it only returns one in A (ether BA or BB) event though b is a list and there is multiple rows for b (BA, BB) Could you please help me?

继承是有效的,但它只返回一个A(乙醚BA或BB)事件,虽然b是一个列表,并且有多行b(BA,BB)你能帮帮我吗?

The reason for using the BA & BB classes is that those contains the seperate businesss logic (as per DDD).

使用BA和BB类的原因是它们包含单独的业务逻辑(根据DDD)。

I am using ibatis 2.3.4.726 for java

我正在使用ibatis 2.3.4.726 for java

2 个解决方案

#1


I think i found the problem, the mapping is wrong. When it tried this:

我想我发现了问题,映射是错误的。当它尝试这个:

<resultMap id="aResult" class="A" groupBy="id">

It worked.

#2


Johan, I don't think there's enough information to go on.

约翰,我认为没有足够的信息可以继续下去。

What exactly do you get when you run the SQL query manually, do you get multiple result rows? Maybe you really are getting just one result row, which would map to a single A containing a List<B> with either a BA or a BB in it.

当您手动运行SQL查询时,您到底获得了多少结果行?也许你真的只得到一个结果行,它会映射到一个包含List 的A,其中包含BA或BB。

Can you show us the Java code you use to invoke iBatis with this query? If you say queryForObject() you'll get just one top level A, while queryForList() will give you a List<A>. This doesn't sound like your problem, but perhaps it's a partial answer.

你能告诉我们你用这个查询调用iBatis的Java代码吗?如果你说queryForObject()你只会得到一个*A,而queryForList()会给你一个List 。这听起来不像你的问题,但也许这是一个部分答案。

There are errors in your SQL (missing comma) and your SQL Map (doubled double quotes), but I assume those are typing mistakes.

您的SQL(缺少逗号)和SQL Map(双引号加倍)中存在错误,但我认为这些错误是输入错误。

Your Java result objects I believe need to be JavaBeans, and therefore must have get/set methods, which you don't show above. But if that were the problem, I'd expect you to get no data back.

我认为你的Java结果对象需要是JavaBeans,因此必须有get / set方法,这些方法在上面没有显示。但如果这是问题,我希望你不会得到任何数据。

#1


I think i found the problem, the mapping is wrong. When it tried this:

我想我发现了问题,映射是错误的。当它尝试这个:

<resultMap id="aResult" class="A" groupBy="id">

It worked.

#2


Johan, I don't think there's enough information to go on.

约翰,我认为没有足够的信息可以继续下去。

What exactly do you get when you run the SQL query manually, do you get multiple result rows? Maybe you really are getting just one result row, which would map to a single A containing a List<B> with either a BA or a BB in it.

当您手动运行SQL查询时,您到底获得了多少结果行?也许你真的只得到一个结果行,它会映射到一个包含List 的A,其中包含BA或BB。

Can you show us the Java code you use to invoke iBatis with this query? If you say queryForObject() you'll get just one top level A, while queryForList() will give you a List<A>. This doesn't sound like your problem, but perhaps it's a partial answer.

你能告诉我们你用这个查询调用iBatis的Java代码吗?如果你说queryForObject()你只会得到一个*A,而queryForList()会给你一个List 。这听起来不像你的问题,但也许这是一个部分答案。

There are errors in your SQL (missing comma) and your SQL Map (doubled double quotes), but I assume those are typing mistakes.

您的SQL(缺少逗号)和SQL Map(双引号加倍)中存在错误,但我认为这些错误是输入错误。

Your Java result objects I believe need to be JavaBeans, and therefore must have get/set methods, which you don't show above. But if that were the problem, I'd expect you to get no data back.

我认为你的Java结果对象需要是JavaBeans,因此必须有get / set方法,这些方法在上面没有显示。但如果这是问题,我希望你不会得到任何数据。