如何使用 Spring JPA DATA 仅获取特定子实体及其具有特定值的父实体
How to fetch only the specific child entity along with its Parent entities with specific value using Spring JPA DATA
我的实体 类 在下面,
Account
accountId
balance
withdrawls
deposits
@OneToMany
List<CustomerAccount> customerAccounts
CustomerAccount
accountId
customerId
accountType
customerType (Primary/Secondary)
@OneToOne
Customer customer
Customer
accountId
customerId
firstName
lastName
假设用户搜索
名字 = viki 和
姓氏 = 黑色
账户资料库
findByCustomerAccountsCustomerFirstNameAndCustomerAccountsCustomerLastame(String firstName, String lastName);
此查询 return 既是主要对象又是次要对象,即使一个对象正在匹配也是如此。当名字和姓氏与主要匹配时,我不想 return 次要。辅助应为空,主要应有响应。
但它return既是主要的也是次要的。 有没有办法在 spring jpa 数据中仅 return
匹配的子实体和父实体??
"CustAccnt":{
"Account":{
"balance":"",
"deposits":""
},
"primary":{
"firstName":"Viki",
"lastName":"Black"
},
"secondary":{
"firstName":"Noah",
"lastName":"Morgan"
}
}
]}
我认为你需要一个从 CustomerAccount
到 Account
class 的双向 link 和一个 CustomerAccountRepository
class方法声明 findByCustomerFirstNameAndCustomerLastName
return 是 CustomerAccount
,link 返回 Account
。
为什么您的方法不起作用 - 帐户存储库仅 returns 帐户对象(除非您添加 JPQL 并将 return 对象映射到其他对象)。 AccountRepository.findByCustomerAccountsCustomerFirstNameAndCustomerAccountsCustomerLastame
找到一个(/所有)Account
(/s),其中 CustomerAccount
与 Customer.firstName
和 Customer.lastname
匹配用户提供的内容。 Account
仍然与 CustomerAccount
存在一对多关系,因此将包含 Account
的所有 CustomerAccount
(在您的情况下包括 Viki 和 Noah)。
您需要按客户类型排序,并使用分页机制。如果您真的必须使用方法名称约定(我讨厌它,它不可读,并且由于方法重命名,每个查询更改都需要重新启动应用程序)该方法可能应该是 findTopByCustomerAccountsCustomerFirstNameAndCustomerAccountsCustomerLastameOrderByCustomerAccountsCustomerTypeAsc
我认为这里的需求是获取数据并处理它。作为 open projections 的一部分,一些基本处理是可能的。但是,恐怕您必须在服务层中完成以下第 2、3 和 4 点中的主要和次要帐户的预期处理。
- 获取所有帐户,
- 如果主要客户和次要客户都匹配,请同时考虑,
- 否则,如果主要客户匹配,则从帐户中删除次要客户,
- 否则,如果次要客户匹配,则从帐户中删除主要客户。
我的实体 类 在下面,
Account
accountId
balance
withdrawls
deposits
@OneToMany
List<CustomerAccount> customerAccounts
CustomerAccount
accountId
customerId
accountType
customerType (Primary/Secondary)
@OneToOne
Customer customer
Customer
accountId
customerId
firstName
lastName
假设用户搜索 名字 = viki 和 姓氏 = 黑色
账户资料库
findByCustomerAccountsCustomerFirstNameAndCustomerAccountsCustomerLastame(String firstName, String lastName);
此查询 return 既是主要对象又是次要对象,即使一个对象正在匹配也是如此。当名字和姓氏与主要匹配时,我不想 return 次要。辅助应为空,主要应有响应。
但它return既是主要的也是次要的。 有没有办法在 spring jpa 数据中仅 return 匹配的子实体和父实体??
"CustAccnt":{
"Account":{
"balance":"",
"deposits":""
},
"primary":{
"firstName":"Viki",
"lastName":"Black"
},
"secondary":{
"firstName":"Noah",
"lastName":"Morgan"
}
}
]}
我认为你需要一个从 CustomerAccount
到 Account
class 的双向 link 和一个 CustomerAccountRepository
class方法声明 findByCustomerFirstNameAndCustomerLastName
return 是 CustomerAccount
,link 返回 Account
。
为什么您的方法不起作用 - 帐户存储库仅 returns 帐户对象(除非您添加 JPQL 并将 return 对象映射到其他对象)。 AccountRepository.findByCustomerAccountsCustomerFirstNameAndCustomerAccountsCustomerLastame
找到一个(/所有)Account
(/s),其中 CustomerAccount
与 Customer.firstName
和 Customer.lastname
匹配用户提供的内容。 Account
仍然与 CustomerAccount
存在一对多关系,因此将包含 Account
的所有 CustomerAccount
(在您的情况下包括 Viki 和 Noah)。
您需要按客户类型排序,并使用分页机制。如果您真的必须使用方法名称约定(我讨厌它,它不可读,并且由于方法重命名,每个查询更改都需要重新启动应用程序)该方法可能应该是 findTopByCustomerAccountsCustomerFirstNameAndCustomerAccountsCustomerLastameOrderByCustomerAccountsCustomerTypeAsc
我认为这里的需求是获取数据并处理它。作为 open projections 的一部分,一些基本处理是可能的。但是,恐怕您必须在服务层中完成以下第 2、3 和 4 点中的主要和次要帐户的预期处理。
- 获取所有帐户,
- 如果主要客户和次要客户都匹配,请同时考虑,
- 否则,如果主要客户匹配,则从帐户中删除次要客户,
- 否则,如果次要客户匹配,则从帐户中删除主要客户。