0

我试图了解如何为从各种表中检索各种字段的单个选择创建映射 XML。

请参阅下面的 SQL(为简单起见,我将检索每个表中的所有字段)

SELeCT a.*, b.*, c.*, d.*
from
    table1 a,
    table2 b,
    table3 c,
    table4 d
where
    a.id = b.id
    and a.id = c.id (+)
    and a.code = d.code (+)
    and a.id = 34

这是表定义。

我的主表是 table1,它与 table2 是一对一的关系。table1 在 table3 和 table4 中可能有也可能没有对应的记录。表 1、2 和 4 中的 id 列具有相同的值。(例如,如果 table1.id = 34 并且如果 table1 在 table3 和 4 中有记录,则 table3.id 和 table4.id = 34)

table1 (
    id int,
    code varchar2(255)
)

table2 (
    id int,
    some_column1 varchar2(255)
    some_column2 varchar2(255)
)

table3 (
    code varchar2(255),
    description varchar(512)
)

table3 (
    id int,
    url varchar(1024)
)

我一直在阅读 jboss.org 站点上的 HQL 入门和基本 o/R,但它并没有真正告诉我如何为上面的 SQL 创建一个休眠映射 XML。

谢谢你!:)

4

1 回答 1

0

为 Hibernate编写有效的XML 映射的最佳方法是阅读本教程

读者关注的几件事是:

  • XML 类映射
  • 关系类型

在您的示例中,我们有四个表。应该以某种方式连接。

所以第一步是为所有表创建一个 POJO 类,并为简单的属性创建 XML 映射。

模型应该看起来像

class A {
  int id; 
  String code;
  B b
  C c;
  D d;
} 

class B {
  int id;
}

class C {
  int id;
}

class D {
  String code;
}

映射

A和B之间的连接

<one-to-one name="b" class="B">

然后我们映射表 C (a.id = b.id(+))

<many-to-one name="c" class="C"/>

我们映射表 D (a.code = c.code(+))

<many-to-one name="d" class="D" column="CODE"/>

于 2012-12-10T01:10:01.973 回答