Understanding bidirectional relationship from Hibernate documentation
来源:互联网 发布:怎么没人去淘宝打假 编辑:程序博客网 时间:2024/06/05 18:01
I am going through the Hibernate documentation for bidirectional relationship, in the doc it says that:
Example 7.21. Bidirectional one to many with many to one side as association owner
@Entitypublic class Troop { @OneToMany(mappedBy="troop") public Set<Soldier> getSoldiers() { ...}
@Entitypublic class Soldier { @ManyToOne @JoinColumn(name="troop_fk") public Troop getTroop() { ...}
Troop
has a bidirectional one to many relationship with Soldier
through the troop
property. You don’t have to (must not) define any physical mapping in the mappedB
y side.
To map a bidirectional one to many, with the one-to-many side as the owning side, you have to remove the mappedBy
element and set the many to one @JoinColumn
as insertable
and updatable
to false. This solution is not optimized and will produce additional UPDATE statements.
Example 7.22. Bidirectional association with one to many side as owner
@Entitypublic class Troop { @OneToMany @JoinColumn(name="troop_fk") //we need to duplicate the physical information public Set<Soldier> getSoldiers() { ...}
@Entitypublic class Soldier { @ManyToOne @JoinColumn(name="troop_fk", insertable=false, updatable=false) public Troop getTroop() { ...}
I am finding difficulty in understanding this as I am new to Hibernate.
1) What it means when the doc says: You don’t have to (must not) define any physical mapping in the mappedBy
side.
2) @JoinColumn
in 7.22 has same value (troop_fk
) for name attribute. Can we specify different values? What is the advantage & disadvantages of setting insertable=false, updatable=false
here?
It’s a bidirectional association. So, if a soldier balongs to a troop, the troop contains the soldier. These are just the two ways of saying the same thing.
In Soldier, you tell how the association is represented in the database: using a join column named troop_fk:
@JoinColumn(name="troop_fk")public Troop getTroop() {
So, repeating that same information on the other side of this bidirectional association is redundant. You must not do it. By saying
@OneToMany(mappedBy="troop")public Set<Soldier> getSoldiers() {
you’re telling Hibernate that getSoldiers()
is the inverse side of a bidirectional association, and that the way this association is mapped can be found on the Soldier.troop
property.
Regarding your second question. Once again, the goal is to define a single, but bidirectional association. You don’t need two different foreign keys to map a single association. So specifying a different name for the join column would make no sense: it would create a different, unidirectional association.
This way of doing is an ugly hack that, AFAIK, is not supported by the JPA spec. The JPA spec mandates that the owner side of a bidirectional OneToMany association is the many side. In fact it creates two unidirectional associations mapped the same way, and tells Hibernate (using insertable = false and updatable = false) to ignore one of them when saving the entity. It will populate soldier.troop when reading a soldier from the database, but whatever you put into soldier.troop will be ignored when saving the soldier. You should avoid this way of doing, IMHO.
- Understanding bidirectional relationship from Hibernate documentation
- Understanding Bidirectional (BIDI) Text in Unicode
- Mapping a bidirectional list with Hibernate
- Understanding Workflow(From SalesForce)
- Hibernate Reference Documentation
- ICE Manual(Documentation for Ice 3.5)---Connection Management(Bidirectional Connections)
- Hibernate: Understanding Lazy Fetching
- 8.1.2 Hibernate:一对一双向关联(bidirectional)
- DO Separate Process Documentation from Procedures
- Enhancement--User exit documentation from SAP
- Enhancement--User exit documentation from SAP
- Working with Blocks - from Apple Documentation
- Mac--NSIndexSet学习笔记from Apple Documentation
- Understanding mappedBy annotation in Hibernate
- Hibernate: Understanding of Hibernate Fetch Strategies
- From Flash to Flex: Understanding Flex Events
- Understanding Machine Learning: From Theory to Algorithms
- Documentation
- django的session操作
- 控件WebView的使用
- 沙盒文件管理
- 控件测试
- android 百度地图 点击选中到最上层 置顶
- Understanding bidirectional relationship from Hibernate documentation
- 宣告“主权”
- 验证码-技术选型
- Android studio快捷键设置为Eclipse风格
- Linux2.6.32移植到MINI2440(3)支持yaffs2文件系统
- 【2014-2015】是什么让我遇见了你们
- 使用 HTML5 input 类型提升移动端输入体验
- 申请了百度云计算空间BAE
- [持续更新]CSS3学习笔记(二)渐变&圆角&阴影&变换&动画