I am creating a project that functions like an ecommerce website, but I keep getting the errors mentioned in the title. I tried looking on other threads, and they talked about adding different annotations to the classes. For example, I added the @EntityScan and @ComponentScan annotations to my main class, but I am still getting the same errors. Here are the exact error messages, and below that is my code:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'categoryService': Unsatisfied dependency expressed through field 'categoryRepository': Error creating bean with name 'categoryRepository' defined in com.ecommerce.website.repository.CategoryRepository defined in @EnableJpaRepositories declared on EcommerceWebsiteApplication: Not a managed type: class com.ecommerce.website.model.Category
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:712) ~[spring-beans-6.0.2.jar:6.0.2]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:692) ~[spring-beans-6.0.2.jar:6.0.2]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:127) ~[spring-beans-6.0.2.jar:6.0.2]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:481) ~[spring-beans-6.0.2.jar:6.0.2]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1397) ~[spring-beans-6.0.2.jar:6.0.2]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:598) ~[spring-beans-6.0.2.jar:6.0.2]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:521) ~[spring-beans-6.0.2.jar:6.0.2]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:326) ~[spring-beans-6.0.2.jar:6.0.2]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-6.0.2.jar:6.0.2]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:324) ~[spring-beans-6.0.2.jar:6.0.2]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) ~[spring-beans-6.0.2.jar:6.0.2]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:961) ~[spring-beans-6.0.2.jar:6.0.2]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:915) ~[spring-context-6.0.2.jar:6.0.2]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:584) ~[spring-context-6.0.2.jar:6.0.2]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:146) ~[spring-boot-3.0.0.jar:3.0.0]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:730) ~[spring-boot-3.0.0.jar:3.0.0]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:432) ~[spring-boot-3.0.0.jar:3.0.0]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:308) ~[spring-boot-3.0.0.jar:3.0.0]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1302) ~[spring-boot-3.0.0.jar:3.0.0]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1291) ~[spring-boot-3.0.0.jar:3.0.0]
at com.ecommerce.website.EcommerceWebsiteApplication.main(EcommerceWebsiteApplication.java:19) ~[classes/:na]
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'categoryRepository' defined in com.ecommerce.website.repository.CategoryRepository defined in @EnableJpaRepositories declared on EcommerceWebsiteApplication: Not a managed type: class com.ecommerce.website.model.Category
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1751) ~[spring-beans-6.0.2.jar:6.0.2]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:599) ~[spring-beans-6.0.2.jar:6.0.2]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:521) ~[spring-beans-6.0.2.jar:6.0.2]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:326) ~[spring-beans-6.0.2.jar:6.0.2]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-6.0.2.jar:6.0.2]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:324) ~[spring-beans-6.0.2.jar:6.0.2]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) ~[spring-beans-6.0.2.jar:6.0.2]
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:254) ~[spring-beans-6.0.2.jar:6.0.2]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1405) ~[spring-beans-6.0.2.jar:6.0.2]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1325) ~[spring-beans-6.0.2.jar:6.0.2]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:709) ~[spring-beans-6.0.2.jar:6.0.2]
... 20 common frames omitted
Caused by: java.lang.IllegalArgumentException: Not a managed type: class com.ecommerce.website.model.Category
at org.hibernate.metamodel.model.domain.internal.JpaMetamodelImpl.managedType(JpaMetamodelImpl.java:181) ~[hibernate-core-6.1.5.Final.jar:6.1.5.Final]
at org.hibernate.metamodel.model.domain.internal.MappingMetamodelImpl.managedType(MappingMetamodelImpl.java:496) ~[hibernate-core-6.1.5.Final.jar:6.1.5.Final]
at org.hibernate.metamodel.model.domain.internal.MappingMetamodelImpl.managedType(MappingMetamodelImpl.java:99) ~[hibernate-core-6.1.5.Final.jar:6.1.5.Final]
at org.springframework.data.jpa.repository.support.JpaMetamodelEntityInformation.<init>(JpaMetamodelEntityInformation.java:77) ~[spring-data-jpa-3.0.0.jar:3.0.0]
at org.springframework.data.jpa.repository.support.JpaEntityInformationSupport.getEntityInformation(JpaEntityInformationSupport.java:69) ~[spring-data-jpa-3.0.0.jar:3.0.0]
at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getEntityInformation(JpaRepositoryFactory.java:246) ~[spring-data-jpa-3.0.0.jar:3.0.0]
at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:211) ~[spring-data-jpa-3.0.0.jar:3.0.0]
at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:194) ~[spring-data-jpa-3.0.0.jar:3.0.0]
at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:81) ~[spring-data-jpa-3.0.0.jar:3.0.0]
at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:317) ~[spring-data-commons-3.0.0.jar:3.0.0]
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.lambda$afterPropertiesSet$5(RepositoryFactoryBeanSupport.java:279) ~[spring-data-commons-3.0.0.jar:3.0.0]
at org.springframework.data.util.Lazy.getNullable(Lazy.java:229) ~[spring-data-commons-3.0.0.jar:3.0.0]
at org.springframework.data.util.Lazy.get(Lazy.java:113) ~[spring-data-commons-3.0.0.jar:3.0.0]
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:285) ~[spring-data-commons-3.0.0.jar:3.0.0]
at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:132) ~[spring-data-jpa-3.0.0.jar:3.0.0]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1797) ~[spring-beans-6.0.2.jar:6.0.2]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1747) ~[spring-beans-6.0.2.jar:6.0.2]
... 30 common frames omitted
Main class
@SpringBootApplication
@EnableJpaRepositories("com.ecommerce.website.repository")
@ComponentScan("com.ecommerce.website.service")
@EntityScan("com.ecommerce.website.model")
public class EcommerceWebsiteApplication {
public static void main(String[] args) {
SpringApplication.run(EcommerceWebsiteApplication.class, args);
}
}
Controller class
package com.ecommerce.website.controller;
import java.util.Optional;
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.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import com.ecommerce.website.model.Category;
import com.ecommerce.website.service.CategoryService;
//Category ID still having issues
//Need to add methods for Category delete and update methods
@Controller
public class AdminController {
//Injecting dependency
@Autowired
CategoryService categoryService;
//This method maps for /admin endpoint. It shows the adminHome.html page. The method is @GetMapping because you are getting information
//@return the adminHome page
@GetMapping("/admin")
public String adminHome() {
//Display html page
return "adminHome";
}
//This method maps for /admin/categories endpoint. It shows the categories.html page. The method is @GetMapping because you are getting information
//@return the categories page
@GetMapping("/admin/categories")
public String getCat(Model model) {
model.addAttribute("categories", categoryService.getAllCategories());
//Display html page
return "categories";
}
//This method maps for /admin/categories endpoint. It shows the categoriesAdd.html page, but before that, it creates
//a model that is sent to the html file via Thymeleaf. The categoriesAdd.html page is a form for adding a category
//The method is @GetMapping because you are getting information
//@param model will hold the category object so that the form can add information to it
//@return the categoriesAdd page
@GetMapping("/admin/categories/add")
public String getCatAdd(Model model) {
//Adding the model name "category", and creating a new Category object and putting it in the model
model.addAttribute("category", new Category());
//Display html page
return "categoriesAdd";
}
//This method maps for /admin/categories endpoint. It allows the user to add a category to the list of categories
//The method is @PostMapping because you are sending information
//@param category gets its value from the ModelAttribute, which gets its value from the form
//@return the categories page
@PostMapping("/admin/categories/add")
public String postCatAdd(@ModelAttribute("category") Category category) {
//Calling categoryService to add the category
categoryService.addCategory(category);
//Redirecting to page that shows all categories
return "redirect:/admin/categories";
}
Service
package com.ecommerce.website.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ecommerce.website.model.Category;
import com.ecommerce.website.repository.CategoryRepository;
@Service
public class CategoryService {
//Injecting dependency
@Autowired
CategoryRepository categoryRepository;
//This method calls categoryRepository to add a category to the database
//@param category is the category that is being added
public void addCategory(Category category) {
//Saving the category to the database
categoryRepository.save(category);
}
//This method calls categoryRepository to retrieve a list of categories
//@return the list of categories
public List<Category> getAllCategories() {
return categoryRepository.findAll();
}
// public Optional<Category> findCategoryById(String id) {
//
// Optional<Category> category = categoryRepository.findById(id);
//
// return category;
//
// }
}
Repository
package com.ecommerce.website.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.ecommerce.website.model.Category;
@Repository
public interface CategoryRepository extends JpaRepository<Category, String> {
}
POJO
package com.ecommerce.website.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Table;
import org.springframework.data.annotation.Id;
@Entity
@Table
public class Category {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "category_id")
private int id;
private String name;
public Category() {
}
public Category(int id, String name) {
super();
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
application.properties
spring.h2.console.enabled=true
spring.datasource.url=jdbc:h2:file:./db
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.hibernate.ddl-auto=update
server.port=8080
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.ecommerce</groupId>
<artifactId>ecommerce-website</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>ecommerce-website</name>
<description>Ecommerce website using Spring Boot</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-validation -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.2.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-security -->
<!--<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
<version>3.0.0</version>
</dependency>-->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
CodePudding user response:
Please remove this dependency from pom.xml
and try again.
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.2.Final</version>
</dependency>
CodePudding user response:
keep only this annotation
@SpringBootApplication
public class EcommerceWebsiteApplication {
public static void main(String[] args) {
SpringApplication.run(EcommerceWebsiteApplication.class, args);
}
@Autowired
private CategoryRepository categoryRepository;
}
you nedd to add an interface like this
public interface CategoryService2 {
public void addCategory(Category category);
public List<Category> getAllCategories();
}
and modify the classe like this:
@Service
public class CategoryService implements CategoryService2{
private CategoryRepository categoryRepository;
public CategoryService(categoryRepository){
super();
this.categoryRepository = categoryRepository;
}
@Override
public void addCategory(Category category) {
categoryRepository.save(category);
}
@Override
public List<Category> getAllCategories() {
return categoryRepository.findAll();
}
and remove the annotation @Repository from CategoryRepository and try to modify String in Long
public interface CategoryRepository extends JpaRepository<Category, Long> {
}
Modify the class Category like this.
@Entity
@Table(name = "Ecommerce")
public class Category {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "category_id")
private Long id;
@Column(name = "name")
private String name;
public Category() {
}
public Category(int id, String name) {
super();
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
and try with and without this dependecy:
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.2.Final</version>
</dependency>
modify the controllore like this:
@Controller
public class AdminController {
private CategoryService2 categoryService2;
public AdminController(CategoryService2 categoryService){
super();
this.categoryService2 = categoryService2;
}
//This method maps for /admin endpoint. It shows the adminHome.html page. The method is @GetMapping because you are getting information
//@return the adminHome page
@GetMapping("/admin")
public String adminHome() {
//Display html page
return "adminHome";
}
//This method maps for /admin/categories endpoint. It shows the categories.html page. The method is @GetMapping because you are getting information
//@return the categories page
@GetMapping("/admin/categories")
public String getCat(Model model) {
model.addAttribute("categories", categoryService2.getAllCategories());
//Display html page
return "categories";
}
//This method maps for /admin/categories endpoint. It shows the categoriesAdd.html page, but before that, it creates
//a model that is sent to the html file via Thymeleaf. The categoriesAdd.html page is a form for adding a category
//The method is @GetMapping because you are getting information
//@param model will hold the category object so that the form can add information to it
//@return the categoriesAdd page
@GetMapping("/admin/categories/add")
public String getCatAdd(Model model) {
//Adding the model name "category", and creating a new Category object and putting it in the model
model.addAttribute("category", new Category());
//Display html page
return "categoriesAdd";
}
//This method maps for /admin/categories endpoint. It allows the user to add a category to the list of categories
//The method is @PostMapping because you are sending information
//@param category gets its value from the ModelAttribute, which gets its value from the form
//@return the categories page
@PostMapping("/admin/categories/add")
public String postCatAdd(@ModelAttribute("category") Category category) {
//Calling categoryService to add the category
categoryService2.addCategory(category);
//Redirecting to page that shows all categories
return "redirect:/admin/categories";
}