【问题标题】:How to fetch only the specific child entity along with its Parent entities with specific value using Spring JPA DATA如何使用 Spring JPA DATA 仅获取特定子实体及其具有特定值的父实体
【发布时间】:2021-01-18 11:21:53
【问题描述】:

我的实体类如下,

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);

此查询同时返回主要和次要对象,即使其中一个正在匹配。当名字和姓氏与主要匹配时,我不想返回次要。次要应为空,主要应有响应。

但它返回主要和次要。 spring jpa数据有没有办法只返回 匹配的子实体连同父实体??

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

【问题讨论】:

    标签: java spring hibernate jpa entity


    【解决方案1】:

    我认为这里的要求是获取数据并对其进行处理。作为open projections 的一部分,可以进行一些基本处理。但是,恐怕您在下面第 2,3 和第 4 点中对主要和次要帐户的预期处理必须在服务层中完成。

    1. 获取所有帐户,
    2. 如果主要客户和次要客户都匹配,请同时考虑,
    3. 否则,如果主要客户匹配,则从帐户中删除次要客户,
    4. 如果次要客户匹配,则从帐户中删除主要客户。

    【讨论】:

    • 这怎么是正确答案而不是stackoverflow.com/a/64193239/412446
    • Pts 1,2,3,4 就像 OP 所期望的“问题改述”。答案是要为 2,3,4 完成的工作必须在服务层中完成。 @ChristianBeikov。 stackoverflow.com/a/64193239/412446 不是正确答案,因为您无法通过您提供的方法达到 pts: 2,3 和 4。 AccountRepository.findTopByCustomerAccountsCustomerFirstNameAndCustomerAccountsCustomerLastameOrderByCustomerAccountsCustomerTypeAsc - 这将返回所有符合条件的帐户,但会将所有 CustomerAccounts 作为其 customerAccounts 属性的一部分。
    • 如果只想获取匹配的客户帐户,无论如何都需要编写专门的查询,因为在获取实体时不可能只获取集合的子集。
    • 仍然无法使用 JPQL 查询按照 OP 的预期进行过滤,可能使用本机查询并将它们映射到具有平面结构的简单视图对象。但同样,这里 OP 期望的对象不是具有平面结构的对象(它具有类似CustomerAccount 列表的属性)。所以我认为用查询过滤它会很困难,如果不是不可能的话。使用服务层中的过滤逻辑更容易。 :)
    • 如果我理解正确的话并不难:SELECT a.balance, a.deposits, p.firstName, p.lastName, s.firstName, s.lastName FROM Account a LEFT JOIN a.customerAccounts p ON p.customerType = 'Primary' LEFT JOIN a.customerAccounts s ON s.customerType = 'Secondary
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      我认为您需要一个从CustomerAccountAccount 类的双向链接和一个CustomerAccountRepository 类,该类有一个声明为findByCustomerFirstNameAndCustomerLastName 的方法,该方法返回链接回AccountCustomerAccount

      为什么您的方法不起作用 - 帐户存储库仅返回帐户对象(除非您添加 JPQL 并将返回对象映射到其他对象)。 AccountRepository.findByCustomerAccountsCustomerFirstNameAndCustomerAccountsCustomerLastame 为您找到一个(/全部)Account(/s),它的 CustomerAccountCustomer.firstNameCustomer.lastname 匹配用户提供的内容。 Account 仍然与CustomerAccount 具有一对多关系,因此将包含Account 的所有CustomerAccounts(在您的情况下为Viki 和Noah)。

      【讨论】:

      • 感谢您的建议,这可能会奏效。但是,如果主要和次要都与搜索条件匹配怎么办。在这种情况下,Primary 将有一个客户对象,Secondary 将有另一个客户对象。如果我选择双向,那么主要和次要都有 2 个不同的列表?关于如何处理这种情况的任何想法?
      • 我刚刚尝试了到 Account 的双向链接,即使只有 Primary 匹配,它仍然返回主要和次要。
      • 有趣。我看到你提到你有双向链接,但你能确认你已经添加了 CustomerAccountRepository 并且正在通过下面的查询吗? CustomerAccountRepository.findByCustomerFirstNameAndCustomerLastName 或者更确切地说是 CustomerAccountRepository.findAllByCustomerFirstNameAndCustomerLastName 第一种方法应该给你一个 CustomerAccount 或者如果你使用第二种方法,它应该给你一个 CustomerAccount 的列表你可以使用新的链接从客户账户进入账户你说你加了。
      • 正如我在第一个 cmets 中提到的,如果一个帐户的主要和次要都匹配。然后它将返回 2 个不同的对象,即 CustomerAccount 的列表并使用双向链接我可以获得 Account 但在这种情况下将有 2 个 Account 列表,一个将包含 Primary ,另一个将包含次要的。任何想法如何处理这个? 如果两者都匹配,实体Account 应该同时具有 Primary 和 Secondary。此外,如果只有,Secondary 被匹配,那么 Primary 应该为 null。
      • 我现在明白了,您还需要获取数据并对其进行处理。作为open projections 的一部分,可以进行一些基本处理。但是,恐怕您对主要和次要帐户的预期处理必须在服务层中完成。 -- 1) 获取所有帐户,-- 2) 如果主要客户和次要客户都匹配,则将两者都保留在帐户中,-- 3) 如果主要客户匹配,则从帐户中删除次要客户,-- 4) 如果次要客户匹配,则否则,从帐户中删除主要客户。
      猜你喜欢
      • 1970-01-01
      • 2016-10-16
      • 2022-09-27
      • 2021-06-14
      • 2018-12-03
      • 2020-05-16
      • 2023-03-19
      • 2016-10-07
      • 2020-07-11
      相关资源
      最近更新 更多