Spring Data一对一和一对多例子
时间: 2024-05-25 08:19:35 浏览: 14
Sure, I can provide you with examples of one-to-one and one-to-many relationships in Spring Data.
For a one-to-one relationship, imagine you have an Employee class and an Address class. An employee can only have one address, and an address can only belong to one employee. You can define the relationship in the Employee class as follows:
public class Employee {
@OneToOne
private Address address;
// other fields and methods
}
And in the Address class:
public class Address {
@OneToOne(mappedBy = "address")
private Employee employee;
// other fields and methods
}
For a one-to-many relationship, imagine you have a Manager class and an Employee class. A manager can have multiple employees reporting to them, but an employee can only have one manager. You can define the relationship in the Manager class as follows:
public class Manager {
@OneToMany(mappedBy = "manager")
private List<Employee> employees;
// other fields and methods
}
And in the Employee class:
public class Employee {
@ManyToOne
private Manager manager;
// other fields and methods
}
I hope this answers your question!
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)