我正在嘗試在 products Repository 類中創建一個函式,該函式將回傳與給定引數類別關聯的所有產品。所以我將傳入categoryName,然后我將取回所有具有相同類別名稱的產品。
產品庫:
@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 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!
uj5u.com熱心網友回復:
下劃線 ( _) 在 Spring Data JPA 查詢方法中具有特殊含義(請參閱參考檔案)。
遵循 Java 命名約定并在類中重命名product_category為。productCategoryProducts
之后,這應該作業:
@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);
}
uj5u.com熱心網友回復:
請嘗試以下操作:
@Repository
public interface ProductRepository extends JpaRepository<Products, Long> {
List<Products> findByProduct_categoryCategoryName(String category);
}
product_category您用于過濾的屬性屬于型別,Categories而不是String您在方法中定義的引數。為了使其有效,您需要categoryName從Categories類中指定嵌套屬性。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/337288.html
