0

I am a beginner in Java and in the Spring environment. At the moment I have to implement a CustomerRepository. The CustomerRepository implements a CrudRepository interface. The purpose is that in the repository, customer objects should be stored and retrieved. I have to use a Mock implementation of the Spring class CrudRepository.

The classdiagram looks like this: classdiagram

CrudRepository interface:

public interface CrudRepository<Customer, String> {

<S extends Customer> S save( S entity );
...
}

The complete CustomerRepository class:

public class CustomerRepository implements CrudRepository<Customer, String> {
 
private CrudRepository crudrepository;

/*long count();*/
@Override
public long count() {
    long count = crudrepository.count();
    return count;
}

/*<S extends Customer> S save( S entity );*/
@Override
public <S extends Customer> S save(S entity) {
    crudrepository.save(entity);
    return entity;
}

/*<S extends Customer> Iterable<S> saveAll( Iterable<S> entities );*/
@Override
public <S extends Customer> Iterable<S> saveAll(Iterable<S> entities) {
    Iterable<S> response = crudrepository.saveAll(entities);
    return (Iterable<S>) response;
}

/*Optional<Customer> findById(String id );*/
@Override
public Optional<Customer> findById(String id) {
    Optional<Customer> customerResponse = crudrepository.findById(id);
    return customerResponse;

}

/*Iterable<Customer> findAllById(Iterable<String> ids );*/
@Override
public Iterable<Customer> findAllById(Iterable<String> ids) {
    Iterable<Customer> customerResponse = crudrepository.findAllById(ids);
    return customerResponse;
}

/*Iterable<Customer> findAll();*/
@Override
public Iterable<Customer> findAll() {
    Iterable<Customer> customerResponse = (Iterable<Customer>) crudrepository
            .findAll();
    return customerResponse;
}

/*boolean existsById(String id );*/
@Override
public boolean existsById(String id) {
    return crudrepository.existsById(id);
}

/*void deleteById(String id );*/
@Override
public void deleteById(String id) {
    crudrepository.deleteById(id);

}

/*void delete(Customer entity );*/
@Override
public void delete(Customer entity) {
    crudrepository.delete(entity);

}

/*void deleteAllById(Iterable<? extends String> ids );*/
@Override
public void deleteAllById(Iterable<? extends String> entities) {
    crudrepository.deleteAll(entities);

}

/*void deleteAll();*/
@Override
public void deleteAll() {
    crudrepository.deleteAll();
}

/*void deleteAll(Iterable<? extends Customer> entities );*/
@Override
public void deleteAll(Iterable<? extends Customer> entities) {
    crudrepository.deleteAll(entities);
    
} }

How does that look for you ? Any suggestions ?

1 Answers1

0

I think you are misunderstanding some concepts.

CrudRepository is a Spring object and it's an interface you don't need to implement. You have to extend it and Spring provides you all the magic.

You can achieve your goal simply in the following way:

Repository

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface CustomerRepository extends CrudRepository<Customer, String> {

}

Service

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CustomerService {
    
    @Autowired
    private CustomerRepository customerRepository;

    public Customer save(Customer customer) {
        Customer response = customerRepository.save(customer);
        return response;
    }
}
nachì
  • 46
  • 2
  • 4
  • nachi , thank you very much ! Is it in general possible to implement the interface with Iterable ? – tionRegist May 26 '21 at 23:36
  • I don't understand what the `Iterable` is for but maybe this can help you: https://stackoverflow.com/questions/11880924/how-to-add-custom-method-to-spring-data-jpa. Yes, in general I think it's possible. – nachì May 26 '21 at 23:48
  • WTF you are constantly changing the question lol ‍♂️ – nachì May 28 '21 at 15:11
  • However... that looks strange to me. Why do you need a mock? For unit testing? I can't get your point and I think you should explain your scenario/problem in a better way. – nachì May 28 '21 at 15:21