拥有一方vs非拥有一方在休眠和它的用法参考元素映射? [英] Owning Side vs Non-Owning Side In hibernate and its usage in reference of mapped by element?

查看:150
本文介绍了拥有一方vs非拥有一方在休眠和它的用法参考元素映射?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在研究hibernate时,我遇到了拥有方和非拥有方的条款。例如: - 这里是
关于使用mappedby元素的声明就其在一对一映射中的用法而言


如果关系是双向的,非拥有方必须使用
mappedBy OneToOne 注释
的元素指定拥有方的关系字段或属性。 p>

但是我没有得到真正的拥有方和非拥有方方是什么?

解决方案

这里是我对大学有很多学生(一对多关系)的简单例子的理解。 b

 对应于
College(Value Object)----------------->学院(数据库表)

对应
学生(数值对象)----------------->学生(数据库表的列是
College表的外键
所以这是拥有方和College是
非拥有方)

就Object表示而言,Student对象拥有自己的一面,因为它将引用指向college列。所以学生拥有side和College在非拥有方。

在元素映射关系中,hibernate中所有者方和非所有方的用法



非持有方对象

  @Entity 
public class College {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int collegeId;
@OneToMany(mappedBy =college)//这里非拥有方使用elment映射来指定
//拥有方的关系字段
private List< Student>学生们;
}


拥有Side对象

@实体
公共类学生{
@ManyToOne
私立学院学院;
}

总而言之,拥有的一方是对另一方的引用的实体。在数据库方面,
翻译成表格实体,其列表是其他表格中列的外键,如
案例,学院有许多学生。



注意:拥有方表必须包含引用另一个表的id的连接列。这意味着拥有边的实体应该包含@joincolumn注释,否则它会将该名称视为主键列其他表的名称。请参阅 @ OneToOne单向和双向


I came across the terms i.e owning side and non-owning side while studying hibernate.For example :- here is the statement regarding usage of mappedby element in terms of its usage in one to one mapping

If the relationship is bidirectional, the non-owning side must use the mappedBy element of the OneToOne annotation to specify the relationship field or property of the owning side.

But i did not get what actually owning side and non-owning side is?

解决方案

Here is my understanding with simple example where College has many students(One to many relationship)

                       corresponds to       
College(Value Object) -----------------> College (Database Table)

                       corresponds to       
Student(Value Object) -----------------> Student (Database Table having column which is
                                                  foreign key to College table . 
                                                  So This is owning side and College  is
                                                  Non-owning side )

In terms of Object representation, Student object is owning side becoz it will be having reference pointing to college column.So Student is owning side and College in non-owning side.

Usage of Owner side and Non-Owning side in hibernate in relation of mapped by element

Non-Owning Side Object

@Entity
public class College {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int collegeId;
    @OneToMany(mappedBy="college") // here non-owning side using mapped by elment to specify
                                   // the relationship field of owning side
    private List<Student> students;
    }


Owning Side Object

@Entity
public class Student {
@ManyToOne
    private College college;
}

In conclusion the owning side is the entity that has the reference to the other. In terms of DB, it translate to table entity having column which is foreign key to column in other table like in case of College has many students.

Note:- owning-side table must contain a join column that refers to the other table's id.It means owning side entity should contain @joincolumn annotation otherwise it will consider the name as primary key column name of other table. See @OneToOne unidirectional and bidirectional

这篇关于拥有一方vs非拥有一方在休眠和它的用法参考元素映射?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆