why inverse must be setted in hibernate bidirectional association of one-to-many and many-to-many

原创 2006年06月22日 10:35:00

   If you’ve used CMP 2.0/2.1, you’re familiar with the concept of a managed association (or managed relationship). CMP associations are called container-managed relationships (CMRs) for a reason. Associations in CMP are inherently bidirectional:A change made to one side of an association is instantly reflected at the other side. For example, if we call bid.setItem(item), the container automatically calls item.getBids().add(item).

Transparent POJO-oriented persistence implementations such as Hibernate do
not implement managed associations. Contrary to CMR, Hibernate associations are
all inherently unidirectional. As far as Hibernate is concerned, the association from Bid to Item is a different association than the association from Item to Bid.
文中Item和Bid是one-to-many关系

这段话告诉我们hibernate不会向CMP那样由容器管理对象间的关系(assotiation),hibernate中的对象先天是unidirectional(单向的);hibernate中,对象有两种关系:unidirection、bidirection,对象的双向关系通过inverse指明,inverse属性告诉Hibernate “inverse端”维护这种关系:one-to-many的many端或many-to-many的link table

Making the association bidirectional


So far so good. But we also need to be able to easily fetch all the bids for a particular item. We need a bidirectional association here, so we have to add scaffolding code to the Item class:
public class Item {...
private Set bids = new HashSet();
public void setBids(Set bids) {
this.bids = bids;
}
public Set getBids() {
return bids;}
public void addBid(Bid bid) {
bid.setItem(this);
bids.add(bid);
}
...
}
You can think of the code in addBid() (a convenience method) as implementing
a managed association in the object model.
A basic mapping for this one-to-many association would look like this:
<class
name="Item"
table="ITEM">
...
<set name="bids">
<key column="ITEM_ID"/>
<one-to-many class="Bid"/>
</set>
</class>

The column mapping defined by the <key> element is a foreign key column of the
associated BID table. Notice that we specify the same foreign key column in this collection mapping that we specified in the mapping for the many-to-one association.
The table structure for this association mapping is shown in figure 3.11.
Now we have two different unidirectional associations mapped to the same foreign key, which poses a problem. At runtime, there are two different in-memory representations of the same foreign key value: the item property of Bid and an element of the bids collection held by an Item. Suppose our application modifies the association by, for example, adding a bid to an item in this fragment of the addBid() method:
bid.setItem(item);//从而bid的ItemId字段有值,为item.id;否则,hibernate更新为null
bids.add(bid);
This code is fine, but in this situation, Hibernate detects two different changes to the in-memory persistent instances. From the point of view of the database, just one value must be updated to reflect these changes: the ITEM_ID column of the BID table. Hibernate doesn’t transparently detect the fact that the two changes refer to the same database column, since at this point we’ve done nothing to indicate that this is a bidirectional association.

We need one more thing in our association mapping to tell Hibernate to treat
this as a bidirectional association: The inverse attribute tells Hibernate that the collection is a mirror image of the many-to-one association on the other side:
<class
name="Item"
table="ITEM">
...
<set
name="bids"
inverse="true">
<key column="ITEM_ID"/>
 <one-to-many class="Bid"/>
</set>
</class>

事实上,在one-to-many关系中,不设置invers="true",hibernate也会产生预期结果,只是会多执行几条无用的update语句(这些update看起来有点莫名其妙),一般情况下不会影响执行结果;

about many-to-many
When you map a bidirectional many-to-many association, you must declare one
end of the association using inverse="true" to define which side’s state is used to update the link table.same as  one-to-many

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

hibernate 的many to one 的问题IllegalArgumentException occurred while calling setter of hibernatePOJO

package com.oyth.model; public class Book { private int BookID; private String BookName; pri...

Hibernate-one-to-many

  • 2011-12-15 08:01
  • 98KB
  • 下载

【Rails】inverse_of在has_many和belongs_to中的用法

最近使用Rails时,遇到了通过关联关系来多次访问同一条record,虽然得到的内容一样,但是每次都会创建不同对象的情况。通过查询Rails官方文档,找到了对关联关系的一种设置inverse_of,可...

hibernate annotation one-to-many

hibernate一对多,多的一方建立外键,例: CREATE TABLE `person` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `pe...

Hibernate的配置文件的映射many-to-one错误

先看一下错误 虽然,这么多错误挺吓人的,还有SessionFactory为空,但是其实就一个错误文件是这样的Customer.javapackage ch06.pojo;import jav...

Hibernate one-to-many example

1. “One-to-many” example This is a one-to-many relationship table design, a STOCK table has many oc...

hibernate:many to one

这里就拿练手的图书馆系统来说吧… 这个案例用到其中的表 tb_book 图书信息表 tb_bookcase 书架信息表 tb_booktype 图书类型表 ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)