如何使用Spring JPA Data仅检索具有特定值的父实体及其特定子实体。

huangapple go评论71阅读模式
英文:

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

假设用户搜索条件为
firstName = viki 和
lastName = black

AccountRepository

findByCustomerAccountsCustomerFirstNameAndCustomerAccountsCustomerLastName(String firstName, String lastName);

即使匹配一个条件,此查询会返回主要对象和次要对象。我不希望在名字和姓氏与主要对象匹配时返回次要对象。次要对象应该为空,主要对象应该有响应。

但是它返回了主要和次要对象。在Spring JPA Data中是否有一种方法只返回匹配的子实体以及父实体?

{
    "CustAccnt": {
        "Account": {
            "balance": "",
            "deposits": ""
        },
        "primary": {
            "firstName": "Viki",
            "lastName": "Black"
        },
        "secondary": {
            "firstName": "Noah",
            "lastName": "Morgan"
        }
    }
}
英文:

My Entity classes are below,

Account
    accountId
	balance
	withdrawls
	deposits
    @OneToMany
	List<CustomerAccount> customerAccounts

CustomerAccount
	accountId
	customerId
	accountType
	customerType (Primary/Secondary)
	@OneToOne
	Customer customer

Customer
	accountId
	customerId
	firstName
	lastName

Assume the user searches with
firstName = viki and
lastName = black

AccountRepository

findByCustomerAccountsCustomerFirstNameAndCustomerAccountsCustomerLastame(String firstName, String lastName);

This query returns both the primary and secondary object even when one is getting matched. I dont want to return secondary, when the firstname and lastname matches with Primary. The secondary should be null and Primary should have response.

But it returns both the primary and secondary. Is there a way in spring jpa data to return only
the matched child entity along with the Parent entity??

"CustAccnt":{	
	"Account":{
		"balance":"",
        "deposits":""
	},
	"primary":{
         "firstName":"Viki",
         "lastName":"Black"
	},
	"secondary":{
         "firstName":"Noah",
         "lastName":"Morgan"
	}
}
]}

答案1

得分: 1

我认为你需要从 CustomerAccountAccount 类之间建立一个双向链接,并且创建一个名为 CustomerAccountRepository 的类,其中声明了一个方法 findByCustomerFirstNameAndCustomerLastName,该方法返回与之关联的 CustomerAccount,后者又链接到一个 Account

关于你已有的方法为何不起作用 - Account 存储库只会返回 Account 对象(除非你添加 JPQL 并将返回对象映射到其他内容)。AccountRepository.findByCustomerAccountsCustomerFirstNameAndCustomerAccountsCustomerLastame 会查找具有与用户提供的 Customer.firstNameCustomer.lastname 匹配的 CustomerAccount 的一个或多个 AccountAccount 仍然与 CustomerAccount 之间保持一对多的关系,因此将包含所有适用于该 AccountCustomerAccount(在你的情况中即 Viki 和 Noah)。

英文:

I think you need a bidirectional link from the CustomerAccount to the Account class and a CustomerAccountRepository class which has a method declared findByCustomerFirstNameAndCustomerLastName that returns the CustomerAccount which links back to an Account.

Why the method you have will not work - Account repository only returns Account objects(unless you add JPQL and map the return object to something else). AccountRepository.findByCustomerAccountsCustomerFirstNameAndCustomerAccountsCustomerLastame
finds you an(/all) Account(/s) that have a CustomerAccount with Customer.firstName and Customer.lastname matching what is provided by the user. The Account still has a one to many relationship to CustomerAccount, so will contain all the CustomerAccounts for the Account(in your case both Viki and Noah).

答案2

得分: 1

你需要按客户类型进行排序,并使用分页机制。如果你真的必须使用方法名约定(我讨厌它,它很难读,每次查询更改都需要重新启动应用程序,因为方法被重命名了),该方法的名称可能应为findTopByCustomerAccountsCustomerFirstNameAndCustomerAccountsCustomerLastameOrderByCustomerAccountsCustomerTypeAsc

英文:

You need to order by the customer type and use the pagination mechanism. If you really must use the method name convention(I hate it, it's unreadable and every query change requires a restart of the application because of method renaming) the method should probably be findTopByCustomerAccountsCustomerFirstNameAndCustomerAccountsCustomerLastameOrderByCustomerAccountsCustomerTypeAsc

答案3

得分: 1

我认为这里的要求是获取数据并进行处理。一些基本的处理可以作为开放式投影的一部分来实现。然而,您在下面的2、3和4点中对主账户和辅助账户的预期处理将不得不在服务层中完成,恐怕我无能为力。

  1. 获取所有账户,
  2. 如果主要客户和辅助客户都匹配,则保留两者在账户中,
  3. 否则,如果主要客户匹配,则从账户中移除辅助客户,
  4. 否则,如果辅助客户匹配,则从账户中移除主要客户。
英文:

I think the requirement here is to fetch data and process it as well. Some basic processing is possible as part of open projections. However, your expected processing of Primary and Secondary Accounts as in points 2,3 and 4 below will have to be done in a service layer I'm afraid.

  1. fetch all accounts,
  2. if both primary and secondary customer match, keep both in account,
  3. else if primary customer match, remove secondary customer from account,
  4. else if secondary customer match, remove primary customer from account.

huangapple
  • 本文由 发表于 2020年10月3日 03:38:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/64177354.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定