Ibatis继承和一对多

发布于 2024-07-21 16:26:16 字数 1856 浏览 7 评论 0原文

美好的一天

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

我的模型如下:

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);
  }
}

我当前的 XML 映射:

<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>

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
)

继承正在工作,但它只在 A(以太 BA 或 BB)事件中返回一个,尽管 b 是一个列表并且 b(BA,BB)有多行 请你帮助我好吗?

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

我正在使用 ibatis 2.3.4.726 for java

Good day

I have a complex model (ddd) which i want to map using 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?

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

I am using ibatis 2.3.4.726 for java

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花间憩 2024-07-28 16:26:16

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

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

它成功了。

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

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

It worked.

寄风 2024-07-28 16:26:16

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

当你手动运行SQL查询时,你到底得到了什么,你得到多个结果行吗? 也许您确实只得到一个结果行,它将映射到包含 List 的单个 A。 其中包含 BA 或 BB。

您能否向我们展示您用于通过此查询调用 iBatis 的 Java 代码? 如果你说 queryForObject() 你只会得到一个顶级 A,而 queryForList() 会给你一个 List。 这听起来不像你的问题,但也许这是一个部分答案。

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

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

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.

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.

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文