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

标签 java spring-boot repository azure-cosmosdb findby

我定义了一个对象,该对象将字符串的 ChildAccountList 作为属性之一。

是否有我可以使用的 findBy 方法,它会返回所有行的列表,其中数组中的元素之一等于传入的参数。 我的意思是:

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

更新(带例子)

这是我的类定义

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

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

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

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

}

这就是我的 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"
}

我在 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

我应该如何定义“findBy”方法?

谢谢

最佳答案

试试 findByChildrenAccountsListContaining(yourString)

关于java - 如何在 Spring-Boot CrudRepository 中获取数组中具有值的对象列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54602801/

相关文章:

java - Glassfish 3.1.2 中的连接池遇到 ClassCastException

Java String 过滤掉不需要的字符

java - 遍历 Map 特别是 LinkedHashMap 并使用 ForEach 循环检索 Key 和 Value

java - 在 hystrix 调用后备之前,Ribbon 是否会遍历所有 Eurkea 注册实例?

svn - 与多个用户一起使用 SVN

Git 错误 : Unable to negotiate with XX. XX.XXX.XXX : no matching host key type found . 他们的报价:ssh-dss

java - 使用java中的插入排序算法对数组列表中的温度进行排序

java - Spring boot @Autowired 在单元测试用例中不起作用

java - 聆听单个消费者的多个主题

repository - 每个分支的任务与 perforce