4

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

2 Answers 2

12

Encountered the same issue.

In my case, the problem was :

  1. In wrong dependency (javax.persistence:javax.persistence-api:2.2), removed it
  2. Wrong imports for @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

Sign up to request clarification or add additional context in comments.

1 Comment

Correct answer. You save my day. Thank you.
0

in the class DepartmentServiceImpl there is no public constructor. It is needed by Spring for dependency injection.

Solution

Either add public identifier to the constructor or remove it. A public constructor without arguments will be created by Java internally (if no other constructor present).

Edited after you added the pom.xml to the question:

Please remove this dependency:

<dependency>
    <groupId>javax.persistence</groupId>
    <artifactId>javax.persistence-api</artifactId>
    <version>2.2</version>
</dependency>

3 Comments

I tried both solutions both adding public and removing the entire constructor declaration, but unfortunately, still the same problem :(
When I remove this dependency, I can't use import 'javax' anymore. Should I use 'jakarta' instead of 'javax' and it will be the same? (I'm asking because both guides i am converting use javax i dont know if jakarta will be the correct form)
jakarta is the correct one to work smoothly with the other Spring dependencies you have already.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.