java - Spring 错误: No property prod found for type Product

标签 java spring spring-data-jpa

我是 Spring 新手,面临一些问题。
我的实体有一个字段:
包 com.ecommercesystem.ecommerce.entities;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.sql.Blob;

@Entity
public class Product {

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private Integer ID_product;

  @NotNull
  private String prod_name;

  @NotNull
  private String prod_desc;

  @NotNull
  private float price;

  @NotNull
  private String prod_brand;

  @NotNull
  private Blob prod_image;
  private int prod_rating;

  @ManyToOne
  private Category category;

  protected Product(){}

  public Product(Integer ID_product, @NotNull String prod_name, @NotNull 
    String prod_desc, @NotNull float price, @NotNull String prod_brand, 
    @NotNull Blob prod_image, int prod_rating, Category category) {
    this.ID_product = ID_product;
    this.prod_name = prod_name;
    this.prod_desc = prod_desc;
    this.price = price;
    this.prod_brand = prod_brand;
    this.prod_image = prod_image;
    this.prod_rating = prod_rating;
    this.category = category;
  }

  public Integer getID_product() {
    return ID_product;
  }

  public void setID_product(Integer ID_product) {
    this.ID_product = ID_product;
  }

  public String getProd_name() {
    return prod_name;
  }

  public void setProd_name(String prod_name) {
    this.prod_name = prod_name;
  }

  public String getProd_desc() {
    return prod_desc;
  }

  public void setProd_desc(String prod_desc) {
    this.prod_desc = prod_desc;
  }

  public float getPrice() {
    return price;
  }

  public void setPrice(float price) {
    this.price = price;
  }

  public String getProd_brand() {
    return prod_brand;
  }

  public void setProd_brand(String prod_brand) {
    this.prod_brand = prod_brand;
  }

  public Blob getProd_image() {
    return prod_image;
  }

  public void setProd_image(Blob prod_image) {
    this.prod_image = prod_image;
  }

  public int getProd_rating() {
    return prod_rating;
  }

  public void setProd_rating(int prod_rating) {
    this.prod_rating = prod_rating;
  }

  public Category getCategory() {
    return category;
  }

  public void setCategory(Category category) {
    this.category = category;
  }
}


我的产品存储库是这样的:
包 com.ecommercesystem.ecommerce.repositories;

import org.springframework.data.jpa.repository.JpaRepository;
import com.ecommercesystem.ecommerce.entities.Product;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import java.util.Optional;

// This will be AUTO IMPLEMENTED by Spring into a Bean called 
productRepository
// CRUD refers Create, Read, Update, Delete

@Repository
public interface ProductRepository extends JpaRepository<Product, Integer> {
  Optional<Product> findByProd_name(String prod_name);
//  Optional<Product> findByProd_brand (String prod_brand);
}


我的产品 Controller 是这样的:
包 com.ecommercesystem.ecommerce.controllers;

import com.ecommercesystem.ecommerce.entities.Category;
import com.ecommercesystem.ecommerce.entities.Product;
import com.ecommercesystem.ecommerce.repositories.ProductRepository;
//import com.ecommercesystem.ecommerce.services.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.Optional;

@Controller
public class ProductController {

  @Autowired
  ProductRepository productRepository;

  @GetMapping(path = "/")
  public String front() {
    return "index";
  }

  @GetMapping(path = "/search_results")
  public String search(@RequestParam(value = "search", required = false) 
        String search, Model model){
    Optional<Product> productName = 
         productRepository.findByProd_name(search);
    if (productName.isPresent()){
      if (search.equals(productName.get().getProd_name())){
        model.addAttribute("ID_product", productName.get().getID_product());
        model.addAttribute("price", productName.get().getPrice());
        model.addAttribute("prodName", productName.get().getProd_name());
        model.addAttribute("category_name", 
          productName.get().getCategory().getCategory_name());
//        model.addAttribute("prodDesc", productName.get().getProd_desc());
//        model.addAttribute("prodImage", productName.get().getProd_image());
//        model.addAttribute("prodBrand", productName.get().getProd_brand());
      }
    }
    return "search_results";
  }
}


但是当我运行该程序时,我收到此错误消息:
错误日志:

[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 10.277 s <<< FAILURE! - in com.ecommercesystem.ecommerce.EcommerceApplicationTests
[ERROR] contextLoads(com.ecommercesystem.ecommerce.EcommerceApplicationTests)  Time elapsed: 0.017 s  <<< ERROR!
java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'productController': Unsatisfied dependency expressed through field 'productRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'productRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.Optional com.ecommercesystem.ecommerce.repositories.ProductRepository.findByProd_name(java.lang.String)! No property prod found for type Product!
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'productRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.Optional com.ecommercesystem.ecommerce.repositories.ProductRepository.findByProd_name(java.lang.String)! No property prod found for type Product!
Caused by: java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.Optional com.ecommercesystem.ecommerce.repositories.ProductRepository.findByProd_name(java.lang.String)! No property prod found for type Product!
Caused by: org.springframework.data.mapping.PropertyReferenceException: No property prod found for type Product!


非常感谢任何帮助。谢谢!

最佳答案

Spring Data 在搜索方法名称时使用驼峰命名法

  public void setProdName(String prodName) {
     this.prodName = prodName;
  } 

  public String getProdName() {
    return prodName;
  }

关于java - Spring 错误: No property prod found for type Product,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50160588/

相关文章:

java - hibernate 事务 : Only Last item of list is saved into the table

java - 使用 Firebase 刷新列表的最佳方法是什么

具有外部资源目录的 Java Web 应用程序

java - 表单输入文件的 Spring 注释验证

java - Spring Data JPA (Hibernate) 与动态条件的一对多关系

java - 扩展异常处理

java - 您的 SQL 语法有错误;检查与您的 MySQL 服务器对应的手册

java - 加快搜索表单中的数据访问速度

Spring 规范 'and' 不起作用

java - 如何在不需要主键的情况下通过 JPA 持久化数据