Spring data Jpa implement generic methods

1

I have 3 services with these methods in common create() , deleteById() , findAll() , getById() and update() .

@Service
public class AutorService {create(),deleteById(),findAll(),getById(),update(), etc...}

@Service
public class GrupoService {create(),deleteById(),findAll(),getById(),update(), etc...}

@Service
public class GeneroService {create(),deleteById(),findAll(),getById(),update(), etc...}

I want to transform them into an interface and create the implementation with the business logic in them.

Would it be a good practice to create an interface with these common methods to extend then implement them? Because every time I have to be creating these methods.

Example:

GenericService with the methods in common

public interface GenericService<T, I extends Serializable> {

    List<T> findAll();

    T getById(Long id);

    T create(T entity);

    T update(T entity);

    void deleteById(Long id);
}

AutorService extend GenericService and has its findByName () method

public interface AutorService extends GenericService<AutorEntity,Long>{

    public AutorEntity findByNome(String nome);
}

AutorServiceImpl implements GenericService with AutorService methods

@Service
public class AutorServiceImpl implements AutorService {/*.....*/}
    
asked by anonymous 23.11.2018 / 00:23

0 answers