0

Below is my DTO class:

@Entity
@Data
@Table(name = "carservice")
public class CarService {

//  public static void main(String[] args) {
        
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO) 
        long serviceCode;

        @Column(name = "service")
        private String service;
        
        @Column(name = "hatchback")
        private String hatchback;
        
        @Column(name = "sedan")
        private String sedan;
        
        @Column(name = "suv")
        private String suv;

        
        
        public CarService(long serviceCode, String service, String hatchback, String sedan, String suv) {
            super();
            this.serviceCode = serviceCode;
            this.service = service;
            this.hatchback = hatchback;
            this.sedan = sedan;
            this.suv = suv;
        }

        public long getServiceCode() {
            return serviceCode;
        }

        public void setServiceCode(long serviceCode) {
            this.serviceCode = serviceCode;
        }

        public String getService() {
            return service;
        }

        public void setService(String service) {
            this.service = service;
        }

        public String getHatchback() {
            return hatchback;
        }

        public void setHatchback(String hatchback) {
            this.hatchback = hatchback;
        }

        public String getSedan() {
            return sedan;
        }

        public void setSedan(String sedan) {
            this.sedan = sedan;
        }
.....

My controller is as follows:

@CrossOrigin(origins = "http://localhost:8081")
@RestController
@RequestMapping("/api")
public class CarServiceController {

    @Autowired
    CarServiceRepository CarServiceRepository;

    @GetMapping("/CarServices/{id}")
    public ResponseEntity<CarService> getCarServiceById(@PathVariable("id") long id) {
        List<CarService> CarServiceData = CarServiceRepository.findByServiceCode(id);

        if (CarServiceData!=null) {
            return new ResponseEntity<>(CarServiceData.get(0), HttpStatus.OK);
        } else {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
    }

    

}

And the repository class is as follows:

@Repository
public interface CarServiceRepository extends JpaRepository<CarService, Long> {
     
    @Query(value = "SELECT * FROM CARSERVICE", nativeQuery = true)
    List<CarService> findByServiceCode(Long id);

//  List<CarService> findByTitleContaining(Long id);
      
    }

When I am running the maven install command , the tests are failing and the error in surefire-reports are as follows:

-------------------------------------------------------------------------------
Test set: com.example.demo.CarServiceApplicationTests
-------------------------------------------------------------------------------
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 5.445 s <<< FAILURE! - in com.example.demo.CarServiceApplicationTests
contextLoads  Time elapsed: 0.036 s  <<< ERROR!
java.lang.IllegalStateException: Failed to load ApplicationContext for [WebMergedContextConfiguration@7a78d380 testClass = com.example.demo.CarServiceApplicationTests, locations = [], classes = [com.example.demo.CarServiceApplication], contextInitializerClasses = [], activeProfiles = [], propertySourceLocations = [], propertySourceProperties = ["org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true"], contextCustomizers = [org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@5082d622, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@4278284b, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@44c73c26, org.springframework.boot.test.autoconfigure.actuate.observability.ObservabilityContextCustomizerFactory$DisableObservabilityContextCustomizer@9da1, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@1ab06251, org.springframework.boot.test.context.SpringBootTestAnnotation@8dc993d1], resourceBasePath = "src/main/webapp", contextLoader = org.springframework.boot.test.context.SpringBootContextLoader, parent = null]
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'carServiceController': Unsatisfied dependency expressed through field 'CarServiceRepository': Error creating bean with name 'carServiceRepository' defined in com.example.demo.repository.CarServiceRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Not a managed type: class com.example.demo.model.CarService
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'carServiceRepository' defined in com.example.demo.repository.CarServiceRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Not a managed type: class com.example.demo.model.CarService
Caused by: java.lang.IllegalArgumentException: Not a managed type: class com.example.demo.model.CarService

I've tried many solutions found on the net regarding similar issues but didn't solve my issue. Please help. Thanks in advance!

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
Geethika
  • 27
  • 7
  • check this: https://stackoverflow.com/questions/28664064/spring-boot-not-a-managed-type The part you are exactly missing is this: @EnableJpaRepositories("my.package.base.*") @ComponentScan(basePackages = { "my.package.base.*" }) @EntityScan("my.package.base.*") – kastillo Jan 02 '23 at 13:24
  • Thanks for your response. I've added the annotations stated above in the "CarServiceApplication", I then ran maven clean n then maven install, still getting same errors. Have I done the right changes or am I wrong? Thanks. – Geethika Jan 02 '23 at 13:44
  • There is far too little information here and the information shown here indicate you don't understand the technologies you are using. First you are using JPA and as such `CarService` needs a no-args constructor (required). Assuming you are using Spring Boot 3 you need to make sure you are using `jakarta.persistence` annotations and not `javax.persistence` annotations. Make sure that your `@SpringBootApplication` class is in a top-level package (and ditch the `@EnableJPaRepositories` as that isn't needed with Spring Boot. – M. Deinum Jan 02 '23 at 13:58

0 Answers0