Home > other >  how to create a function in the repository class in Spring Boot
how to create a function in the repository class in Spring Boot

Time:10-26

I am trying to create a function in the products Repository class, this function will return all the products associated with the given parameter category. so I will pass in the categoryName and I will get back all the products that has the same category name.

Product Repository :

    @Repository
    public interface ProductRepository extends JpaRepository<Products, Long> {
    
List<Products> findByProduct_categoryCategoryName(String category);    }

Category Entity:

@Entity

public class Categories {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long categories_id;

    private String categoryName;

    @OneToMany(mappedBy = "product_category",fetch = FetchType.LAZY) //the name of the variable in the other class
    private Set<Products> product_category = new HashSet<>();

    public Categories(String categoryName, Set<Products> product_category) {
        this.categoryName = categoryName;
        this.product_category = product_category;
    }

    public Categories() {

    }

    public Long getCategories_id() {
        return categories_id;
    }

    public void setCategories_id(Long categories_id) {
        this.categories_id = categories_id;
    }

    public String getCategoryName() {
        return categoryName;
    }

    public void setCategoryName(String categoryName) {
        this.categoryName = categoryName;
    }

    public Set<Products> getProduct_category() {
        return product_category;
    }

    public void setProduct_category(Set<Products> product_category) {
        this.product_category = product_category;
    }

    @Override
    public String toString() {
        return categoryName;

    }
}

Products Entity Class:

@Entity
@Table(name = "Products")
@AllArgsConstructor
@NoArgsConstructor

public class Products {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long product_id;

    private String product_name;

    private double product_price;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "categories_id") //the name of the column in the other class and that name will be a column in the class
    private Categories product_category;


    private String product_quantity;

    private String product_Section;

    private String product_ExpDate;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "cart_item_id") //the name of the column in the other class and that name will be a column in the class
    private CartItem cartItem;


    public Long getProduct_id() {
        return product_id;
    }

    public void setProduct_id(Long product_id) {
        this.product_id = product_id;
    }

    public String getProduct_name() {
        return product_name;
    }

    public void setProduct_name(String product_name) {
        this.product_name = product_name;
    }

    public double getProduct_price() {
        return product_price;
    }

    public void setProduct_price(double product_price) {
        this.product_price = product_price;
    }

    public Categories getProduct_category() {
        return product_category;
    }

    public void setProduct_category(Categories product_category) {
        this.product_category = product_category;
    }

    public String getProduct_quantity() {
        return product_quantity;
    }

    public void setProduct_quantity(String product_quantity) {
        this.product_quantity = product_quantity;
    }

    public String getProduct_Section() {
        return product_Section;
    }

    public void setProduct_Section(String product_Section) {
        this.product_Section = product_Section;
    }

    public String getProduct_ExpDate() {
        return product_ExpDate;
    }

    public void setProduct_ExpDate(String product_ExpDate) {
        this.product_ExpDate = product_ExpDate;
    }
}

Category Repository:

@Repository
public interface CategoriesRepository extends JpaRepository<Categories,Long> {
}

Error Log:

    org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'cartItemsControllers' defined in file [E:\Spring Boot\warehouseManagementSystem\target\classes\com\example\warehouseManagementSystem\Controllers\CartItemsControllers.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'shoppingCartImp': Unsatisfied dependency expressed through field 'productRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'productRepository' defined in com.example.warehouseManagementSystem.Repository.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.warehouseManagementSystem.Repository.ProductRepository.findByProduct_categoryCategoryName(java.lang.String)! Reason: Failed to create query for method public abstract java.util.List com.example.warehouseManagementSystem.Repository.ProductRepository.findByProduct_categoryCategoryName(java.lang.String)! No property product found for type Products!; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.List com.example.warehouseManagementSystem.Repository.ProductRepository.findByProduct_categoryCategoryName(java.lang.String)! No property product found for type Products!
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:800) ~[spring-beans-5.3.10.jar:5.3.10]
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:229) ~[spring-beans-5.3.10.jar:5.3.10]
    Caused by: org.springframework.data.repository.query.QueryCreationException: Could not create query for public abstract java.util.List com.example.warehouseManagementSystem.Repository.ProductRepository.findByProduct_categoryCategoryName(java.lang.String)! Reason: Failed to create query for method public abstract java.util.List com.example.warehouseManagementSystem.Repository.ProductRepository.findByProduct_categoryCategoryName(java.lang.String)! No property product found for type Products!; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.List com.example.warehouseManagementSystem.Repository.ProductRepository.findByProduct_categoryCategoryName(java.lang.String)! No property product found for type Products!
    at org.springframework.data.repository.query.QueryCreationException.create(QueryCreationException.java:101) ~[spring-data-commons-2.5.5.jar:2.5.5]
.
.
.
Caused by: java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.List com.example.warehouseManagementSystem.Repository.ProductRepository.findByProduct_categoryCategoryName(java.lang.String)! No property product found for type Products!
    at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.<init>(PartTreeJpaQuery.java:96) ~[spring-data-jpa-2.5.5.jar:2.5.5]
    at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:107) ~[spring-data-jpa-2.5.5.jar:2.5.5]
    at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:218) ~[spring-data-jpa-2.5.5.jar:2.5.5]
    at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:81) ~[spring-data-jpa-2.5.5.jar:2.5.5]
    at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.lookupQuery(QueryExecutorMethodInterceptor.java:102) ~[spring-data-commons-2.5.5.jar:2.5.5]
    ... 71 common frames omitted
Caused by: org.springframework.data.mapping.PropertyReferenceException: No property product found for type Products!

CodePudding user response:

The underscore (_) has a special meaning in Spring Data JPA query methods (See reference documentation).

Follow Java naming conventions and rename product_category to productCategory in the Products class.

After that, this should work:

@Repository
public interface ProductRepository extends JpaRepository<Products, Long> {
   List<Products> findByProductCategoryCategoryName(String category);
}

If it does not, you can also try this:

@Repository
public interface ProductRepository extends JpaRepository<Products, Long> {
   List<Products> findByProductCategory_CategoryName(String category);
}

CodePudding user response:

Try the following:

@Repository
public interface ProductRepository extends JpaRepository<Products, Long> {
   List<Products> findByProduct_categoryCategoryName(String category);
}

The property product_category that you used to filter is of type Categories and not String which is the parameter that you defined in the method. So that this works, you need to specify the nested categoryName property from Categories class.

  • Related