如何在Spring Boot的存储库类中创建函数



我试图在产品存储库类中创建一个函数,此函数将返回与给定参数类别相关的所有产品。所以我将传入和我将返回具有相同类别名称的所有产品。

Product Repository:

@Repository
public interface ProductRepository extends JpaRepository<Products, Long> {

List<Products> findByProduct_categoryCategoryName(String category);    }

类别实体:

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

产品实体类:

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

类别库:

@Repository
public interface CategoriesRepository extends JpaRepository<Categories,Long> {
}
错误日志:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'cartItemsControllers' defined in file [E:Spring BootwarehouseManagementSystemtargetclassescomexamplewarehouseManagementSystemControllersCartItemsControllers.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!

下划线(_)在Spring Data JPA查询方法中具有特殊含义(参见参考文档)。

遵循Java命名约定,将product_category重命名为Products类中的productCategory

之后,这应该可以工作:

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

如果没有,你也可以这样做:

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

尝试如下:

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

您用来过滤的属性product_category的类型是Categories,而不是您在方法中定义的参数String。因此,您需要从Categories类中指定嵌套的categoryName属性。

相关内容

  • 没有找到相关文章

最新更新