java - 获取存款的所有者(JPA 存储库)
问题描述
我有问题,如何获取存款集包含存款对象的 Person 对象(下面的代码)。我必须为我的存储库创建函数,该函数返回单个 Person 类对象。
我的人类:
public class Person {
@GeneratedValue(strategy=GenerationType.AUTO)
@Id
protected int id;
protected String firstName;
protected String lastName;
protected String pesel;
@OneToMany
protected Set<Deposit> deposits;
}
还有我的存款课程:
public class Deposit {
@GeneratedValue(strategy=GenerationType.AUTO)
@Id
protected long id;
protected String depositNumber;
@OneToMany
protected Set<Credit> credits;
@OneToMany
protected Set<Transfer> transfers;
}
解决方案
要基于 deposit 对象获取 Person 对象,您还必须在 Deposit 对象中映射 Person <-> Deposit 关系。假设人 <-> 存款是一对多的,在表格中,您将在存款表中拥有人员 ID 的 FK。这就是您的存款课程中缺少的内容。更改存款类,如下所示
public class Deposit {
@GeneratedValue(strategy=GenerationType.AUTO)
@Id
protected long id;
protected String depositNumber;
@OneToMany
protected Set<Credit> credits;
@OneToMany
protected Set<Transfer> transfers;
@ManyToOne
private Person person;
}
使用 id 获取存款对象DepositRepository
,然后执行deposit.getPerson()
推荐阅读
- tensorflow-datasets - 当未指定steps_per_epochs时,不需要tensorflow数据集中的repeat()函数
- excel - 基于两个复选框值进行过滤
- sql - 通过更改元素进行 T-SQL 分组
- google-analytics - 跟踪登录用户与访客用户
- java - 如何以表格格式打印我的数组列表值
- excel - 不同用户的日期排序宏问题
- python - 如何解决 IndexError: list index out of range in a list of dict?
- r - 检查电子邮件列中的名称是否重复
- azure-batch - Azure Batch 作业发布任务未运行
- python - 如何在python中的3维数组中进行连接组件标记?