我正在使用hibernate映射,当我实现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
+ "]";
}
}
因此,没有从反面到拥有方的参考,具有双向映射的用途是什么?
答案 0 :(得分:0)
主要区别在于双向将为您提供双向导航访问。 这意味着你可以在不查询的情况下获得关系的另一面。
它还允许级联选项(在@OneToMany
中关于性能的小心)。
数据库在单向或双向之间没有区别。