Unable to create Bean in Spring boot app with Google Cloud Datastore

52 Views Asked by At

For an existing project running in Google app Engine, I am trying to create a standalone Spring boot app to do CRUD operations in the Google Cloud Datastore.

With lot of examples from web, one such site I tried to follow is - [https://www.knowledgefactory.net/2023/02/spring-boot-google-cloud-datastore-build-rest-crud-apis.html]

So as per instructions, my repository :

public interface CarRepository extends DatastoreRepository<Car, Long> {
    
}

Car class:

@Entity(name = "cars")
public class Car {
    
    
    public Car(Long carId, String model, String brand, Integer mileage) {
        
        this.carId = carId;
        this.model = model;
        this.brand = brand;
        this.mileage = mileage;
    }

    @Id
    @Field(name = "Car_ID")
    private Long carId;
    
    private String model;
    
    private String brand;
    
    private Integer mileage;

    public Long getCarId() {
        return carId;
    }

    public void setCarId(Long carId) {
        this.carId = carId;
    }

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public Integer getMileage() {
        return mileage;
    }

    public void setMileage(Integer mileage) {
        this.mileage = mileage;
    }

    @Override
    public String toString() {
        return "Car [carId=" + carId + ", model=" + model + ", brand=" + brand + ", mileage=" + mileage + "]";
    }
}

Basic Controller:

@RestController
@Slf4j
public class Controller {   
    
    @Autowired
    private CarRepository carRepository; //Bean not creating
    
    @GetMapping("/")
    public String getHome() {

        return "Its working !!";
    }
    
    @GetMapping("/getAllCars")
    public List<Car> getAllCars()
    {
        List<Car> cars= new ArrayList<Car>();
        
        for (Car car : carRepository.findAll()) {
            cars.add(car);
        }
        
        return cars ;
    }
    
    @PostMapping("/createCar")
    public boolean createCar(@RequestParam String model,@RequestParam String brand,@RequestParam Integer mileage) {
        
        Long id = (long) (getAllCars().size() + 1);
        Car car = new Car(id,model,brand,mileage);
            carRepository.save(car);    
        return true;
    }

}

When I run it, I get an error saying

***************************
APPLICATION FAILED TO START
***************************

Description:

Field carRepository in com.kpts.gary.controller.Controller required a bean of type 'com.kpts.gary.repository.CarRepository' that could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'com.kpts.gary.repository.CarRepository' in your configuration.

So I made a configuration and add CarRepository bean

@Configuration
@EnableDatastoreRepositories("com.kpts.gary.repository")
public class ConfigurationData {
    
        
    @Bean
    public CarRepository getCarRepository ()
    {
        CarRepository carRepository = new CarRepository() {
            
            @Override
            public <S extends Car> Optional<S> findOne(Example<S> example) {
                // TODO Auto-generated method stub
                return Optional.empty();
            }
            
            @Override
            public <S extends Car> Page<S> findAll(Example<S> example, Pageable pageable) {
                // TODO Auto-generated method stub
                return null;
            }
            
            @Override
            public <S extends Car> Iterable<S> findAll(Example<S> example, Sort sort) {
                // TODO Auto-generated method stub
                return null;
            }
            
            @Override
            public <S extends Car> Iterable<S> findAll(Example<S> example) {
                // TODO Auto-generated method stub
                return null;
            }
            
            @Override
            public <S extends Car> boolean exists(Example<S> example) {
                // TODO Auto-generated method stub
                return false;
            }
            
            @Override
            public <S extends Car> long count(Example<S> example) {
                // TODO Auto-generated method stub
                return 0;
            }
            
            @Override
            public <S extends Car> Iterable<S> saveAll(Iterable<S> entities) {
                // TODO Auto-generated method stub
                return null;
            }
            
            @Override
            public <S extends Car> S save(S entity) {
                // TODO Auto-generated method stub
                return null;
            }
            
            @Override
            public Optional<Car> findById(Long id) {
                // TODO Auto-generated method stub
                return Optional.empty();
            }
            
            @Override
            public Iterable<Car> findAllById(Iterable<Long> ids) {
                // TODO Auto-generated method stub
                return null;
            }
            
            @Override
            public Iterable<Car> findAll() {
                // TODO Auto-generated method stub
                return null;
            }
            
            @Override
            public boolean existsById(Long id) {
                // TODO Auto-generated method stub
                return false;
            }
            
            @Override
            public void deleteById(Long id) {
                // TODO Auto-generated method stub
                
            }
            
            @Override
            public void deleteAllById(Iterable<? extends Long> ids) {
                // TODO Auto-generated method stub
                
            }
            
            @Override
            public void deleteAll(Iterable<? extends Car> entities) {
                // TODO Auto-generated method stub
                
            }
            
            @Override
            public void deleteAll() {
                // TODO Auto-generated method stub
                
            }
            
            @Override
            public void delete(Car entity) {
                // TODO Auto-generated method stub
                
            }
            
            @Override
            public long count() {
                // TODO Auto-generated method stub
                return 0;
            }
            
            @Override
            public Page<Car> findAll(Pageable pageable) {
                // TODO Auto-generated method stub
                return null;
            }
            
            @Override
            public Iterable<Car> findAll(Sort sort) {
                // TODO Auto-generated method stub
                return null;
            }
            
            @Override
            public <A> A performTransaction(Function<DatastoreRepository<Car, Long>, A> operations) {
                // TODO Auto-generated method stub
                return null;
            }
        };  
        return carRepository;
    }

Now it works but I will have to manually recreate all functions which isnt the right way I guess, where am I going wrong and why the bean for repository not getting created ?

0

There are 0 best solutions below