I know that similar questions have been asked before on this forum, but none of the proposed solutions helped me, so I am writing a separate question. The code is from the spring boot course on youtube (https://youtu.be/zvR-Oif_nxg)(im stuck about 2:21:00), but this error also occurs when I tried to do the course on the spring boot site. The full text of the error is:
"Error creating bean with name 'departmentController': Unsatisfied dependency expressed through field 'departmentService': Error creating bean with name 'departmentServiceImpl': Unsatisfied dependency expressed through field 'departmentRepository': Error creating bean with name 'departmentRepository' defined in com.kamilosinni.course.repository.DepartmentRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Not a managed type: class com.kamilosinni.course.entity.Department"
main app class (CourseApplication.java):
package com.kamilosinni.course;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class CourseApplication {
public static void main(String[] args) {
SpringApplication.run(CourseApplication.class, args);
}
}
DepartmentController.java:
package com.kamilosinni.course.controller;
import com.kamilosinni.course.entity.Department;
import com.kamilosinni.course.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class DepartmentController {
@Autowired
private DepartmentService departmentService;
@GetMapping("/departments")
public Department saveDepartment(@RequestBody Department department)
{
return departmentService.saveDepartment(department);
}
}
Department.java:
package com.kamilosinni.course.entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Department {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long departmentId;
private String departmentName;
private String departmentAdress;
private String departmentCode;
public Long getDepartmentId() {
return departmentId;
}
public void setDepartmentId(Long departmentId) {
this.departmentId = departmentId;
}
public String getDepartmentName() {
return departmentName;
}
public void setDepartmentName(String departmentName) {
this.departmentName = departmentName;
}
public String getDepartmentAdress() {
return departmentAdress;
}
public void setDepartmentAdress(String departmentAdress) {
this.departmentAdress = departmentAdress;
}
public String getDepartmentCode() {
return departmentCode;
}
public void setDepartmentCode(String departmentCode) {
this.departmentCode = departmentCode;
}
public Department(Long departmentId, String departmentName, String departmentAdress, String departmentCode) {
this.departmentId = departmentId;
this.departmentName = departmentName;
this.departmentAdress = departmentAdress;
this.departmentCode = departmentCode;
}
public Department(){}
@Override
public String toString() {
return "Department{" +
"departmentId=" + departmentId +
", departmentName='" + departmentName + '\'' +
", departmentAdress='" + departmentAdress + '\'' +
", departmentCode='" + departmentCode + '\'' +
'}';
}
}
DepartmentRepository.java:
package com.kamilosinni.course.repository;
import com.kamilosinni.course.entity.Department;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface DepartmentRepository extends JpaRepository<Department, Long> {}
DepartmentService.java:
package com.kamilosinni.course.service;
import com.kamilosinni.course.entity.Department;
public interface DepartmentService {
Department saveDepartment(Department department);
}
DepartmentServiceImpl.java:
package com.kamilosinni.course.service;
import com.kamilosinni.course.entity.Department;
import com.kamilosinni.course.repository.DepartmentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class DepartmentServiceImpl implements DepartmentService{
@Autowired
DepartmentRepository departmentRepository;
DepartmentServiceImpl(){}
@Override
public Department saveDepartment(Department department) {
return departmentRepository.save(department);
}
}
i tried to add additional annotations to the main class:
package com.kamilosinni.course;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
@SpringBootApplication
@EntityScan("com.kamilosinni.course.entity")
@EnableJpaRepositories("com.kamilosinni.course.repository")
public class CourseApplication {
public static void main(String[] args) {
SpringApplication.run(CourseApplication.class, args);
}
}
Also i tried to move the class to the main package with the main class and addthe @NoRepositoryBean
annotation to the DepartmentRepository
, that didn't do anything either
Edit: if i replace javax
imports with jakarta
in Department.java, there is no error. In addition, in the guides on the official site, javax
is used all the time, which is quite strange, because even the rewritten code does not work. Maybe the configuration is incorrect? Or maybe dependencies.
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
I also add my pom.xml file, it may be helpful:
<?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.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.kamilosinni</groupId>
<artifactId>course</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>course</name>
<description>Spring boot course</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
<version>2.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
File structure here
Encountered the same issue.
In my case, the problem was :
javax.persistence:javax.persistence-api:2.2
), removed it@Entity
and @Id
- replaced them with :import jakarta.persistence.Entity;
import jakarta.persistence.Id;
Seems that javax.persistence
was replaced by jakarta.persistence
last year. Anyway, spring data JPA examples now uses jakarta package
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With