[英]Get owner of deposit (JPA repository)
我有问题,如何在存款集包含存款对象的情况下获取 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;
}
要根据存款对象获取 Person 对象,您还必须在存款对象中映射 Person <-> Deposit 关系。 假设 Person <-> 存款是一对多的,在表中,您将在存款表中拥有个人 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;
}
使用DepositRepository
通过 id 获取存款对象,然后执行deposit.getPerson()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.