'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.
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. 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
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;
}
}
And this is how my JSON document looks like
{
"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");
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 ?
Thanks
Solution 1:[1]
Try findByChildrenAccountsListContaining(yourString)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Ashish Gupta |