java - 从 JPA 子实体中获取价值
问题描述
如何使用 Spring-data-jpa 从 childofchild 实体中检索值?
例如。Person Entity ->Account Entity ->Debit_Account Entity
借记帐户实体包含我要检索的值 dbtAmount。
我是 spring 和 spring-data-jpa 存储库中的新手,使用 find 方法我无法从 childofchild 实体获取详细信息。
解决方案
我们必须正确地实现实体之间的关系。如,
- 一对一
- 一对多
- 多对多
如果您已经正确配置了这个,您应该能够获取人员实体,使用 getAccounts 方法从中获取 Account 实体,并使用 getDebitAccounts 方法从中获取 DebitAccount。
JPA 关系映射概念 https://www.mkyong.com/hibernate/hibernate-one-to-many-relationship-example/
假设关系是一对多,我添加了更多解释,
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.OneToMany;
@Entity
public class Person {
@Column
private String username;
@Column
private String email;
@OneToMany(mappedBy="user")
private Set<Account> accounts;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Set<Account> getAccounts() {
return accounts;
}
public void setAccounts(Set<Account> accounts) {
this.accounts = accounts;
}
}
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.OneToMany;
@Entity
public class Account {
@Column
String name;
@OneToMany(mappedBy="account")
private Set<DebitAccount> debitAccounts;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<DebitAccount> getDebitAccounts() {
return debitAccounts;
}
public void setDebitAccounts(Set<DebitAccount> debitAccounts) {
this.debitAccounts = debitAccounts;
}
}
import javax.persistence.Column;
import javax.persistence.Entity;
@Entity
public class DebitAccount {
@Column
String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
希望这可以帮助。
推荐阅读
- sql - 向下填充顶层详细信息
- azure-service-fabric - Azure Service Fabric - 无法绑定到已在使用的地址
- python - for循环不按顺序输出
- javascript - 当仅调用一个数组进行更改/排序时,反应状态数组正在更改/被排序
- python - 如何在 python 中替换阶乘、nCr 和 nPr 操作?
- angular - Angular 8 - 如何将 iframe src 动态设置为 pdf
- ios - 当应用程序终止/不在后台时,处理用户点击 Siri IntentsUI 以转到应用程序中的正确视图
- powershell - 检测 PowerShell 会话正在使用哪种字体
- javascript - 将值数组转换为具有该值作为属性的对象数组
- c# - 使用 versionId 和对象键 .net core 将文件从 s3 存储桶异步下载到本地机器