使用主键进行一对一映射
我有两个简单的表: Item 和 ItemDetail
表 Item 具有以下列:id、name
表 ItemDetail 具有以下列:itemId、color、size
如果没有 Item,则 ItemDetail 永远不存在。项目可以在没有任何 ItemDetail 的情况下存在。对于每个 Item 最多有一个 ItemDetail。
这种情况在hibernate中应该如何正确映射呢?
到目前为止,我只找到了以下解决方案:
<class name="Item" table="Item">
<id name="id" column="id">
<generator class="native"></generator>
</id>
<property name="name" />
<one-to-one name="itemDetail" class="ItemDetail" cascade="save-update"/>
</class>
<class name="ItemDetail">
<id name="itemId">
<generator class="foreign">
<param name="property">item</param>
</generator>
</id>
<one-to-one name="item" class="Item" constrained="true" />
<property name="color" />
<property name="size" />
</class>
该解决方案有效,但我认为它不是“干净”,因为在类 ItemDetail 中我需要同时拥有 itemId 和 item 属性。
private class Item {
private long id;
private String name;
private ItemDetail detail;
}
private class ItemDetail {
private long itemId;
private Item item;
private int color;
private int size;
}
我认为从 OOP 的角度来看,如果对象 ItemDetail 了解有关 Item 的任何信息,那么这是不正确的。这意味着它不应该具有 itemId 和 item 属性。即使我们同意 ItemDetail 知道 Item 是正确的,但它有两个对 Item 的引用仍然是不正确的 - 一个使用属性 itemId,另一个使用属性 itemDetail。然后很容易发生这两个属性不同步的情况。也就是说,itemId 引用了与属性项不同的 Item。为了避免这种情况,每次当我们决定将 Item 设置为 ItemDetail 时,我们都需要更新 itemId 和 item。
有没有其他解决方案没有这些缺点?
谢谢
I have two simple tables: Item and ItemDetail
Table Item has following columns: id, name
Table ItemDetail has following columns: itemId, color, size
An ItemDetail never exists without an Item. An Item can exist without any ItemDetail. For every Item there is at most one ItemDetail.
How should this situation be correctly mapped in hibernate?
So far I found only the following solution:
<class name="Item" table="Item">
<id name="id" column="id">
<generator class="native"></generator>
</id>
<property name="name" />
<one-to-one name="itemDetail" class="ItemDetail" cascade="save-update"/>
</class>
<class name="ItemDetail">
<id name="itemId">
<generator class="foreign">
<param name="property">item</param>
</generator>
</id>
<one-to-one name="item" class="Item" constrained="true" />
<property name="color" />
<property name="size" />
</class>
This solution works however I think it is not "clean" since in class ItemDetail I need to have both properties itemId and item.
private class Item {
private long id;
private String name;
private ItemDetail detail;
}
private class ItemDetail {
private long itemId;
private Item item;
private int color;
private int size;
}
I think that from OOP perspective it is incorrect if object ItemDetail knows anything about Item. This means that it should not have properties itemId and item. Even if we agreed that it is correct if ItemDetail knows about Item, it is still incorrect that it has two references to Item - one using property itemId and one using property itemDetail. It can then very easily happen that these two properties are not synchronized. That is itemId references to different Item than the property item. To avoid this we would need to update both itemId and item everytime when we decide to set an Item to an ItemDetail.
Is there any other solution which does not have these drawbacks?
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论