Home > Enterprise >  UnsatisfiedDependencyException, Error creating bean with name
UnsatisfiedDependencyException, Error creating bean with name

Time:09-26

when I'm trying to start the application I get the following message:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'productServiceImpl' defined in file [C:\Users\Acasa\0 SDA\0 Proiecte practice\attentive2details\target\classes\com\example\attentive2details\Service\ProductServiceImpl.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'productRepository' defined in com.example.attentive2details.repositories.ProductRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Invocation of init method failed; nested exception is org.springframework.data.repository.query.QueryCreationException: Could not create query for public abstract java.util.List com.example.attentive2details.repositories.ProductRepository.findByNameStartingWith(java.util.List)! No property name found for type Produs!

This is happening since I have added a new method List<Produs> findByNameStartingWith(List<Produs> string) into the ProductRepository.

The application code is:

@Data
@Table
@Entity
@AllArgsConstructor
@NoArgsConstructor


public class Product {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    @Column
    private String produs;
    @Column
    private Float calories;
}



@Repository

public interface ProductRepository extends JpaRepository<Product, Integer> {

   List<Product> findByNameStartingWith(List<Product> string);
 
}


public interface ProductService {

    List<Product> findByNameStartingWith(List<Product> string);
    
}


@Service

public class ProductServiceImpl implements ProductService {

    ProductRepository productRepository;

    public ProductServiceImpl(ProductRepository productRepository) {
        this.productRepository = productRepository;
    }
    
    @Override
    public List<Product> findByNameStartingWith(List<Product> string) {
        return productRepository.findByNameStartingWith(string);
    }
}


@RequestMapping("/api")
@RestController

public class ProductController {

    private final ProductService productService;

    public ProductController(ProductService productService) {
        this.productService = productService;
    }


    @GetMapping("findProductbyNameStartingWith/produs")
    @Query("FROM Product p WHERE p.produs LIKE %:name%")
    public List<Product> findProductsbyNameStartingWith(@Param("name") String beginswith) {
        List<Product> allproducts = new ArrayList<>( productService.findAll());
        List<Product> productsListBeginWith = new ArrayList<>();
        for (Product product : allproducts) {
            if(product.getProdus().startsWith(String.valueOf(beginswith))){
                productsListBeginWith.add(product);
            }
        }
        return productService.findByNameStartingWith(productsListBeginWith);
    }
}

the application structure

CodePudding user response:

As your log mention

com.example.attentive2details.repositories.ProductRepository.findByNameStartingWith(java.util.List)! No property name found for type Produs!

You wrote findByNameStartingWith but you don't have name filed in your model. Jpa couldn't find this property in your entity class. You should put the name property in your model or change the query to another field for example findByProdusStartingWith or findByCaloriesStartingWith.

CodePudding user response:

In addition to @Faramarz Afzali answer (which is actually correct), please note that the following @Query have no effect:

@RequestMapping("/api")
@RestController
public class ProductController {
    (...)

    @GetMapping("findProductbyNameStartingWith/produs")
    @Query("FROM Product p WHERE p.produs LIKE %:name%")
    public List<Product> findProductsbyNameStartingWith(@Param("name") String beginswith)
    
    (...)
}

@Query must be used in Repository methods, not on a Controller or Service.

  • Related