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

why inverse must be setted in hibernate bidirectional association of one-to-many and many-to-many相关推荐

  1. hibernate官方新手教程 (转载)

    hibernate官方新手教程第一部分 - 第一个Hibernate程序 首先我们将创建一个简单的控制台(console-based)Hibernate程序.我们使用内置数据库(in-memory d ...

  2. HIBERNATE - 符合Java习惯的关系数据库持久化 Hibernate参考文档

    Hibernate参考文档 3.1.2 目录 前言 1. 翻译说明 2. 版权声明 1.Hibernate入门 1.1.前言 1.2.第一部分-第一个Hibernate应用程序 1.2.1.第一个cl ...

  3. Hibernate中文参考文档(JFIS)

    HIBERNATE - 符合Java习惯的关系数据库持久化      下一页 HIBERNATE - 符合Java习惯的关系数据库持久化 Hibernate参考文档 3.0.4 目录 前言 1. 翻译 ...

  4. Hibernate框架--学习笔记(中):一对多配置、多对多配置

    一.一对多: 一个客户可以有多个联系人,一个联系人只能属于一个客户. 1.一对多映射配置: (1)创建实体类 //客户类:一个客户可以有多个联系人,一个联系人只能属于一个客户 public class ...

  5. inverse和Cascade详解

    Hibernate中的inverse在表关系映射中经常应用, inverse的值有两种,"true"和"false".inverse="false&q ...

  6. Hibernate(三) - hibernate 表操作-多对多配置

    Hibernate 的一对多关联映射 之前在学习 Hibernate 的时候,其实都是单表的操作.在实际的开发当中,比如做一个商城,就需要好多张数据库表,表与表之间是有关系的.之前些做一些关联查询或者 ...

  7. Hibernate中的级联一对多关系

    两个pojo,Baby 和Baby File 对应数据库中表baby和babyfile.两个表有外键关联,且babyfile的外键不能为空 配置了个单向一对多,级联关系为save-update /** ...

  8. Hibernate 常见异常

    Hibernate 常见异常 net.sf.hibernate.MappingException        当出现net.sf.hibernate.MappingException: Error ...

  9. 表删除时 Cannot delete or update a parent row: a foreign key constraint fails 异常处理

    有两张表,结构如下: t_item: t_bid: id int id int name varchar name varchar item_id int 其中表t_item的主键id是表t_bid的 ...

最新文章

  1. 你住的城市7.5亿年前长啥样?这张互动地图能让你看到
  2. java容易混淆的知识点
  3. 项目管理六大制约因素_知道了这7点,软件项目管理会变得更简单!
  4. 使用C#把Tensorflow训练的.pb文件用在生产环境
  5. linux 命令 ppt,Linux基本命令()讲解.ppt
  6. 《Linux杂记:一》
  7. 使用jdbc执行SQL实现登录查询2-避免SQL注入版
  8. Spring Boot 之spring.factories
  9. [Re] ABC: Always Be Coding
  10. 弹出菜单快捷键的使用方法
  11. linux之git入门命令
  12. Chrome 谷歌浏览器中文默认小于12px设置无效解决办法
  13. 2019寒假作业一:PTA7-1 打印沙漏
  14. Python(十七):python的编程规范、PEP8
  15. android系统签名一样不,解决Android应用签名和系统不一致的问题
  16. Subclipse更新地址
  17. 什么是冒烟测试?什么是回归测试?
  18. 通用能力-《即兴演讲》-樊登读书总结
  19. 你真的理解了MVC, MVP, MVVM吗?
  20. WEditor没有自动打开浏览器

热门文章

  1. Lombok减少代码冗余量
  2. fork函数创建子进程僵尸进程孤儿进程详讲
  3. 数字图像处理的招聘公司
  4. ubuntu16.04安装cajviewer(亲测有效)
  5. Python 实现验证身份证号真假以及查询归属地、出生年月等信息
  6. 写简历有疑惑?戳进来找答案!
  7. 条形码录入测试软件,ERP软件测试中条形码测试
  8. (七牛云系列)七牛云指定文件上传路径(各种语言通用)
  9. BSV网络完成历史性的“创世纪”硬分叉升级
  10. solidity运算符03