单向 OneToOne 映射与双向 OneToOne 映射
Uni-directinal OneToOne mapping vs Bi-directinal OneToOne mapping
我正在使用休眠映射,当我实现 OneToOne 双向映射时,在反面没有创建列来引用拥有方。
这些是我用过的类,
@Entity
@Table(name = "Address121_2")
public class Address {
@Id
@GeneratedValue
private int addressId;
private String city;
@OneToOne(mappedBy = "address")
private Employee employee;
public int getAddressId() {
return addressId;
}
public void setAddressId(int addressId) {
this.addressId = addressId;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public Employee getEmployee() {
return employee;
}
public void setEmployee(Employee employee) {
this.employee = employee;
}
@Override
public String toString() {
return "Address [addressId=" + addressId + ", city=" + city + ", employee=" + employee + "]";
}
}
另一个,
@Entity
@Table(name="emp121_2")
public class Employee {
@Id
@GeneratedValue
private int employeeId;
private String name;
private String email;
@OneToOne
@JoinColumn(name="a_id")
private Address address;
public int getEmployeeId() {
return employeeId;
}
public void setEmployeeId(int employeeId) {
this.employeeId = employeeId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
@Override
public String toString() {
return "Employee [employeeId=" + employeeId + ", name=" + name + ", email=" + email + ", address=" + address
+ "]";
}
}
所以如果没有从反面到拥有面的引用,双向映射有什么用?
主要区别在于双向将为您提供两个方向的导航访问。
这意味着您无需查询即可获得关系的另一端。
它还允许级联选项(在 @OneToMany
关于性能方面要小心)。
单向或双向在数据库中没有区别。
我正在使用休眠映射,当我实现 OneToOne 双向映射时,在反面没有创建列来引用拥有方。
这些是我用过的类,
@Entity
@Table(name = "Address121_2")
public class Address {
@Id
@GeneratedValue
private int addressId;
private String city;
@OneToOne(mappedBy = "address")
private Employee employee;
public int getAddressId() {
return addressId;
}
public void setAddressId(int addressId) {
this.addressId = addressId;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public Employee getEmployee() {
return employee;
}
public void setEmployee(Employee employee) {
this.employee = employee;
}
@Override
public String toString() {
return "Address [addressId=" + addressId + ", city=" + city + ", employee=" + employee + "]";
}
}
另一个,
@Entity
@Table(name="emp121_2")
public class Employee {
@Id
@GeneratedValue
private int employeeId;
private String name;
private String email;
@OneToOne
@JoinColumn(name="a_id")
private Address address;
public int getEmployeeId() {
return employeeId;
}
public void setEmployeeId(int employeeId) {
this.employeeId = employeeId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
@Override
public String toString() {
return "Employee [employeeId=" + employeeId + ", name=" + name + ", email=" + email + ", address=" + address
+ "]";
}
}
所以如果没有从反面到拥有面的引用,双向映射有什么用?
主要区别在于双向将为您提供两个方向的导航访问。 这意味着您无需查询即可获得关系的另一端。
它还允许级联选项(在 @OneToMany
关于性能方面要小心)。
单向或双向在数据库中没有区别。