@OneToOne单向和双向 [英] @OneToOne unidirectional and bidirectional

查看:246
本文介绍了@OneToOne单向和双向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个例子,第一个是@OneToOne单向映射,第二个是双向.在单向映射中,拥有方表必须包含一个连接列,该连接列引用了另一个表的ID.那么在双向中,它们两个都必须包含彼此的外键列.但是,在使用自动生成策略生成数据库模式之后,两个示例对数据库模式具有相同的作用.单向映射是正常的,但是双向示例仅包含一个外键列,但必须包含彼此的外键!

I have two examples that first is @OneToOne unidirectional mapping and second bidirectional. In unidirectional mapping, the owning-side table must contain a join column that refers to the other table's id; then in bidirectional both of them must contain a foreign key column for each other. But after generating the database schema with autogenerate strategy, two examples have the same effect on database schema. Unidirectional mapping is normal but the bidirectional example only contains one foreign key column, but it must be involve each other's foreign key!

单向映射

@Entity
public class Customer43 {

@Id
@GeneratedValue
private Long id;
private String firstName;
private String lastName;
private String email;
private String phoneNumber;
@OneToOne
@JoinColumn(name = "address_fk")
private Address43 address;

// Getters, Setters and Constructors.
}

@Entity
public class Address43 {

@Id
@GeneratedValue
private Long id;
private String street1;
private String street2;
private String city;
private String state;
private String zipcode;
private String country;

// Getters, Setters and Constructors.
}

双向映射

@Entity
public class Customer44 {

@Id
@GeneratedValue
private Long id;
private String firstName;
private String lastName;
private String email;
private String phoneNumber;
@OneToOne
@JoinColumn(name = "address_fk")
private Address43 address;

// Getters, Setters and Constructors.
}

@Entity
public class Address44 {

@Id
@GeneratedValue
private Long id;
private String street1;
private String street2;
private String city;
private String state;
private String zipcode;
private String country;
@OneToOne(mappedBy = "address")
private Customer44 customer;

// Getters, Setters and Constructors.
}

为什么数据库模式输出相同,为什么双向映射就像单向一样?

Why is the database schema output the same and why does bidirectional mapping act like unidirectional?

推荐答案

因为您不了解双向OneToOne的映射方式.您不需要两个外键.单个表足以在两个方向上执行两个表之间的联接:

Because you didn't understand how a bidirectional OneToOne is mapped. You don't need two foreign keys. A single one is sufficient to perform a join between both tables, in both directions:

select a.* from address a inner join customer c on c.addressId = a.id;
select c.* from customer c inner join address a on c.addressId = a.id;

关联是单向或双向的事实并不会改变将表链接在一起的方式.它只是更改映射,并允许在两个方向上浏览关联.

The fact that the association is unidirectional or bidirectional doesn't change how the tables are linked together. It just changes the mapping, and allows navigating through the association in both directions.

在双向关联中,您总是拥有一个拥有侧(它表示关联是如何映射的,使用哪个连接列),以及一个相反的一侧,您只需说:我是关联的另一侧,即在目标实体中由该字段(mappedBy属性值中的字段)映射.

In a bidirectional association, you always have an owning side (which tells how the association is mapped, using which join column), and an inverse side, where you just say: I'm the other side of the association that is mapped by this field (the field in the mappedBy attribute value) in the target entity.

这篇关于@OneToOne单向和双向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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