Я разрабатываю приложение Spring Boot CRUD и обнаружил ошибку при запуске приложения. Сообщение об ошибке указывает на проблему с внедрением зависимостей, связанную с ProductService и ProductRepository. Несмотря на многочисленные попытки исправить это, я не могу решить проблему. Ниже приведены подробные сведения об ошибке и соответствующие фрагменты кода.
Сообщение об ошибке:
o.s.boot.SpringApplication: Ошибка запуска приложения
org.springframework.beans.factory.UnsatisfiedDependencyException: Ошибка создания bean-компонента с именем «productRepository», определенный в com.khomsi.site_project.repository.ProductRepository, определенный в @EnableJpaRepositories, объявленный в JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: не управляемый тип: класс com.khomsi.site_project.entity.Product
Это adminController:
package com.khomsi.site_project.repository;
import com.khomsi.site_project.entity.Category;
import com.khomsi.site_project.entity.Product;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface ProductRepository extends JpaRepository
{
@Query("SELECT product FROM Product product WHERE (product.category.id = ?1 OR product.category.allParentsIDs LIKE %?2%)"
+ "ORDER BY product.title ASC")
public Page listByCategory(Integer categoryId, Pageable pageable, String categoryIDMatch);
@Query(value = "SELECT * FROM product WHERE MATCH(title, description) AGAINST (?1)",
nativeQuery = true)
public Page search(String keyword, Pageable pageable);
public Product findByAlias(String alias);
public Product findByTitle(String title);
public List findAllByCategoryId(Integer category);
public Long countById(Integer id);
@Query("SELECT p FROM Product p WHERE p.title LIKE %?1% "
+ "OR p.description LIKE %?1% "
+ "OR p.vendor.title LIKE %?1% "
+ "OR p.category.title LIKE %?1%")
public Page findAll(String keyword, Pageable pageable);
@Query("SELECT p FROM Product p WHERE p.category.id = ?1 " +
"OR p.category.allParentsIDs LIKE %?2%")
public Page findAllInCategory(Integer categoryId, String categoryIdMatch,
Pageable pageable);
@Query("SELECT p FROM Product p WHERE (p.category.id = ?1 " +
"OR p.category.allParentsIDs LIKE %?2%) AND "
+ "(p.title LIKE %?3% "
+ "OR p.description LIKE %?3% "
+ "OR p.vendor.title LIKE %?3% "
+ "OR p.category.title LIKE %?3%)")
public Page searchInCategory(Integer categoryId, String categoryIdMatch,
String keyword, Pageable pageable);
}
Я убедился, что сущность Product помечена аннотацией @Entity, и использовал @Repository для ProductRepository. Однако я по-прежнему сталкиваюсь с ошибкой «Не управляемый тип» для класса Product.
Может ли кто-нибудь помочь мне определить, что может быть причиной этой проблемы и как ее решить?
Я разрабатываю приложение Spring Boot CRUD и обнаружил ошибку при запуске приложения. Сообщение об ошибке указывает на проблему с внедрением зависимостей, связанную с ProductService и ProductRepository. Несмотря на многочисленные попытки исправить это, я не могу решить проблему. Ниже приведены подробные сведения об ошибке и соответствующие фрагменты кода. Сообщение об ошибке: o.s.boot.SpringApplication: Ошибка запуска приложения org.springframework.beans.factory.UnsatisfiedDependencyException: Ошибка создания bean-компонента с именем «productRepository», определенный в com.khomsi.site_project.repository.ProductRepository, определенный в @EnableJpaRepositories, объявленный в JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: не управляемый тип: класс com.khomsi.site_project.entity.Product Это adminController: [code]package com.khomsi.site_project.controller;
@GetMapping("/products/edit/{id}") public String updateProduct(@PathVariable(name = "id") int id, Model model, RedirectAttributes attributes) { try { Product product = ProductService.getProduct(id); List vendorList = vendorService.getAllVendors(); List categoryList = categoryService.listCategoriesUserInForm(); model.addAttribute("product", product); model.addAttribute("vendorList", vendorList); model.addAttribute("categoryList", categoryList); } catch (ProductNotFoundException e) { attributes.addFlashAttribute("message", e.getMessage()); return "redirect:/admin/product/products"; } return "admin/product/product_form"; }
@GetMapping("/products/new") public String addProduct(Model model) {
List vendorList = vendorService.getAllVendors(); List categoryList = categoryService.listCategoriesUserInForm(); model.addAttribute("product", new Product()); model.addAttribute("vendorList", vendorList); model.addAttribute("categoryList", categoryList);
return "admin/product/product_form"; }
@PostMapping("/products/save") public String saveProduct(Product product, RedirectAttributes redirect) { ProductService.saveProduct(product); redirect.addFlashAttribute("message", "The product was saved successfully"); return "redirect:/admin/products"; }
@GetMapping("/products/delete/{id}") public String deleteProduct(@PathVariable(name = "id") Integer id, RedirectAttributes redirect) { try { ProductService.deleteProduct(id); redirect.addFlashAttribute("message", "The product ID " + id + " has been deleted successfully"); } catch (ProductNotFoundException e) { redirect.addFlashAttribute("message", e.getMessage()); } return "redirect:/admin/products"; }
@GetMapping("/users") public String listUsersFirstPage(Model model) { return adminTools.listUsersByPage(1, model); }
@GetMapping("/users/new") public String newUser(Model model) { model.addAttribute("user", new User()); model.addAttribute("userInfo", new UserInfo()); model.addAttribute("roles", Role.values()); return "admin/user/user_form"; }
@PostMapping("/users/save") public String createUser(UserInfo userInfo, User user, RedirectAttributes redirect) { user.setUserInfo(userInfo); userInfo.setUser(user); userService.saveUser(user); redirect.addFlashAttribute("message", "The user was saved successfully");
return "redirect:/admin/users"; }
@GetMapping("/users/edit/{id}") public String updateUser(@PathVariable(name = "id") int id, Model model, RedirectAttributes redirect) { try { User user = userService.getUser(id); UserInfo userInfo = userInfoService.getUserDetail(id); model.addAttribute("user", user); model.addAttribute("userInfo", userInfo); model.addAttribute("roles", Role.values()); } catch (UserNotFoundException e) { redirect.addFlashAttribute("message", e.getMessage()); return "redirect:/admin/users"; } return "admin/user/user_form"; }
@GetMapping("/users/delete/{id}") public String deleteUser(@PathVariable(name = "id") Integer id, RedirectAttributes redirect) { try { userService.deleteUser(id); redirect.addFlashAttribute("message", "The user ID " + id + " has been deleted successfully"); } catch (UserNotFoundException e) { redirect.addFlashAttribute("message", e.getMessage()); } return "redirect:/admin/users"; }
@GetMapping("/categories") public String listCategoriesFirstPage(Model model) { return adminTools.listCategoriesByPage(1, model); }
@GetMapping("/categories/edit/{id}") public String updateCategory(@PathVariable int id, Model model, RedirectAttributes attributes) { try { Category category = categoryService.getCategory(id); List categoryList = categoryService.listCategoriesUserInForm(); model.addAttribute("category", category); model.addAttribute("categoryList", categoryList); return "admin/category/category_form"; } catch (CategoryNotFoundException e) { attributes.addFlashAttribute("message", e.getMessage()); return "redirect:/admin/categories"; } }
@GetMapping("/categories/new") public String addCategory(Model model) { List categoryList = categoryService.listCategoriesUserInForm(); model.addAttribute("category", new Category()); model.addAttribute("categoryList", categoryList);
return "admin/category/category_form"; }
@PostMapping("/categories/save") public String saveCategory(@ModelAttribute Category category, RedirectAttributes attributes) { categoryService.saveCategory(category); attributes.addFlashAttribute("message", "The category has been saved successfully"); return "redirect:/admin/categories"; }
@GetMapping("/categories/delete/{id}") public String deleteCategory(@PathVariable(name = "id") Integer id, RedirectAttributes redirect) { try { categoryService.deleteCategory(id); redirect.addFlashAttribute("message", "The category ID " + id + " has been deleted successfully"); } catch (CategoryNotFoundException e) { redirect.addFlashAttribute("message", e.getMessage()); } return "redirect:/admin/categories"; }
@GetMapping("/vendors") public String listVendorsFirstPage(Model model) { return adminTools.listVendorsByPage(1, model); }
@GetMapping("/vendors/new") public String newVendor(Model model) { model.addAttribute("vendor", new Vendor()); return "admin/vendor/vendor_form"; }
@PostMapping("/vendors/save") public String createVendor(Vendor vendor, RedirectAttributes redirect) { vendorService.saveVendor(vendor); redirect.addFlashAttribute("message", "The vendor was saved successfully"); return "redirect:/admin/vendors"; }
@GetMapping("/products/edit/{id}") public String updateProduct(@PathVariable(name = "id") int id, Model model, RedirectAttributes attributes) { try { Product product = ProductService.getProduct(id); List vendorList = vendorService.getAllVendors(); List categoryList = categoryService.listCategoriesUserInForm(); model.addAttribute("product", product); model.addAttribute("vendorList", vendorList); model.addAttribute("categoryList", categoryList); } catch (ProductNotFoundException e) { attributes.addFlashAttribute("message", e.getMessage()); return "redirect:/admin/product/products"; } return "admin/product/product_form"; }
@GetMapping("/products/new") public String addProduct(Model model) {
List vendorList = vendorService.getAllVendors(); List categoryList = categoryService.listCategoriesUserInForm(); model.addAttribute("product", new Product()); model.addAttribute("vendorList", vendorList); model.addAttribute("categoryList", categoryList);
return "admin/product/product_form"; }
@PostMapping("/products/save") public String saveProduct(Product product, RedirectAttributes redirect) { ProductService.saveProduct(product); redirect.addFlashAttribute("message", "The product was saved successfully"); return "redirect:/admin/products"; }
@GetMapping("/products/delete/{id}") public String deleteProduct(@PathVariable(name = "id") Integer id, RedirectAttributes redirect) { try { ProductService.deleteProduct(id); redirect.addFlashAttribute("message", "The product ID " + id + " has been deleted successfully"); } catch (ProductNotFoundException e) { redirect.addFlashAttribute("message", e.getMessage()); } return "redirect:/admin/products"; }
@GetMapping("/users") public String listUsersFirstPage(Model model) { return adminTools.listUsersByPage(1, model); }
@GetMapping("/users/new") public String newUser(Model model) { model.addAttribute("user", new User()); model.addAttribute("userInfo", new UserInfo()); model.addAttribute("roles", Role.values()); return "admin/user/user_form"; }
@PostMapping("/users/save") public String createUser(UserInfo userInfo, User user, RedirectAttributes redirect) { user.setUserInfo(userInfo); userInfo.setUser(user); userService.saveUser(user); redirect.addFlashAttribute("message", "The user was saved successfully");
return "redirect:/admin/users"; }
@GetMapping("/users/edit/{id}") public String updateUser(@PathVariable(name = "id") int id, Model model, RedirectAttributes redirect) { try { User user = userService.getUser(id); UserInfo userInfo = userInfoService.getUserDetail(id); model.addAttribute("user", user); model.addAttribute("userInfo", userInfo); model.addAttribute("roles", Role.values()); } catch (UserNotFoundException e) { redirect.addFlashAttribute("message", e.getMessage()); return "redirect:/admin/users"; } return "admin/user/user_form"; }
@GetMapping("/users/delete/{id}") public String deleteUser(@PathVariable(name = "id") Integer id, RedirectAttributes redirect) { try { userService.deleteUser(id); redirect.addFlashAttribute("message", "The user ID " + id + " has been deleted successfully"); } catch (UserNotFoundException e) { redirect.addFlashAttribute("message", e.getMessage()); } return "redirect:/admin/users"; }
@GetMapping("/categories") public String listCategoriesFirstPage(Model model) { return adminTools.listCategoriesByPage(1, model); }
@GetMapping("/categories/edit/{id}") public String updateCategory(@PathVariable int id, Model model, RedirectAttributes attributes) { try { Category category = categoryService.getCategory(id); List categoryList = categoryService.listCategoriesUserInForm(); model.addAttribute("category", category); model.addAttribute("categoryList", categoryList); return "admin/category/category_form"; } catch (CategoryNotFoundException e) { attributes.addFlashAttribute("message", e.getMessage()); return "redirect:/admin/categories"; } }
@GetMapping("/categories/new") public String addCategory(Model model) { List categoryList = categoryService.listCategoriesUserInForm(); model.addAttribute("category", new Category()); model.addAttribute("categoryList", categoryList);
return "admin/category/category_form"; }
@PostMapping("/categories/save") public String saveCategory(@ModelAttribute Category category, RedirectAttributes attributes) { categoryService.saveCategory(category); attributes.addFlashAttribute("message", "The category has been saved successfully"); return "redirect:/admin/categories"; }
@GetMapping("/categories/delete/{id}") public String deleteCategory(@PathVariable(name = "id") Integer id, RedirectAttributes redirect) { try { categoryService.deleteCategory(id); redirect.addFlashAttribute("message", "The category ID " + id + " has been deleted successfully"); } catch (CategoryNotFoundException e) { redirect.addFlashAttribute("message", e.getMessage()); } return "redirect:/admin/categories"; }
@GetMapping("/vendors") public String listVendorsFirstPage(Model model) { return adminTools.listVendorsByPage(1, model); }
@GetMapping("/vendors/new") public String newVendor(Model model) { model.addAttribute("vendor", new Vendor()); return "admin/vendor/vendor_form"; }
@PostMapping("/vendors/save") public String createVendor(Vendor vendor, RedirectAttributes redirect) { vendorService.saveVendor(vendor); redirect.addFlashAttribute("message", "The vendor was saved successfully"); return "redirect:/admin/vendors"; }
import java.util.List; @Repository public interface ProductRepository extends JpaRepository { @Query("SELECT product FROM Product product WHERE (product.category.id = ?1 OR product.category.allParentsIDs LIKE %?2%)" + "ORDER BY product.title ASC") public Page listByCategory(Integer categoryId, Pageable pageable, String categoryIDMatch);
@Query(value = "SELECT * FROM product WHERE MATCH(title, description) AGAINST (?1)", nativeQuery = true) public Page search(String keyword, Pageable pageable);
public Product findByAlias(String alias);
public Product findByTitle(String title);
public List findAllByCategoryId(Integer category);
public Long countById(Integer id);
@Query("SELECT p FROM Product p WHERE p.title LIKE %?1% " + "OR p.description LIKE %?1% " + "OR p.vendor.title LIKE %?1% " + "OR p.category.title LIKE %?1%") public Page findAll(String keyword, Pageable pageable);
@Query("SELECT p FROM Product p WHERE p.category.id = ?1 " + "OR p.category.allParentsIDs LIKE %?2%") public Page findAllInCategory(Integer categoryId, String categoryIdMatch, Pageable pageable);
@Query("SELECT p FROM Product p WHERE (p.category.id = ?1 " + "OR p.category.allParentsIDs LIKE %?2%) AND " + "(p.title LIKE %?3% " + "OR p.description LIKE %?3% " + "OR p.vendor.title LIKE %?3% " + "OR p.category.title LIKE %?3%)") public Page searchInCategory(Integer categoryId, String categoryIdMatch, String keyword, Pageable pageable); }
[/code] Я убедился, что сущность Product помечена аннотацией @Entity, и использовал @Repository для ProductRepository. Однако я по-прежнему сталкиваюсь с ошибкой «Не управляемый тип» для класса Product. Может ли кто-нибудь помочь мне определить, что может быть причиной этой проблемы и как ее решить?