Home > Blockchain >  FindBy...In Spring JPA while using an enum error
FindBy...In Spring JPA while using an enum error

Time:03-12

I'm trying to query a findBy...in in Spring JPA. It gives me an java.util.NoSuchElementException. Here's my code:

ProductType:

public enum ProductType {
   Product, Service, PeriodicalService;
}

Controller:

public ResponseEntity<JsonStruct> findByProductsType(@RequestHeader("MarinaId") Integer idMarina, @PathVariable String[] productTypes){
    JsonStruct struct = new JsonStruct();
    struct.setStatusToSuccess();
    List<ProductType> productTypeList = Arrays.stream(productTypes).map(ProductType::valueOf).collect(Collectors.toList());
    struct.put("products", productService.findByProductTypeIn(productTypeList, idMarina));
    return new ResponseEntity<JsonStruct>(struct, HttpStatus.OK);
}

ServiceImpl:

@Override
public List<Product> findByProductTypeIn(List<ProductType> productTypes, Integer marinaId){
    return productProvider.findByProductTypeIn(productTypes, marinaId);
}

Service:

List<Product> findByProductTypeIn(List<ProductType> productTypes, Integer marinaId);

Provider(this is the Repository):

List<Product> findByProductTypeIn(List<ProductType> productTypes, Integer marinaId);

I can't see what I'm doing wrong...

CodePudding user response:

You should have only 1 argument in your method. The marinaId should be rénovée to match findByProductTypeIn method.

CodePudding user response:

As per spring documentation,if you are passing collection then it will support only 1 argument :

findByProductTypeIn(List<ProductType> productTypes);

If you want to use 2 arguments, you might need to iterate over List<ProductType> & then use something like this :

findByProductTypeAndMarinaid(ProductType p,Integer marinaid) 

Provided your underlying db supports ProductType & Integer in query.

  • Related