How can i fix this Exception? -> Caused by: java.lang.IllegalArgumentException: Not a managed type:

692 views Asked by At

i have got this Exception

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'dumpController' defined in file [...\tech\backend\webapplication\com\controller\DumpController.class]: Unsatisfied dependency expressed through constructor parameter 0: Error creating bean with name 'dumpService' defined in file [...\tech\backend\webapplication\com\service\DumpService.class]: Unsatisfied dependency expressed through constructor parameter 0: Error creating bean with name 'dumpRepository' defined in tech.backend.webapplication.com.repo.DumpRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Not a managed type: class tech.backend.webapplication.com.model.Dump
...


Caused by: java.lang.IllegalArgumentException: Not a managed type: class tech.backend.webapplication.com.model.Dump
...

Process finished with exit code 1

Here are my classes:

MainClass

package tech.backend.webapplication;

@SpringBootApplication
public class WebapplicationApplication {

    public static void main(String[] args) {
        SpringApplication.run(WebapplicationApplication.class, args);
    }

}

ModelClass:

package tech.backend.webapplication.com.model;


@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@ToString
@Entity
public class Dump implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String name;
    private String email;
    
}

Repository:

package tech.backend.webapplication.com.repo;

@Repository
public interface DumpRepository extends JpaRepository<Dump, Long> {
    void deleteDumpById(Long id);
    Optional<Dump> findDumpById(Long id);
}

Service:

package tech.backend.webapplication.com.service;

@Service
@RequiredArgsConstructor
@Slf4j
@Transactional
public class DumpService {

    private final DumpRepository dumpRepository;

    public Dump addDump(Dump dump) {
        dump.setName("Johannes");
        dump.setEmail("[email protected]");
        return dumpRepository.save(dump);
    }

    public List<Dump> getAllDumps() {
        return dumpRepository.findAll();
    }

    public Dump getDumpById(Long id) {
        return dumpRepository.findDumpById(id)
                .orElseThrow(() -> new DumpNotFoundException("Dump by id " + id + "was not found"));
    }

    public Dump updateDump(Dump dump) {
        return dumpRepository.save(dump);
    }

    public void deleteDump(Long id) {
        dumpRepository.deleteDumpById(id);
    }
}

Controller:

package tech.backend.webapplication.com.controller;

@RestController
@RequestMapping("/dump")
@RequiredArgsConstructor
@Slf4j
public class DumpController {

    private final DumpService dumpService;

    @GetMapping("/all")
    public ResponseEntity<List<Dump>> getAllDumps() {
        List<Dump> dumps = dumpService.getAllDumps();
        return new ResponseEntity<>(dumps, HttpStatus.OK);
    }

    @GetMapping("/find/{id}")
    public ResponseEntity<Dump> getDumpById(@PathVariable("id") Long id) {
        Dump dump = dumpService.getDumpById(id);
        return new ResponseEntity<>(dump, HttpStatus.OK);
    }

    @PostMapping("/add")
    public ResponseEntity<Dump> addDump(@RequestBody Dump dump) {
        Dump newDump = dumpService.addDump(dump);
        return new ResponseEntity<>(newDump, HttpStatus.CREATED);
    }

    @PutMapping("/update")
    public ResponseEntity<Dump> updateDump(@RequestBody Dump dump) {
        Dump updatedDump = dumpService.updateDump(dump);
        return new ResponseEntity<>(updatedDump, HttpStatus.CREATED);
    }

    @DeleteMapping("/delete/{id}")
    public ResponseEntity<?> deleteDump(@PathVariable("id") Long id) {
        dumpService.deleteDump(id);
        return new ResponseEntity<>(HttpStatus.OK);
    }

}

The packages are as follows:

  • java
    • tech.backend.application
    • com
      • controller
      • exception
      • model
      • repo
      • service
    • WebapplicationApplication (class)
  • ressources

Can somebody help me to fix this error? I would appreciate that :)

Thanks

Every time I start the application, this exception is the output

2

There are 2 answers

2
Oliver On

Spring Boot's auto-configuration probably cannot find your entity Dump.

Add the @EntityScan annotation to your application class or a configuration class, for example:

package tech.backend.webapplication;

@SpringBootApplication
@EntityScan("tech.backend.webapplication.com.model")
public class WebapplicationApplication {

    public static void main(String[] args) {
        SpringApplication.run(WebapplicationApplication.class, args);
    }

}
0
amir rad On

On multi-modules this issue can happen,you can do on your main class,

@SpringBootApplication
@ComponentScan(basePackages = {"tech.backend.webapplication.*"})
public class WebapplicationApplication {

public static void main(String[] args) {
    SpringApplication.run(WebapplicationApplication.class, args);
}
}