Spring Error:找不到类型Product的属性prod

时间:2018-05-03 17:13:18

标签: java spring spring-data-jpa

我是Spring的新手,面临一些问题。
我的实体有一个字段:
    package 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;
  }
}


我的产品存储库是这样的:
package 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);
}


我的产品控制器是这样的:
package 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!

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

1 个答案:

答案 0 :(得分:1)

Spring Data在搜索方法名称时使用camelCase约定

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

  public String getProdName() {
    return prodName;
  }