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 one-to-many关联查询<set>中使用filter

我们将放在了Student的实体映射文件中,然后在College的实体映射文件中引用过滤器。经过我的验证,这样也是可以的。由此可以看出:是一个全局效果,无论放在哪儿个实体映射文件,效果都是一样的。有一...
  • aitangyong
  • aitangyong
  • 2016年01月10日 17:46
  • 2454

重新组织数据之八 :Change Bidirectional Association to Unidirectional(将双向关联改为单向)

两个鄉之间有双向关联,但其中一个class如今不再需要另一个class的特性。 去除不必要的关联(association)。 动机(Motivation) 双向关联(bid...
  • yuwei629
  • yuwei629
  • 2013年05月07日 08:53
  • 542

使用注解的Hibernate one-to-many映射

One to many映射关系指的是两个实体间一个实体可以和多个实体有关联关系,但是多的这一端只能和一的这一端的一个实例有关系。它是一个1 到 n的关系。例如在任何的公司员工可以注册多个银行账户,一个...
  • maoyeqiu
  • maoyeqiu
  • 2015年12月24日 23:32
  • 4365

hibernate 单向one-to-many association

 单向one-to-many association的insert/update1)only insert a new child    测试代码,          items 上篇有介绍和代码  ...
  • liuy2517
  • liuy2517
  • 2007年11月27日 04:16
  • 213

8.8 change bidirectional association to unidirectional (将双向关联改为单向关联)

两个类之间有双向关联,但其中一个类如今不再需要另一个类的特性。 动机: 双向关联很有用,但你必须为它付出代价,那就是维护双向连接。 大量的双向连接也很容易造成“僵尸对象”。 双向关联也迫使两个...
  • ptn3900
  • ptn3900
  • 2011年10月19日 10:59
  • 1077

hibernate常见错误汇总

1. 插入失败,某一字段没有默认值:insert fail;filed depid doesn't have a default value      这个错误往往是你丈二和尚—摸不着头脑,...
  • jiangtaoking
  • jiangtaoking
  • 2014年05月05日 21:03
  • 604

Hibernate的many-to-many双向关联需要注意的要求及简单示例

 在做hbiernate的many-to-many的双向关联时,需要了解的几点: 一方要设置inverse="true",另一方要设置inverse="false"。inverse="false"的一...
  • mini_snow
  • mini_snow
  • 2009年07月20日 15:14
  • 11847

Change Bidirectional Association to Unidirectional(将双向关联改为单向)

两个classes之间有双向关联,但其中一个class如今不再需要另一个class的特性.去除不必要的关联(association). 动机双向关联(bidirectional association...
  • bendan999999999
  • bendan999999999
  • 2009年01月24日 09:03
  • 429

many-to-one/one-to-many/many-to-many关系映射 (Hibernate)

many-to-one/one-to-many/many-to-many关系映射 本文的内容: 介绍NH如何处理对象间many-to-one,one-to-many和many-to-many的...
  • Henry_YQH
  • Henry_YQH
  • 2013年02月24日 22:31
  • 484

Hibernate one-to-many && many-to-many 的关系映射

one-to-many 关系映射 实体类:/** * 订单的实体: * */ public class Order { private Integer oid; privat...
  • eaphyy
  • eaphyy
  • 2017年05月02日 17:40
  • 145
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:why inverse must be setted in hibernate bidirectional association of one-to-many and many-to-many
举报原因:
原因补充:

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