简体   繁体   English

如何在 Spring-Boot CrudRepository 中获取数组中具有值的对象列表

[英]How to get List of Objects with value in array in Spring-Boot CrudRepository

I have an Object defined that has ChildAccountList of Strings as one of the attributes.我定义了一个对象,该对象将字符串的 ChildAccountList 作为属性之一。

Is there a findBy method that I can use such that it will return the list of all rows where one of the elements in the array equals the passed in parameter.是否有我可以使用的 findBy 方法,以便它返回所有行的列表,其中数组中的一个元素等于传入的参数。 I mean something like:我的意思是这样的:

List <String> childrenAccountsList = Arrays.asList("A","B","C");
List<DBObjectType> retList = findBy_{whatGoesHere} (String childValue);
List<DBObjectType> retList = findBy_{whatGoesHere} ("A");

UPDATE ( with example )更新(举例)

Here are my class definitions这是我的类定义

UserREF.java用户引用文件

package test;

import com.microsoft.azure.spring.data.documentdb.core.mapping.Document;
import com.microsoft.azure.spring.data.documentdb.core.mapping.PartitionKey;
import org.springframework.data.annotation.Id;

import java.util.List;

@Document(collection = "user-management")

public class UserREF{
   @Id
   private String id;
   private String userId;
   private String role;
   private String primaryAccountId;

   @PartitionKey
   private String partition;
   private String shipTo;

//   @ElementCollection
   private List<ShipToAccounts> childShipToAccounts;

   public String getUserId(){
      return userId;
   }
   public void setUserId(String userId){
      this.userId = userId;
   }
   public String getRole(){
      return role;
   }
   public void setRole(String role){
      this.role = role;
   }
   public String getPrimaryAccountId(){
      return primaryAccountId;
   }
   public void setPrimaryAccountId(String primaryAccountId) {
      this.partition = primaryAccountId;
      this.primaryAccountId = primaryAccountId;
   }

   public String getPartition() {
      this.partition = this.primaryAccountId;
      return this.partition;
   }

   public void setPartition(String partition) {
      this.partition = this.primaryAccountId;
   }
   public String getShipTo(){
      return shipTo;
   }
   public void setShipTo(String shipTo){
      this.shipTo = shipTo;
   }
   public List<ShipToAccounts> getChildShipToAccounts(){
      return childShipToAccounts;
   }
   public void setChildShipToAccounts(List<ShipToAccounts> shipToAccounts){
      for (ShipToAccounts shipToAccount : shipToAccounts) {
         shipToAccount.setPrimaryAccountId(this.primaryAccountId);
      }
      this.childShipToAccounts = shipToAccounts;
   }
   public String getId(){
      return id;
   }
   public void setId(String id){
      this.id = id;
   }
}

ShipToAccounts.java ShipToAccounts.java

package test;


public class ShipToAccounts{
   private String primaryAccountId;
   private String accountId;

   public String getPrimaryAccountId() {
      return primaryAccountId;
   }

   public void setPrimaryAccountId(String primaryAccountId) {
      this.primaryAccountId = primaryAccountId;
   }

   public String getAccountId(){
      return accountId;
   }
   public void setAccountId(String input){
      this.accountId = input;
   }
}

UserREFRepository.java UserREFRepository.java

package test;

import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import com.microsoft.azure.spring.data.documentdb.core.DocumentDbOperations;
import com.microsoft.azure.spring.data.documentdb.repository.DocumentDbRepository;

import java.util.List;

@Repository
@ConditionalOnBean(DocumentDbOperations.class)
public interface UserREFRepository  extends DocumentDbRepository<UserREF, String> {

   @Override
   @Cacheable("test_users")
   UserREF findOne(String id, String partitionKeyValue);

   List<UserREF> findByRole(String role);

   List<UserREF> findByChildShipToAccounts_PrimaryAccountId (@Param("primaryAccountId") String primaryAccountId);

   List<UserREF> findAllByChildShipToAccounts_PrimaryAccountId(String s);


}

UserRefService.java用户引用服务.java

package test;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
public class UserRefService {

   private static final Logger log = LoggerFactory.getLogger(UserRefService.class);

   @Autowired
   private UserREFRepository userREFRepository;

   public UserREF save(UserREF inUser) {
      List<UserREF> adminUsersList =  userREFRepository.findAllByChildShipToAccounts_PrimaryAccountId("123456");
      System.out.println("users " + adminUsersList);
      UserREF newUser = userREFRepository.save(inUser);
      return newUser;
   }

}

And this is how my JSON document looks like这就是我的 JSON 文档的样子

    {
    "role": "admin",
    "partition": "123456",
    "primaryAccountId": "123456",
    "childShipToAccounts": [
        {
            "accountId": "1111",
            "primaryAccountId": "123456"
        },
        {
            "accountId": "2222",
            "primaryAccountId": "123456"
        },
        {
            "accountId": "3333",
            "primaryAccountId": "123456"
        }
    ],
    "id": "1b6d8497-1aca-4cab-9e3d-f8be3ba4f71c",
    "userId": "22",
    "shipTo": "2222"
}

I am getting below error on List<UserREF> adminUsersList = userREFRepository.findAllByChildShipToAccounts_PrimaryAccountId("123456");我在List<UserREF> adminUsersList = userREFRepository.findAllByChildShipToAccounts_PrimaryAccountId("123456");上遇到以下错误List<UserREF> adminUsersList = userREFRepository.findAllByChildShipToAccounts_PrimaryAccountId("123456");

java.lang.IllegalStateException: com.microsoft.azure.documentdb.DocumentClientException: 
    Message: {"Errors":["Invalid query. Specified parameter name '@childShipToAccounts.primaryAccountId' is invalid. 
    Parameter names should be in the format of symbol '@' followed by a valid identifier. E.g. @param1"]}
ActivityId: fd188aac-f99c-4983-b442-713c529dc930, Microsoft.Azure.Documents.Common/2.1.0.0, StatusCode: BadRequest

How should I define my "findBy" method ?我应该如何定义我的“findBy”方法?

Thanks谢谢

尝试 findByChildrenAccountsListContaining(yourString)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM