【发布时间】:2010-11-06 04:40:34
【问题描述】:
我正在尝试为我基本上无法控制的数据库模式构建 Hibernate 层。简单来说,有两张表。
表parent有两个重要的列:
-
parent_id,整数,主键,自增 -
parent_code,字符串,唯一密钥,由某处的黑匣子生成(为了理智,假设这是一个 UUID) - 加上一堆数据列
表child有两个重要的列:
-
child_parent_id,整数,主键,自增 -
child_parent_code,字符串,外键指向父级的parent_code值 - 加上一堆数据列
我希望能够调用 Parent.getChilds() 并获得 Child 对象的 Collection。但是设置 Hibernate 映射文件似乎是不可能的。它对以下映射的作用是使用 parent_id 值(而不是 parent_code)搜索 child 表。
在Parent.hbm.xml:
<set name="childs" inverse="true" lazy="true" table="child" fetch="select">
<key>
<column name="child_parent_code" not-null="true" />
</key>
<one-to-many class="foo.bar.Child" />
</set>
在Child.hbm.xml:
<many-to-one name="parent" class="foo.bar.Parent" fetch="select">
<column name="child_parent_code" not-null="true" />
</many-to-one>
我花了一个小时仔细研究我的Java Persistence with Hibernate,但我不知道如何做我想做的事。有可能吗?
【问题讨论】:
标签: java database hibernate schema